当前位置:网站首页>自动化测试中,三种常用的等待方式,强制式(sleep) 、 隐式 ( implicitly_wait ) 、显式(expected_conditions)
自动化测试中,三种常用的等待方式,强制式(sleep) 、 隐式 ( implicitly_wait ) 、显式(expected_conditions)
2022-06-26 08:30:00 【冯大少】
在自动化测试总,主要有三种常用的等待方式,强制式(sleep) 、 隐式 ( implicitly_wait ) 、显式(expected_conditions)。
一、强制式(sleep)
sleep()是线程类(Thread)的方法,直接强制等待设置的秒数,静态方法强制当前正在执行的线程休眠(暂停执行),以“减慢线程”,当睡眠时间到期,则返回到可运行状态。
缺点:由于web加载的速度取决于测试的硬件、网速、服务器的响应时间等因素。如果时间设置太长,容易造成时间浪费,如果设置太短又可能会造成在web还没有加载完所需要定位的element,而出现报错。由于等待时间无法确定,使用太多的sleep会影响运行速度,大大地降低效率。
例: web完整加载需要 6秒,如果设置 sleep(10) ,就浪费了4秒。
二、隐式 ( implicitly_wait )
implicitly_wait() 是Object类的方法,设置超时时间,它的作用的全局的,也就是在一个脚本中只需要使用一次,作用范围是针对整个webdriver对象实例。在每次进行 find_element时起作用,implicitly_wait()会将一个超时的时间阀值传递给 WebDriver,在findelement或者findelements的时候,首先去找 web element,如果没有找到,判断时间否超超过 implicitlyWait()传递进来的阀值,如果没有超过,则再次找这个element,直到找到 element 或者时间超过最大阀值。
缺点:当使用了隐式等待执行测试的时候,如果 WebDriver没有在 DOM(Document Object Model)中找到element,将继续等待,超出设定时间后则抛出找不到element的异常。那就是当查找element或element并没有立即出现的时候,隐式等待将等待一段时间再查找 DOM,默认的时间是0,一旦设置了隐式等待,则它存在整个 WebDriver 对象实例的声明周期中,隐式等待会让一个正常响应的应用的测试变慢,它将会在寻找每个element的时候都进行等待,这样会增加整个测试执行的时间。
总的来说,相对 sleep()来说,即使在不确定web完整加载的时间情况下,虽然只要web加载完成就可以操作下一步的代码,或许可以节省部分的时间,但当页面某些 js无法加载,但是想找的element已经出来了,它还是会继续等待,直到页面加载完成(浏览器标签左上角圈圈不再转),才会执行下一步,这样也会影响运行速度。
例: 同样web完整加载需要 6秒,假设所需的element在最后才出现,如果设置 implicitly_wait (10) ,6秒后就运行下一步代码,相比 sleep(10) 就节省了4秒。
但如果由于web 的部分 js 加载比较慢,如果所需的 element 已经在第2秒出现了,但由于 web 如果还没完成完整加载,在等待完整加载后(那就是6秒后),这样就浪费了4秒的时间。
三、显式(expected_conditions)
显式等待能自定义等待条件,只要满足等待条件即可执行下一步代码操作,一般需要配合该类的until()和until_not()方法一起用,表示程序每隔一定时间就检查一遍条件是否成立,如果成立了就执行下一步,否则就继续等。直到超过设置的最长时间,然后抛出超时错误TimeoutException,以下介绍几个最常用的方法。
1. presence_of_element_located(locator) 判断某个element是否被加到了 dom 树里,并不代表该element一定可见
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
target = EC.presence_of_element_located(By.ID,'user')
2.visibility_of_element_located(locator) 判断element是否可见(可见代表element非隐藏,并且element宽和高都不等于 0)
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
target = EC.visibility_of_element_located(By.ID,'user')
3. element_to_be_clickable(locator) 判断某个element中是否可见并且可点击
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
target = EC.element_to_be_clickable(By.CSS_SELECTOR, '.form-users-search')
4. element_to_be_selected(element) 判断某个element是否被选中了,一般用在下拉列表
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
element = driver.find_element_by_class_name('selector')
EC.element_to_be_selected(element)
5. invisibility_of_element_located (locator) 判断某个element中是否不存在于dom树或不可见。 可见的element返回False,不存在的element见返回True;隐藏的element返回WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
locator = (By.ID, 'button')
target = EC.invisibility_of_element_located(locator)
四、WebDriverWait 与 expected_conditions结合使用
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
target = WebDriverWait(driver,5,1).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR,
"div[id='wrapper_wrapper']>div[id='container']>div[id='content_left']"
">div[class='result-op c-container new-pmd']"
">h3[class='t c-gap-bottom-small']>a")))
target.click()
其中 WebDriverWait(driver,5,1) 的 5表示 最长超时时间,默认以秒为单位; 1表示检测的间隔步长,在等待期间,每隔一定时间(默认0.5秒),调用until或until_not里的方法,直到它返回True或False.
边栏推荐
- Drawing with MATLAB (2) -- color ring
- static const与static constexpr的类内数据成员初始化
- Using transformers of hugging face to realize named entity recognition
- Deploy wiki system Wiki in kubesphere JS and enable Chinese full-text retrieval
- Exploration of webots and ROS joint simulation (I): software installation
- What is Qi certification Qi certification process
- [unity mirror] use of networkteam
- Record the problem yaml file contains Chinese message 'GBK' error
- leetcode2022年度刷题分类型总结(十二)并查集
- How to realize wireless Ethernet high-speed communication for multiple Mitsubishi PLCs?
猜你喜欢

Opencv learning notes 3

Clion installation + MinGW configuration + opencv installation

Euler function: find the number of numbers less than or equal to N and coprime with n

Apple motherboard decoding chip, lightning Apple motherboard decoding I.C

Opencv learning notes II
![[unity mirror] use of networkteam](/img/b8/93f55d11ea4ce2c86df01a9b03b7e7.png)
[unity mirror] use of networkteam

Detailed process of generating URDF file from SW model

Principle of playing card image segmentation

Optimize quiver function in MATLAB to draw arrow diagram or vector diagram (1) -matlab development

Structure diagram of target detection network
随机推荐
Digital image processing learning (II): Gaussian low pass filter
[unity mirror] use of networkteam
Transformers loading Roberta to implement sequence annotation task
Realizing sequence annotation with transformers
Record the problem yaml file contains Chinese message 'GBK' error
Koa_mySQL_Ts 的整合
在同花顺开户证券安全吗,
Recyclerview item gets the current position according to the X and Y coordinates
Use of PCL
Analysis of Yolo series principle
leetcode2022年度刷题分类型总结(十二)并查集
Matlab function foundation (directly abandon version)
Timer code guide in optee
What are the conditions for Mitsubishi PLC to realize Ethernet wireless communication?
Install Anaconda + NVIDIA graphics card driver + pytorch under win10_ gpu
Exploration of webots and ROS joint simulation (II): model import
三菱PLC若想实现以太网无线通讯,需要具备哪些条件?
opencv学习笔记三
Stream analysis of hevc learning
Detailed explanation of SOC multi-core startup process