def set_browser_min(self): try: self.driver.minimize_window() logutils.info('设置浏览器最小化') except Exception as e: logutils.error('设置浏览器最小化失败,原因是:%s' % e.__str__()) self.screen_shoot_as_file()
def close_tab(self): try: self.driver.close() logutils.info('关闭当前tab页签') except Exception as e: logutils.error('关闭浏览器失败,原因是:%s' % e.__str__()) self.screen_shoot_as_file()
def exit_driver(self): try: self.driver.quit() logutils.info('退出浏览器') except Exception as e: logutils.error('退出浏览器失败,原因是:%s' % e.__str__()) self.screen_shoot_as_file()
def find_element(self, element_info): ''' 根据提供的元素参数信息进行元素的查找, :param element_info: 元素信息参数,字典类{。。。} :return: element对象 ''' try: locator_type_name = element_info[ 'locator_type'] #locator_type 定位方式 locator_value_info = element_info[ 'locator_value'] # locator_value_info 被定位到的元素 locator_timeout = element_info['time_out'] #locator_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)) #self.driver传入浏览器,等待时间 locator_type:定位方式,locator_value_info:元素信息 logutils.info('[%s]元素识别成功' % element_info['element_name']) except Exception as e: logutils.error('[%s]元素不能识别,原因是%s' % (element_info['element_name'], e.__str__())) self.screen_shoot_as_file() # finally: # if element is None: # element='' return element
def browser_refresh(self): try: self.driver.refresh() logutils.info('刷新浏览器') except Exception as e: logutils.error('刷新浏览器操作失败,原因是:%s' % e.__str__()) self.screen_shoot_as_file()
def open_url(self, url): try: self.driver.get(url) logutils.info('打开浏览器path地址%s' % url) except Exception as e: logutils.error('不能打开指定的测试网址,原因是:%s' % e.__str__()) self.screen_shoot_as_file()
def implicitly_wait(self, seconds=conf.time_out): try: self.driver.implicitly_wait(seconds) logutils.info('设置隐式等待时间为5秒') except Exception as e: logutils.error('隐式等待设置失败,原因是:%s' % e.__str__()) self.screen_shoot_as_file()
def set_browser_back(self): try: self.wait() self.driver.back() logutils.info('浏览器返回上一页') except Exception as e: logutils.error('返回浏览器上一页失败的原因是:%s' % e.__str__())
def get_title(self): try: value = self.driver.title logutils.info('获取浏览器标题,标题是%s' % value) except Exception as e: logutils.error('获取浏览器title失败,原因是:%s' % e.__str__()) self.screen_shoot_as_file() return value
def move_to_element_be_mouse(self, element_info): try: element = self.find_element(element_info) #识别元素 ActionChains(self.driver).move_to_element(element).perform() logutils.info('鼠标移动至元素%s' % (element)) except Exception as e: logutils.error('鼠标移动至元素%s' % (element), '失败,失败的原因是:%s' % e.__str__())
def switch_to_frame(self, element_info): try: self.wait() element = self.find_element(element_info) self.driver.switch_to.frame(element) logutils.info('切换至frame框架') except Exception as e: logutils.error('切换至frame框架失败,失败的原因是:%s' % e.__str__())
def input_operation(self, element_info, content): try: element = self.find_element(element_info) element.send_keys(content) logutils.info('[%s]元素进行输入内容:%s' % (element_info['element_name'], content)) except Exception as e: logutils.error('输入操作失败,失败的原因是:%s' % e.__str__())
def click_operation(self, element_info): element = self.find_element(element_info) try: element.click() logutils.info('[%s]元素进行点击操作' % element_info['element_name']) except Exception as e: logutils.error('[%s]元素点击操作失败,原因是%s' % e.__str__()) self.screen_shoot_as_file()
def excute_script(self, js_str, element_info=None): try: if element_info: self.driver.execute_script(js_str) else: self.driver.execute_script(js_str, None) logutils.info('执行js操作') except Exception as e: logutils.error('执行JS操作失败,失败的原因是:%s' % e.__str__())
def get_text(self, element_info): try: element = self.find_element(element_info) logutils.info('获取元素文本信息%s' % element_info['element_name']) except Exception as e: logutils.error(('获取源于文本信息%s' % element_info['element_name']), '原因是:%s' % e.__str__()) self.screen_shoot_as_file() return element.text
def screen_shoot_as_file(self): try: repotr_path = os.path.join( os.path.abspath(os.path.dirname(__file__)), '..', conf.report_path) # 定义截图报告路径 repotr_dir = HTMLTestReportCN.ReportDirectory(repotr_path) repotr_dir.get_screenshot(self.driver) logutils.info('截图成功') except Exception as e: logutils.error('截图失败,失败的原因是:%s' % e.__str__())
def screen_shoot_as_file_old(self, *screenshot_path): try: current_dir = os.path.dirname(__file__) if len(screenshot_path) == 0: screenshot_filepath = conf.screen_shoot_path else: screenshot_filepath = screenshot_path[0] now = time.strftime('%Y_%m_%d_%H_%M_%S') screenshot_filepath = os.path.join(current_dir, screenshot_filepath, 'UITest——%s.png' % now) self.driver.get_screenshot_as_file(screenshot_filepath) logutils.info('截图') except Exception as e: logutils.error('截图失败,失败的原因是:%s' % e.__str__())
def switch_to_alter(self, action='accept', time_out=conf.time_out): try: self.wait(time_out) WebDriverWait(self.driver, time_out).until(EC.alert_is_present()) alter = self.driver.switch_to.alert alter_text = alter.text if action == 'accept': alter.accept() elif action == 'dismiss': alter.dismiss() logutils.info('切换至弹出框,返回弹出框文本信息%s' % (alter_text)) except Exception as e: logutils.error('切换至弹出框,返回弹出框文本信息%s' % (alter_text), '失败的原因是:%s' % e.__str__()) return alter_text