def close(self): logger.info( "========================================================测试结束========================================================" ) logger.info("\n") self.driver.close() self.driver.quit()
def close_web_name(self, close_web_name, show_web_name): hander_list = self.driver.window_handles current_handle = self.driver.current_window_handle for i in hander_list: if i != current_handle: self.driver.switch_to.window(i) logger.info("3333333333") logger.info(self.assert_title(close_web_name)) if self.assert_title(close_web_name): logger.info("222222222222222222") self.driver.close() if close_web_name == show_web_name: logger.info("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") break logger.info("1111111111") close_web_name = show_web_name close_web_name(close_web_name, show_web_name)
def until(self, method, *args): """Calls the method provided with the driver as an argument until the \ return value is not False.""" screen = None stacktrace = None end_time = time.time() + self._timeout while True: try: value = method(*args) if value: logger.info("成功定位元素:{0}".format(*args)) return value except self._ignored_exceptions as exc: screen = getattr(exc, 'screen', None) stacktrace = getattr(exc, 'stacktrace', None) time.sleep(self._poll) if time.time() > end_time: break logger.info("元素查找失败:{0}".format(*args)) raise WebDriverException.TimeoutException('', screen, stacktrace)
def handle_windows(self, *args): value = len(args) if value == 1: if args[0] == "max": self.driver.maximize_window() elif args[0] == "min": self.driver.minimize_window() elif args[0] == "back": self.driver.back() elif args[0] == "go": self.driver.forward() elif args[0] == "refresh": self.driver.refresh() elif args[0] == "F5": ActionChains(self.driver).key_down(Keys.CONTROL).send_keys( Keys.F5).key_up(Keys.CONTROL).perform() elif value == 2: self.driver.set_window_size(args[0], args[1]) else: logger.info("你传递的参数有问题")
def __open_browser(self, browser, configuration_file_download): if browser == "chrome": options = webdriver.ChromeOptions() if configuration_file_download: prefs = { "profile.default_content_settings.popups": 0, "download.default_directory": local_config.FILE_DOWNLOAD_PATH } options.add_experimental_option("prefs", prefs) driver = webdriver.Chrome(executable_path=file_utils.location_file( local_config.CHROME_DRIVER_PATH), options=options) elif browser == "firefox": options = webdriver.FirefoxOptions() if configuration_file_download: options.set_preference("browser.download.dir", local_config.FILE_DOWNLOAD_PATH) options.set_preference("browser.download.folderList", "2") options.set_preference( "browser.download.manager.showWhenStarting", False) options.set_preference( "browser.helperApps.neverAsk.saveToDisk", "application/zip") driver = webdriver.Firefox( executable_path=file_utils.location_file( local_config.FIREFOX_DRIVER_PATH), options=options) elif browser == "ie": driver = webdriver.Ie(executable_path=file_utils.location_file( local_config.IE_DRIVER_PATH)) else: driver = webdriver.edge() logger.info("当前测试浏览器:{0} ,下载文件路径配置:{1}".format( browser, local_config.FILE_DOWNLOAD_PATH)) return driver
def __get_url(self, url): if self.driver != None: if 'http' in url: logger.info("测试线上网页:{0}".format(url)) self.driver.get(url) elif 'C' or 'D' or 'E' in url: self.driver.get(url) else: logger.info("测试本地网页:{0}".format(url)) else: logger.info("你传入的url路径有误:{0}".format(url))
def load_excel(self, file_path): path = file_utils.location_file(file_path) if not os.path.exists(path): logger.info("文件路径不存在:{0}".format(path)) return openpyxl.load_workbook(path)
def click_element(self, info, timeout=20): self.wait_element(info, timeout).click() logger.info("点击元素:{0}".format(info))
def send_value(self, info, key): self.wait_element(info).send_keys(key) logger.info("元素:{0},输入值:{1}".format(info, key))
def assert_title(self, title_name): logger.info(title_name) time.sleep(10) logger.info(str(self.driver.title)) return title_name in str(self.driver.title)
def __init__(self, browser, configuration_file_download): logger.info( "========================================================测试开始========================================================" ) self.driver = self.__open_browser(browser, configuration_file_download) self.pkb = PyKeyboard()
def open_url_is_true(self, url, title_name): self.__get_url(url) title_name_isture = self.assert_title(title_name) logger.info("当前网页:{0},结果:{1}".format(title_name, title_name_isture)) return title_name_isture