示例#1
0
    def click(self, selector):

        element = self.find_element(selector)
        try:
            element.click()
            logger.info("The element \' %s \' was clicked." % element.text)
        except NameError as e:
            logger.error("Failed to click the element with %s" % e)
示例#2
0
    def clear(self, selector):

        element = self.find_element(selector)
        try:
            element.clear()
            logger.info("Clear text in input box before typing.")
        except NameError as e:
            logger.error("Failed to clear in input box with %s" % e)
            self.get_windows_img()
示例#3
0
    def type(self, selector, text):

        element = self.find_element(selector)
        element.clear()
        try:
            element.send_keys(text)
            logger.info("Had type \' %s \' in inputBox" % text)
        except NameError as e:
            logger.error("Failed to type in input box with %s" % e)
            self.get_windows_img()
示例#4
0
    def find_element(self, selector):
        """
         这个地方为什么是根据=>来切割字符串,请看页面里定位元素的方法
         submit_btn = "id=>su"
         login_lnk = "xpath => //*[@id='u1']/a[7]"  # 百度首页登录链接定位
         如果采用等号,结果很多xpath表达式中包含一个=,这样会造成切割不准确,影响元素定位
        :param selector:
        :return: element
        """
        element = ''
        if '=>' not in selector:
            return self.driver.find_element_by_id(selector)
        selector_by = selector.split('=>')[0]
        selector_value = selector.split('=>')[1]

        if selector_by == "i" or selector_by == 'id':
            try:
                element = self.driver.find_element_by_id(selector_value)
                logger.info("Had find the element \' %s \' successful "
                            "by %s via value: %s " %
                            (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                logger.error("NoSuchElementException: %s" % e)
                self.get_windows_img()  # take screenshot
        elif selector_by == "n" or selector_by == 'name':
            element = self.driver.find_element_by_name(selector_value)
        elif selector_by == "c" or selector_by == 'class_name':
            element = self.driver.find_element_by_class_name(selector_value)
        elif selector_by == "l" or selector_by == 'link_text':
            element = self.driver.find_element_by_link_text(selector_value)
        elif selector_by == "p" or selector_by == 'partial_link_text':
            element = self.driver.find_element_by_partial_link_text(
                selector_value)
        elif selector_by == "t" or selector_by == 'tag_name':
            element = self.driver.find_element_by_tag_name(selector_value)
        elif selector_by == "x" or selector_by == 'xpath':
            try:
                element = self.driver.find_element_by_xpath(selector_value)
                logger.info("Had find the element \' %s \' successful "
                            "by %s via value: %s " %
                            (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                logger.error("NoSuchElementException: %s" % e)
                self.get_windows_img()
        elif selector_by == "s" or selector_by == 'selector_selector':
            element = self.driver.find_element_by_css_selector(selector_value)
        else:
            raise NameError("Please enter a valid type of targeting elements.")

        return element
示例#5
0
 def get_windows_img(self):
     """
     在这里我们把file_path这个参数写死,直接保存到我们项目根目录的一个文件夹.\Screenshots下
     """
     file_path = os.path.dirname(os.path.abspath('.')) + '/screenshots/'
     rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
     screen_name = file_path + rq + '.png'
     try:
         self.driver.get_screenshot_as_file(screen_name)
         logger.info(
             "Had take screenshot and save to folder : /screenshots")
     except NameError as e:
         logger.error("Failed to take screenshot! %s" % e)
         self.get_windows_img()
示例#6
0
 def quit_browser(self):
     logger.info("Now, Close and quit the browser.")
     self.driver.quit()
示例#7
0
    def open_browser(self, driver):
        config = configparser.ConfigParser()
        file_path = os.path.dirname(
            os.path.abspath('.')) + '/config/config.ini'
        config.read(file_path)

        browser = config.get("browserType", "browserName")
        logger.info("You had select %s browser." % browser)
        url = config.get("testServer", "URL")
        logger.info("The test server url is: %s" % url)

        if browser == "Firefox":
            driver = webdriver.Firefox()
            logger.info("Starting firefox browser.")
        elif browser == "Chrome":
            driver = webdriver.Chrome(self.chrome_driver_path)
            logger.info("Starting Chrome browser.")
        elif browser == "IE":
            driver = webdriver.Ie(self.ie_driver_path)
            logger.info("Starting IE browser.")

        driver.get(url)
        logger.info("Open url: %s" % url)
        driver.maximize_window()
        logger.info("Maximize the current window.")
        driver.implicitly_wait(10)
        logger.info("Set implicitly wait 10 seconds.")
        return driver
示例#8
0
 def close(self):
     try:
         self.driver.close()
         logger.info("Closing and quit the browser.")
     except NameError as e:
         logger.error("Failed to quit the browser with %s" % e)
示例#9
0
 def wait(self, seconds):
     self.driver.implicitly_wait(seconds)
     logger.info("wait for %d seconds." % seconds)
示例#10
0
 def back(self):
     self.driver.back()
     logger.info("Click back on current page.")
示例#11
0
 def forward(self):
     self.driver.forward()
     logger.info("Click forward on current page.")
示例#12
0
 def sleep(seconds):
     time.sleep(seconds)
     logger.info("Sleep for %d seconds" % seconds)
示例#13
0
 def get_page_title(self):
     logger.info("Current page title is %s" % self.driver.title)
     return self.driver.title