Esempio n. 1
0
 def wait_element_visible(self, loc, img_doc, timeout=20, frequency=0.5):
     """
     等待元素可见
     :param frequency:
     :param loc:
     :param img_doc:截图图片
     :param timeout:
     :param frequeny:
     :return:
     """
     case_log.info("在 {} ,等待元素 {} 可见。".format(img_doc, loc))
     start_time = time.time()
     try:
         ele = WebDriverWait(self.driver, timeout, frequency).until(
             EC.visibility_of_element_located(loc))
     except Exception as e:
         # 异常截图 - 通过截图名称,知道是哪个页面或者那个模块出错了
         # 异常日志捕获
         case_log.exception("页面{}等待元素可见失败".format(loc))
         self.save_screen_shoot(img_doc)
         raise e
     else:
         end_time = time.time()
         case_log.info("页面元素{}等待存在,等待时间长为:{}s 。".format(
             loc, round(end_time - start_time, 3)))
         return ele
Esempio n. 2
0
 def get_frame_element(self,
                       loc,
                       img_doc,
                       timeout=20,
                       frequency=0.5):  # need update
     """
     跳转到iframe
     :param frequency:
     :param timeout:
     :param img_doc:
     :param loc:
     :return:
     """
     case_log.info("在 {},跳转到iframe{}。".format(img_doc, loc))
     start_time = time.time()
     try:
         ele = WebDriverWait(self.driver, timeout, frequency).until(
             EC.frame_to_be_available_and_switch_to_it(loc))
     except TimeoutError:
         # 异常截图 - 通过截图名称,知道是哪个页面或者那个模块出错了
         # 异常日志捕获
         case_log.exception("跳转iframe失败")
         self.save_screen_shoot(img_doc)
     else:
         end_time = time.time()
         case_log.info("跳转到iframe{},等待时间长为:{}s 。".format(
             loc, round(end_time - start_time, 3)))
         return ele
Esempio n. 3
0
 def wait_page_contain_element(self,
                               loc,
                               img_doc,
                               timeout=20,
                               frequency=0.5):
     """
     等待元素存在
     :param loc:
     :param img_doc:截图图片
     :param timeout:
     :param frequeny:
     :return:
     """
     case_log.info("在 {},等待元素{}存在。".format(img_doc, loc))
     start_time = time.time()
     try:
         ele = WebDriverWait(self.driver, timeout, frequency).until(
             EC.presence_of_element_located(loc))
     except TimeoutError:
         # 异常截图 - 通过截图名称,知道是哪个页面或者那个模块出错了
         # 异常日志捕获
         case_log.exception("页面元素{}等待元素存在失败".format(loc))
         self.save_screen_shoot(img_doc)
     else:
         end_time = time.time()
         case_log.info("页面元素{}等待存在,等待时间长为:{}s 。".format(
             loc, round(end_time - start_time, 3)))
         return ele
Esempio n. 4
0
 def scroll(self, ele, img_doc):
     try:
         overflow = 1
         for i in range(len(ele)):
             if not overflow % 6:  # 每看到6个元素就滑动1次
                 driver.execute_script("arguments[0].scrollIntoView();",
                                       ele[i])
             overflow += 1
     except Exception as e:
         case_log.exception("在{}中点击元素,点击元素失败".format(img_doc))
         self.save_screen_shoot(img_doc)
         raise e
Esempio n. 5
0
 def get_mouse_element_click(self, img_doc, timeout=20, frequency=0.5):
     """鼠标悬浮"""
     self.wait_element_visible(img_doc, timeout, frequency)
     ele = self.get_element(img_doc)
     case_log.info("在 {},对元素{}进行鼠标操作 。".format(img_doc))
     try:
         ac = ActionChains(self.driver)
         return ac.move_to_element(ele).click(ele).perform()
     except Exception as e:
         case_log.exception("鼠标操作元素失败")
         self.save_screen_shoot(img_doc)
         raise e
