def get_elements(self, locator, doc=""): logger.info("{0}:查找所有匹配的元素:{1}".format(doc, locator)) try: return self.driver.find_elements(*locator) except: logger.exception("查找元素失败!!!") self.save_screenshot(doc) raise
def save_screenshot(self, doc): #图片名称 页面名称 操作名称 时间 png filePath = dir_config.screenshot_dir + "/{0}_{1}.png".format( doc, time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())) try: self.driver.save_screenshot(filePath) logger.info("截屏成功,文件路径为:{}".format(filePath)) except: logger.info("截图失败")
def New_workorder(self): try: WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located( (By.XPATH, "/html/body/div[5]/div/div"))) logger.info("断言成功,用例执行成功") return True except: logger.info("断言失败,用例执行失败") return False
def select_text(self, locator, text, times=15, poll_frequency=0.5, doc=""): self.wait_eleVisible(locator, times, poll_frequency, doc) ele = self.get_element(locator, doc) logger.info("{0}:下拉框text选择元素:{1}".format(doc, locator)) try: Select(ele).select_by_visible_text(text) except: logger.exception("元素下拉框通过text选择元素操作失败!!!") self.save_screenshot(doc) raise
def click_element(self, locator, times=15, poll_frequency=0.5, doc=""): self.wait_eleVisible(locator, times, poll_frequency, doc) ele = self.get_element(locator, doc) logger.info("{0}:点击元素:{1}".format(doc, locator)) try: ele.click() except: logger.exception("元素点击操作失败!!!") self.save_screenshot(doc) raise
def get_text(self, locator, times=15, poll_frequency=0.5, doc=""): self.wait_eleVisible(locator, times, poll_frequency, doc) ele = self.get_element(locator, doc) logger.info("在{0}获取元素:{1}的文本内容".format(doc, locator)) try: return ele.text except: logger.exception("获取元素文本内容失败!!!") self.save_screenshot(doc) raise
def input_text(self, locator, text, times=15, poll_frequency=0.5, doc=""): self.wait_eleVisible(locator, times, poll_frequency, doc) ele = self.get_element(locator, doc) logger.info("{0}:输入操作:{1}".format(doc, locator)) try: ele.send_keys(text) except: logger.exception("元素输入操作失败!!!") self.save_screenshot(doc) raise
def isexist_logout_ele(self): #如果存在就返回Ture,如果不存在就返回False try: WebDriverWait(self.driver, 10).until( EC.visibility_of_element_located( (By.XPATH, "//*[@id='currentChatLi']/i"))) logger.info("断言成功,用例执行成功") return True except: logger.info("断言失败,用例执行失败") return False
def wait_elePresence(self, locator, times=15, poll_frequency=0.5, doc=""): logger.info("{0}:等待元素{1}存在".format(doc, locator)) try: start = datetime.datetime.now() WebDriverWait(self.driver, times, poll_frequency).until( EC.presence_of_element_located(locator)) end = datetime.datetime.now() wait_time = (end - start).seconds logger.info("等待结束,等待时长为:{}秒".format(wait_time)) except: logger.exception("等待元素存在失败!!!") self.save_screenshot(doc) raise
def wait_eleVisible(self, locator, times=15, poll_frequency=0.5, doc=""): logger.info("{}:等待元素{}可见".format(doc, locator)) try: start = datetime.datetime.now() WebDriverWait(self.driver, times, poll_frequency).until( EC.visibility_of_element_located(locator)) end = datetime.datetime.now() wait_time = (end - start).seconds logger.info( "{0} :元素{1}已可见 ,等待起始时间:{2},等待结束时间:{3}, 等待时长为:{4}秒".format( doc, locator, start, end, wait_time)) except: logger.exception("等待元素可见失败!!!") self.save_screenshot(doc) raise
def switch_iframe(self, iframe_reference, times=15, poll_frequency=0.5, doc=""): logger.info("{0}:等待表单元素{1}存在,并进入该表单.".format(doc, iframe_reference)) try: start = datetime.datetime.now() WebDriverWait(self.driver, times, poll_frequency).until( EC.frame_to_be_available_and_switch_to_it(iframe_reference)) end = datetime.datetime.now() wait_time = (end - start).seconds logger.info("等待结束,等待时长为:{}秒".format(wait_time)) except: logger.info("等待表单元素失败!!!") self.save_screenshot(doc) raise
def switch_parent_iframe(self, doc=""): logger.info("{}退回上层表单".format(doc)) self.driver.switch_to.parent_frame()