一惩妇、前言
- 學(xué)習(xí)Appium過(guò)程中跃巡,記錄三種等待時(shí)間的設(shè)置方法
- 整理網(wǎng)上資料后梳理完成
二爆哑、等待類型
1. 強(qiáng)制等待
- 需要設(shè)置固定的等待時(shí)間绘雁,無(wú)論元素是否加載完成橡疼,均需等待該時(shí)間
- 由time.sleep()方法實(shí)現(xiàn)
- 不推薦使用
import time
time.sleep(5) # 固定此段等待時(shí)間為5s
2. 隱式等待
- 隱式等待是由webdriver提供的超時(shí)等待方法;
- implicitly_wait()比 time.sleep() 更加智能庐舟,implicitly_wait()是在一個(gè)時(shí)間范圍內(nèi)智能等待欣除,time.sleep() 只能選擇一個(gè)固定的時(shí)間的等待;
- 當(dāng)使用了隱式等待執(zhí)行測(cè)試的時(shí)候挪略,如果 WebDriver沒(méi)有在 DOM中找到元素历帚,將繼續(xù)等待,超出設(shè)定時(shí)間后則拋出找不到元素的異常杠娱;
- 換句話說(shuō)挽牢,當(dāng)查找元素或元素并沒(méi)有立即出現(xiàn)的時(shí)候,隱式等待將等待一段時(shí)間再查找 DOM摊求,默認(rèn)的時(shí)間是0禽拔;
- 一旦設(shè)置了隱式等待,則它存在整個(gè) WebDriver 對(duì)象實(shí)例的聲明周期中室叉,隱式的等到會(huì)讓一個(gè)正常響應(yīng)的應(yīng)用的測(cè)試變慢睹栖;
- 它將會(huì)在尋找每個(gè)元素的時(shí)候都進(jìn)行等待,這樣會(huì)增加整個(gè)測(cè)試執(zhí)行的時(shí)間茧痕。
# 針對(duì)全局元素設(shè)置的等待時(shí)間
self.driver.implicitly_wait(20)
3. 顯示等待
- 顯式等待是針對(duì)某個(gè)元素來(lái)設(shè)置的等待時(shí)間
- 在設(shè)置時(shí)間內(nèi)野来,默認(rèn)每隔一段時(shí)間檢測(cè)一次當(dāng)前。頁(yè)面元素是否存在凿渊,如果超過(guò)設(shè)置時(shí)間檢測(cè)不到則拋出異常梁只。
- 詳細(xì)格式如下:
class WebDriverWait(object):
def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
"""Constructor, takes a WebDriver instance and timeout in seconds.
:Args:
- driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
- timeout - Number of seconds before timing out
- poll_frequency - sleep interval between calls
By default, it is 0.5 second.
- ignored_exceptions - iterable structure of exception classes ignored during calls.
By default, it contains NoSuchElementException only.
Example:
from selenium.webdriver.support.ui import WebDriverWait \n
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
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)
def __repr__(self):
return '<{0.__module__}.{0.__name__} (session="{1}")>'.format(
type(self), self._driver.session_id)
def until(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value is not False."""
screen = None
stacktrace = None
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message, screen, stacktrace)
def until_not(self, method, message=''):
"""Calls the method provided with the driver as an argument until the \
return value is False."""
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if not value:
return value
except self._ignored_exceptions:
return True
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message)
- 一般搭配until()或until_not()方法配合使用缚柳,另外,lambda還提供了一個(gè)運(yùn)行時(shí)動(dòng)態(tài)創(chuàng)建函數(shù)的方法搪锣。
from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id('test'))
歡迎評(píng)論補(bǔ)充
Blog:
- 簡(jiǎn)書:http://www.reibang.com/u/ec81abf35751
- CSDN:https://blog.csdn.net/qq_21238607
- 微信公眾號(hào):rzbbzr