appium---等待时间

  在做UI自动化的过程中,我们有时候为了等待元素的出现,需要加一些等待时间来帮助,但是有时候时间加的过多或者过少,这个没有办法判断,今天介绍几种等待时间,我们看看那种适合我们 ,我们就用哪一种

强制等待

看到名称就应该知道,强制等待,就是设置多少秒,就必须等待多少秒,才能继续往下面操作

time.sleep()

def sleep(seconds): # real signature unknown; restored from __doc__
    """
    sleep(seconds)
    
   延迟指定的秒数
    """
    pass

使用方法

# 直接在需要等待的地方添加
time.sleep(10)

隐式等待

隐式等待: implicitly_wait?() 默认参数的单位为妙,设置一个等待时间,它并不影响脚本的执行速度。当脚本执行到某个元素定位是,如果元素可以定位,则继续执行,如果元素定位不到,则它将以轮询的方式不断地判断元素是否被定位到。假设在第六秒定位到了元素则继续执行,若直到超出设置的时长10秒还没有定位到元素,则抛出异常。

def implicitly_wait(self, time_to_wait):
        """
        Sets a sticky timeout to implicitly wait for an element to be found,
           or a command to complete. This method only needs to be called one
           time per session. To set the timeout for calls to
           execute_async_script, see set_script_timeout.

        :Args:
         - time_to_wait: Amount of time to wait (in seconds)

        :Usage:
            driver.implicitly_wait(30)
        """
        if self.w3c:
            self.execute(Command.SET_TIMEOUTS, {
                'implicit': int(float(time_to_wait) * 1000)})
        else:
            self.execute(Command.IMPLICIT_WAIT, {
                'ms': float(time_to_wait) * 1000})

使用方法:

# 在需要等待的地方直接添加
driver.implicitly_wait(10)

Activity等待

Activity等待:  app特有一种等待,通过Activity的出现来帮助我们进行判断是否到达这个页面然后进行一系列的操作 ,通过wait_activity 进行判断

 def wait_activity(self, activity, timeout, interval=1):
        """等待一个activity,直到在规定时间内activity出现

        This is an Android-only method.

        :Args:
         - activity - target activity
         - timeout - max wait time, in seconds
         - interval - sleep interval between retries, in seconds
        """
        try:
            WebDriverWait(self, timeout, interval).until(
                lambda d: d.current_activity == activity)
            return True
        except TimeoutException:
            return False

使用方法:

直接在需要等待元素出现的地方添加

# 添加activity,后面加上等待的时间,超过时间就报错
driver.wait_activity('com.ali.user.mobile.login.ui.UserLoginActivity',30)

显示等待

显示等待本来准备等到写selenium教程的时候在介绍,但是感觉后面会用到,这里就直接给大家进行介绍了。

先看源码:

def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
        """
    driver: 返回一个webdriver实例化
    timeout:设置一个超时时长(S)
    poll_frequency:循环读取元素的时间,默认是0.5(s)
  
            使用方法 :
            from selenium.webdriver.support.ui import WebDriverWait 

            element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) 

            is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)). 

                        until_not(lambda x: x.find_element_by_id("someId").is_displayed())
        """
        self._driver = driver
        self._timeout = timeout
        self._poll = poll_frequency
        # avoid the divide by zero
        if self._poll == 0:
            self._poll = POLL_FREQUENCY
        exceptions = list(IGNORED_EXCEPTIONS)
        if ignored_exceptions is not None:
            try:
                exceptions.extend(iter(ignored_exceptions))
            except TypeError:  # ignored_exceptions is not iterable
                exceptions.append(ignored_exceptions)
        self._ignored_exceptions = tuple(exceptions)

从源码中分许出来三个参数的作用

driver:返回一个webdriver实例化

timeout:设置一个超时时长

poll_frequentcy:循环读取元素时间

使用方法:

# 导入方法
from selenium.webdriver.support.ui import WebDriverWait

element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId"))

其中until表示需要执行什么内容

注:显示等待和隐式等待不同的区别是一个是等待元素的加载,另一个是等待页面的加载

等待方法其实很多种,安静这里只简单的介绍这三种,那种方面用哪种,哪种简单用那种,感谢您的阅读,如果写的对您有所帮助的话,可以右下角点个关注。

原文地址:https://www.cnblogs.com/qican/p/12607735.html