Esempio n. 1
0
 def wait_element_to_be_click(self,
                              loc,
                              img_doc,
                              timeout=20,
                              frequency=0.5):
     '''
     等待元素可点击
     :param loc: 元素定位的XPATH元组表达式
     :param img_doc: 截图说明
     :param timeout: 等待的超时时间
     :param frequency: 轮询频率
     :return:
     '''
     try:
         MyLog.info("开始等待页面元素<{}>是否可点击!".format(loc))
         start_time = time.time()
         WebDriverWait(self.driver, timeout,
                       frequency).until(EC.element_to_be_clickable(loc))
     except Exception as e:
         MyLog.error("页面元素<{}>等待可点击失败!".format(loc))
         self.save_screenshot(img_doc)
         raise e
     else:
         end_time = time.time()
         MyLog.info("页面元素<{}>等待可点击,等待时间:{}秒".format(
             loc, round(end_time - start_time, 2)))
Esempio n. 2
0
 def switch_to_webview(self, loc, img_doc, timeout=20, frequency=0.5):
     '''
     切换到webview页面
     :param loc: webview页面的元素
     :param img_doc: 截图说明
     :param timeout: 等待的超时时间
     :param frequency: 轮询频率
     :return:
     '''
     try:
         MyLog.info("等待元素{}可见,并进行webview切换".format(loc))
         start_time = time.time()
         WebDriverWait(self.driver, timeout, frequency).until(
             EC.visibility_of_element_located(loc))
         cons = self.driver.contexts
         MyLog.info("开始切换到webview:{}".format(cons[-1]))
         self.driver.switch_to.context(cons[-1])
     except Exception as e:
         MyLog.error("切换webview失败!")
         self.save_screenshot(img_doc)
         raise e
     else:
         end_time = time.time()
         MyLog.info("切换到webview:{}成功,等待时间:{}秒".format(
             cons[-1], round(end_time - start_time, 2)))
Esempio n. 3
0
 def switch_to_native_app(self, img_doc):
     '''
     切换到app原生页面
     :param img_doc: 截图说明
     :return:
     '''
     try:
         MyLog.info("切换到app原生页面")
         self.driver.switch_to.context('NATIVE_APP')
     except Exception as e:
         MyLog.error("切换到app原生页面失败!")
         self.save_screenshot(img_doc)
         raise e
Esempio n. 4
0
 def application_switching(self, package_name, activity_name, img_doc):
     '''
     应用切换
     :param package_name: 包名
     :param activity_name: 欢迎页面名
     :param img_doc: 截图说明
     :return:
     '''
     try:
         MyLog.info("切换应用到{}".format(package_name))
         self.driver.start_activity(app_package=package_name, app_activity=activity_name)
     except Exception as e:
         MyLog.error("切换应用到{}失败!".format(package_name))
         self.save_screenshot(img_doc)
         raise e
Esempio n. 5
0
 def get_element_text(self, loc, img_doc, timeout=20, poll_frequency=0.5):
     '''
     获取WebElement对象的文本值
     :param loc: 元素定位的XPATH元组表达式
     :param img_doc: 截图说明
     :param timeout: 等待的超时时间
     :param frequency: 轮询频率
     :return: WebElement对象的文本值
     '''
     try:
         MyLog.info("在{}中获取元素<{}>的文本值".format(img_doc, loc))
         self.wait_ele_visible(loc, img_doc, timeout, poll_frequency)
         text = self.get_element(loc, img_doc).text
     except Exception as e:
         MyLog.error("在{}中获取元素<{}>的文本值失败!".format(img_doc, loc))
         self.save_screenshot(img_doc)
         raise e
     else:
         MyLog.info("获取到的元素文本值为:{}".format(text))
         return text
Esempio n. 6
0
 def sliding_screen(self, direction, img_doc):
     '''
     滑屏操作
     :param direction: 滑屏方向:上-up;下-down;左-left;右-right
     :param img_doc: 截图说明
     :return:
     '''
     size = self.driver.get_window_size()
     try:
         MyLog.info("开始向{}方向滑动".format(direction))
         if direction.lower() == 'up':
             self.driver.swipe(start_x=size['width'] * 0.5,
                               start_y=size['height'] * 0.9,
                               end_x=size['width'] * 0.5,
                               end_y=size['height'] * 0.1,
                               duration=200)
         elif direction.lower() == 'down':
             self.driver.swipe(start_x=size['width'] * 0.5,
                               start_y=size['height'] * 0.1,
                               end_x=size['width'] * 0.5,
                               end_y=size['height'] * 0.9,
                               duration=200)
         elif direction.lower() == 'left':
             self.driver.swipe(start_x=size['width'] * 0.9,
                               start_y=size['height'] * 0.5,
                               end_x=size['width'] * 0.1,
                               end_y=size['height'] * 0.5,
                               duration=200)
         elif direction.lower() == 'right':
             self.driver.swipe(start_x=size['width'] * 0.1,
                               start_y=size['height'] * 0.5,
                               end_x=size['width'] * 0.9,
                               end_y=size['height'] * 0.5,
                               duration=200)
         else:
             MyLog.error("方向选择错误!")
     except Exception as e:
         MyLog.error("向{}方向滑动屏幕失败!".format(direction))
         self.save_screenshot(img_doc)
         raise e
Esempio n. 7
0
 def get_elements_text(self, loc, doc="", timeout=20, poll_frequency=0.5):
     '''
     获取WebElement对象的所有文本值
     :param loc: 元素定位的XPATH元组表达式
     :param img_doc: 截图说明
     :param timeout: 等待的超时时间
     :param frequency: 轮询频率
     :return: WebElement对象的文本值的列表
     '''
     try:
         MyLog.info("在{}中获取元素<{}>的所有文本值".format(doc, loc))
         self.wait_ele_visible(loc, doc, timeout, poll_frequency)
         all_text = self.get_elements(loc, doc)
         text_list = []
         for one_text in all_text:
             text_list.append(one_text.text)
     except Exception as e:
         MyLog.error("在{}中获取元素<{}>的所有文本值失败!".format(doc, loc))
         self.save_screenshot(doc)
         raise e
     else:
         MyLog.info("获取到的元素文本值列表为:{}".format(text_list))
         return text_list