Ejemplo n.º 1
0
 def find_element_click(self, locator, timeout=20):
     '''
     查找元素并点击
     :param locator: 定位的方式及需要定位的元素,以元组方式传入
     :param timeout:查找元素超时时间
     :return:返回可点击元素并点击
     '''
     try:
         return WebDriverWait(self.driver, timeout).until(
             expected_conditions.element_to_be_clickable(locator)).click()
     except Exception as e:
         self.screenshots()
         logger.error('获取可点击元素失败:{}'.format(e))
Ejemplo n.º 2
0
 def swipe_find_element_for_text(self, text):
     '''
     滑动页面并通过text属性查找元素
     :param text: 元素的text属性
     :return: 返回查该元素
     '''
     try:
         return self.driver.find_element_by_android_uiautomator(
             f'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("{text}").instance(0));'
         )
     except Exception as e:
         self.screenshots()
         logger.error('滑动查找元素失败:{}'.format(e))
Ejemplo n.º 3
0
 def find_element(self, locator, timeout=20):
     '''
     查找元素
     :param locator: 定位的方式及需要定位的元素,以元组方式传入
     :param timeout:查找元素超时时间
     :return:返回可见元素
     '''
     try:
         return WebDriverWait(self.driver, timeout).until(
             expected_conditions.visibility_of_element_located(locator))
     # return self.driver.find_element(*locator)
     except Exception as e:
         self.screenshots()
         logger.error('获取可视化元素失败:{}'.format(e))
Ejemplo n.º 4
0
 def is_toast_exist(self, toastmessage, timeout=20, poll_frequency=0.1):
     '''
     判断toast是否存在
     :param toastmessage: toast的文本内容
     :param timeout:查找元素超时时间
     :param poll_frequency:查找频率时间
     :return:布尔值
     '''
     try:
         toast_loc = (By.XPATH, "//*[contains(@text,'%s')]" % toastmessage)
         WebDriverWait(self.driver, timeout, poll_frequency).until(
             expected_conditions.presence_of_element_located(toast_loc))
         logger.info(self.driver.find_element(*toast_loc).text)
         return True
     except Exception as e:
         logger.error('Toast not found: {}'.format(e))
         return False
Ejemplo n.º 5
0
 def find_element_send_keys(
     self,
     locator,
     value,
     timeout=20,
 ):
     '''
     查找元素并输入
     :param locator: 定位的方式及需要定位的元素,以元组方式传入
     :param value: 需要输入的文本内容,传入字符串
     :param timeout: 查找元素超时时间
     :return: 返回存在元素并输入内容
     '''
     try:
         return WebDriverWait(self.driver, timeout).until(
             expected_conditions.presence_of_element_located(
                 locator)).send_keys(value)
     except Exception as e:
         self.screenshots()
         logger.error('获取存在元素失败:{}'.format(e))
Ejemplo n.º 6
0
 def swipe_to_left(self, n=1, base=0.1, duration=1000, text_element=None):
     '''
     从右向左滑动
     :param n: 滑动次数,默认为1
     :param base:辅助参数,忽略
     :param duration:滑动持续时间
     :param text_element:需要滑动的控件
     :return:None
     '''
     try:
         for i in range(n):
             if text_element != None:
                 x_left, x_right, y_top, y_down, x_center, y_center = self._get_element_size(
                     text_element)
                 self.driver.swipe(x_right * (1 - base), y_center,
                                   x_right * base, y_center, duration)
             else:
                 self.driver.swipe(self._width * (1 - base),
                                   self._height * 0.5, self._width * base,
                                   self._height * 0.5, duration)
     except Exception as e:
         self.screenshots()
         logger.error('向左滑动失败:{}'.format(e))
Ejemplo n.º 7
0
 def get_window_size(self):
     '''获取手机屏幕分辨率'''
     try:
         return self.driver.get_window_size()
     except Exception as e:
         logger.error('获取手机屏幕分辨率失败:{}'.format(e))