当前位置:网站首页>自动化测试中,三种常用的等待方式,强制式(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.
边栏推荐
- KNN resolution
- Digital image processing learning (II): Gaussian low pass filter
- Implementation of ffmpeg audio and video player
- RecyclerView Item 根据 x,y 坐标得到当前position(位置)
- VS2005 compiles libcurl to normaliz Solution of Lib missing
- Detailed process of generating URDF file from SW model
- The best time to buy and sell stocks to get the maximum return
- Relationship extraction --tplinker
- Realizing sequence annotation with transformers
- Line detection_ nanyangjx
猜你喜欢

opencv学习笔记三

And are two numbers of S

Whale conference one-stop intelligent conference system helps organizers realize digital conference management

Opencv learning notes 3

Relation extraction model -- spit model

Bezier curve learning

鲸会务一站式智能会议系统帮助主办方实现数字化会议管理

Leetcode22 summary of types of questions brushing in 2002 (XII) and collection search

Corn image segmentation count_ nanyangjx

Segmentation of structured light images using segmentation network
随机推荐
Relationship extraction --tplinker
(4) Independent key
optee中支持的时间函数
KNN resolution
Object extraction_ nanyangjx
torch. fft
(3) Dynamic digital tube
利用无线技术实现分散传感器信号远程集中控制
VS2005 project call free() compiled with static libcurl library reported heap error
What are the conditions for Mitsubishi PLC to realize Ethernet wireless communication?
Is it safe to open an account in flush,
SOC的多核启动流程详解
Nebula diagram_ Object detection and measurement_ nanyangjx
Summary of common instructions for arm assembly
Comparison between Apple Wireless charging scheme and 5W wireless charging scheme
1.17 daily improvement of winter vacation learning (frequency school and Bayesian school) and maximum likelihood estimation
三菱PLC若想实现以太网无线通讯,需要具备哪些条件?
[resolved]setonnavigationitemselectedlistener() deprecated
Ultrasonic image segmentation
Detailed explanation of self attention & transformer