Esempio n. 6
0
 def get_element_click(self, loc, img_doc, timeout=20, frequency=0.5):
     """等待元素可点击"""
     self.wait_element_visible(loc, img_doc, timeout, frequency)
     ele = self.get_element(loc, img_doc)
     case_log.info("在 {},点击元素{}。".format(img_doc, loc))
     try:
         WebDriverWait(self.driver, timeout, frequency).until(
             EC.visibility_of_element_located(loc))
         return ele.click()
     except Exception as e:
         case_log.exception("在{}中点击元素,点击元素失败".format(img_doc, loc))
         self.save_screen_shoot(img_doc)
         raise e
Esempio n. 7
0
 def get_element_input_txt(self,
                           loc,
                           img_doc,
                           content,
                           timeout=20,
                           frequency=0.5):
     """对输入框输入文本内容"""
     self.wait_element_visible(loc, img_doc, timeout, frequency)
     ele = self.get_element(loc, img_doc)
     case_log.info("在 {},对元素{}输入内容:{}。".format(img_doc, loc, content))
     try:
         return ele.send_keys(content)
     except Exception as e:
         case_log.exception("在元素{}中输入内容{}元素输入文本失败".format(loc, content))
         self.save_screen_shoot(img_doc)
         raise e
Esempio n. 8
0
 def get_element(self, loc, img_doc):
     """获取页面中的所有元素"""
     case_log.info("在 {}, 查找元素{}可见。".format(img_doc, loc))
     start_time = time.time()
     try:
         ele = self.driver.find_element(*loc)
     except Exception as e:
         # 异常截图 - 通过截图名称,知道是哪个页面或者那个模块出错了
         # 异常日志捕获
         case_log.exception("在{}中查找元素{}查找失败!".format(img_doc, loc))
         self.save_screen_shoot(img_doc)
         raise e
     else:
         end_time = time.time()
         case_log.info("页面元素{}查找存在,等待时间长为:{}s 。".format(
             loc, round(end_time - start_time, 3)))
         return ele
Esempio n. 9
0
 def get_element_select(self, loc, img_doc, timeout=20, frequency=0.5):
     """
     等待元素可以被选择
     :param loc:
     :param fqc:
     :return:
     """
     case_log.info("在 {},等待元素{}可以选择。".format(img_doc, loc))
     start_time = time.time()
     try:
         ele = WebDriverWait(self.driver, timeout, frequency).until(
             EC.element_located_to_be_selected(loc))
     except Exception as e:
         case_log.exception("等待元素{}选择失败".format(loc))
         self.save_screen_shoot(img_doc)
     else:
         end_time = time.time()
         case_log.info("页面等待元素{}可以选择,等待时间长为:{}s 。".format(
             loc, round(end_time - start_time, 3)))
         return ele
Esempio n. 10
0
 def get_element_txt(self, loc, img_doc, timeout=20, frequency=0.5):
     """
     获取元素文本只要存在就行,不需要可见
     :param loc:
     :param img_doc:截图图片
     :param timeout:
     :return:
     """
     self.wait_page_contain_element(loc, img_doc, timeout, frequency)
     ele = self.get_element(loc, img_doc)
     case_log.info("在 {} ,获取元素文本内容: {} 。".format(img_doc, loc))
     try:
         text = ele.text
     except Exception as e:
         case_log.exception("在{}中获取元素{}元素文本内容失败".format(loc, img_doc))
         self.save_screen_shoot(img_doc)
         raise e
     else:
         case_log.info("获取元素文本内容为:{} ".format(text))
         return text
Esempio n. 11
0
 def get_element_attribute(self,
                           loc,
                           img_doc,
                           attribute_name,
                           timeout=20,
                           frequency=0.5):
     """获取WebElement对象的属性值"""
     self.wait_element_visible(loc, img_doc, timeout, frequency)
     ele = self.get_element(loc, img_doc)
     case_log.info("在 {},获取元素{}属性{}。".format(img_doc, attribute_name, loc))
     try:
         value = ele.get_attribute(attribute_name)
     except Exception as e:
         case_log.exception("在 {}中获取元素{}的属性{}获取元素属性失败".format(
             img_doc, loc, attribute_name))
         self.save_screen_shoot(img_doc)
         raise e
     else:
         case_log.info("获取元素属性内容为: {} ".format(value))
         return value