def find_element(self, element_info): """ 根据提供的元素参数信息进行元素查找 :param element_info:元素信息,字典类型{'element_name':'用户名输入框','locator_type':'xpath','locator_value':'//input[@name="account"]','timeout': 5 } :return: element对象 """ try: locator_type_name = element_info['locator_type'] locator_value_info = element_info['locator_value'] locator_timeout = element_info['timeout'] if locator_type_name == 'id': locator_type = By.ID elif locator_type_name == 'name': locator_type = By.NAME elif locator_type_name == 'class': locator_type = By.CLASS_NAME elif locator_type_name == 'xpath': locator_type = By.XPATH element = WebDriverWait(self.driver, locator_timeout) \ .until(lambda x: x.find_element(locator_type, locator_value_info)) logger.info('[%s]元素识别成功' % element_info['element_name']) # element = WebDriverWait(self.driver, locator_timeout)\ # .until(EC.presence_of_element_located((locator_type, locator_value_info))) except Exception as e: logger.error('[%s]元素不能识别,原因是%s' % (element_info['element_name'], str(e))) self.screenshot_as_file() # finally: # if element is None: # element = '' return element
def click(self, element_info): element = self.find_element(element_info) try: element.click() logger.info('[%s]元素进行点击操作' % element_info['element_name']) except Exception as e: logger.error('[%s]元素点击操作失败,原因是%s' % str(e)) self.screenshot_as_file()
def tearDown(self) -> None: # 测试用例失败截图 errors = self._outcome.errors for test,exc_info in errors: if exc_info: self.base_page.screenshot_as_file() self.base_page.close_tab() logger.info('---------测试方法执行完毕-----------')
def setUp(self) -> None: logger.info('---------测试方法开始执行-----------') self.base_page = BasePage(Browser().get_driver()) self.base_page.set_browser_max() self.base_page.implicitly_wait() self.base_page.open_url(self.url)
def setUpClass(cls) -> None: logger.info('') logger.info('==============测试类开始执行=============') cls.url = cfg.url
def test_error(): try: result = 10 / 0 except Exception: logger.error('Failed to get result', exc_info=True) logger.info('Finish')
def get_attribute_title(self, element_info): element = self.find_element(element_info) value = element.get_attribute('title') logger.info('[%s]元素获取其属性值是:%s' % (element_info['element_name'], value)) return value
def select_value(self, element_info, value): # 下拉选择操作 element = self.find_element(element_info) select_el = Select(element) time.sleep(1) select_el.select_by_value(value) logger.info('[%s]元素选择[%s]成功' % (element_info['element_name'], value))
def refresh(self): self.driver.refresh() logger.info('浏览器刷新操作')
def set_browser_min(self): self.driver.minimize_window() logger.info('设置浏览器最小化')
def set_browser_max(self): self.driver.maximize_window() logger.info('设置浏览器最大化')
def exit_driver(self): self.driver.quit() logger.info('退出浏览器')
def close_tab(self): self.driver.close() logger.info('关闭当前的tab页签')
def open_url(self, url): try: self.driver.get(url) logger.info('打开url地址 %s ' % url) except Exception as e: logger.error('不能打开指定的测试网址,原因是:%s' % e.__str__())
def get_title(self): value = self.driver.title logger.info('获取网页标题,标题是%s' % value) return value
def tearDownClass(cls) -> None: logger.info('==============测试类执行完毕=============')
def input(self, element_info, content): element = self.find_element(element_info) element.send_keys(content) logger.info('[%s]元素输入内容:%s' % (element_info['element_name'], content))