当前位置:网站首页>自动化测试中,三种常用的等待方式,强制式(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.
边栏推荐
- Leetcode notes: binary search simple advanced
- Fourier transform of image
- nn. Modulelist and nn Sequential
- Design based on STM32 works: multi-functional atmosphere lamp, wireless control ws2812 of mobile app, MCU wireless upgrade program
- Opencv learning notes 3
- Recyclerview item gets the current position according to the X and Y coordinates
- Monitor iPad Keyboard Display and hide events
- The principle and function of focus
- opencv學習筆記三
- Esp8266wifi module tutorial: punctual atom atk-esp8266 for network communication, single chip microcomputer and computer, single chip microcomputer and mobile phone to send data
猜你喜欢

2020-10-20

Matlab drawing checkerboard (camera calibration)

Degree of freedom analysis_ nanyangjx

STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial

WBC learning notes (I): manually push WBC formula

Discrete device ~ resistance capacitance

Koa_ mySQL_ Integration of TS

Exploration of webots and ROS joint simulation (I): software installation

鲸会务为活动现场提供数字化升级方案

Corn image segmentation count_ nanyangjx
随机推荐
STM32 project design: smart door lock PCB and source code based on stm32f1 (4 unlocking methods)
Koa_ mySQL_ Integration of TS
Two ways to realize time format printing
Corn image segmentation count_ nanyangjx
Time functions supported in optee
关于极客时间 | MySQL实战45讲的部分总结
Installation of jupyter
在 KubeSphere 部署 Wiki 系统 wiki.js 并启用中文全文检索
Discrete device ~ diode triode
RF filter
ZLMediaKit推流拉流测试
Using MySQL and Qt5 to develop takeout management system (I): environment configuration
Analysis of Yolo series principle
Opencv learning notes II
Comparison between Apple Wireless charging scheme and 5W wireless charging scheme
Opencv learning notes 3
SOC wireless charging scheme
Zlmediakit push pull flow test
Bezier curve learning
OpenCV Learning notes iii