等待机制简介
系统在实际工作中引入等待机制可以保证代码运行的稳定性,保证代码运行不会受网速、计算机心梗等条件的约束。
等待就是当系统运行是,如果页面的渲染速度跟不上程序的运行速度,就需要认为地区限制程序执行的速度。
测试元在做 Web 自动化测试时,一般要等待页面元素加载完成后,才能执行测试操作,否则会报找不到元素等错误,这样就要求在有些测试场景下加上等待机制。
最常见的等待机制有 3 种:隐式等待、显示等待和强制等待,下面介绍这 3 种等待机制。
1.隐式等待
我们在测试用例中设置一个隐式等待时间,测试用例执行时会按时间轮询查找(默认 0.5 秒)元素是否出现,如果在轮询查找的时间内元素没出现系统就抛出异常。
隐式等待的作用域是全局的,隐式等待可以在 setup 方法中设置,是作用在整个 Session 的声明周期。也就是说只要设置一次隐式等待,后面就不需要再设置。如果再次设置隐式等待,那么后一次的设置会覆盖前一次的设置。
实战演示
当我们在 DOM 结构中查找元素,且元素处于不能立即交互的状态时,将会触发隐式等待。
self.drvier.implicitly_wait(30)
复制代码
//隐式等待调用方式,设置等待时间为30秒driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
复制代码
2.显式等待
显式等待是在代码中定义等待条件,触发改条件后再执行后续代码,这是根据判断条件进行等待。通俗地讲就是,程序每隔一段时间进行一次条件判断,如果条件成立,则执行下一步;否则继续等待,知道超过设置的最长时间。核心用法代码如下。
# 导入显式等待from selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_condittions...# 设置10秒的最大等待时间,等待(By.TAG_NAME,"title")这个元素被点击WebDriverWait(driver,10).until( expected_conditions eleniumt_to_be_clickable((By.TAG_NAME,"title")))...
复制代码
这里通过导入 expected_conditions 这个库来满足显式等待所需的使用场景,但是 expected_conditions 库并不能满足所有场景,这个时候就需要定制化开发一个库来满足特定场景,Java 版的实现如下。
import org.openqa.selenium.support.ui.ExpectedCondition;import org.openqa.selenium.support.ui.WebDriverWait;...//设置10秒的最大等待时间,等待(By.tag_NAME,"title")这个元素被点击WebDriverWait wait = new WebDriverWait(driver,10);wait.unitl(ExpectedConditions.elementToBClickable(By.tagName("title")));...
复制代码
实战演示
假设:测试 Web 应用中某个元素超过指定的个数,就可以执行某一个操作。
def ceshiren(): # 定义一个方法 def wait_ele_for(driver): # 将找到的元素个数赋值给eles eles = driver.find_enements(By.XPATH,'//*[@id="site-test-logo"]') #返回结果 returun len(eles)>0 driver = webdriver.Chrome() driver.get('https://ceshiren.com') # 等待10秒,直到wait_ele_for返回true WebDriverWait(driver,10).until(wait_ele_for)
复制代码
void ceshiren(){ webDriver = new ChromeDriver(); webDriver.get("https://ceshiren.com"); //等待10秒,直到wait_ele_for返回true new WebDriverWait(webDriver,10).until((ExpectredCondition<Boolean>) size ->waitEleFor());}//定义方法Boolean waitEleFor(){ // 将找到的元素个数赋值给 elements List<WebElement>element = webDriver.findElements(By.xpath("//*[@id='sire-text-logo']")); return elements.size()>0}
复制代码
3.强制等待
强制等待是使程序中的线程休眠一定是啊金。强制等待一般在隐式等待和显式等待都不起作用使用。示例代码如下(Python 版和 Java 版)。
//等待2000毫秒,相当于等待2秒Thread.sleep(2000)
复制代码
实战演示
访问测试人社区(https://ceshiren.com),点击 “ 分类” 按钮,然后点击 “霍格沃兹答疑区”链接(见图 3-10)
我们进行上述操作时,当点击“分类”按钮后,发现网页中的严肃还未加载完成,这时就需要隐式等待。在点击 ”霍格沃兹答疑区“链接时,网页中的元素已加载完成,但是这个链接还处在不可点击的状态,这时要用到显示等待。下面是实战演示代码(Python 版和 Java 版)。
#导入依赖import time from selenium import webdriverfrom selenium webdriver.common.by import Byfrom selenium webdriver.support import expected_conditionsfrom selenium webdriver.support.wait import WebDriverWait
class TestHogwaets(): def setup(self): self.driver = webdriver.Chrome() self.driver.get('https://ceshiren.com/') #加入隐式等待 self.driver.implicitly_wait(5) def teardown(self): #加入强制等待 time.sleep(10) self.driver.quit() def test_hogwarts(self): #元素定位,这里的category_name是一个元组 category_name = (By.LINK_TEXT,"开源项目") #加入显式等待 WebDriverWait(self.driver,10).until( expected_conditions.ellement_to_be_clickable(category_name)) #点击开源项目 self.driver.find_element(*category_name).click()
复制代码
import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
public class WebDriverWaitTest{ private static ChromeDriver driver; @BeforeAll public static void setUp(){ System.setProperty( "webdrive.chrome.driver", "/driver/chrome95/chromedriver" ); driver = new ChromeDrive(); driver.manage().timeout().implicitlyWait(60,TimeUnit.SECONDS); } @AfterAll public static void teardown(){ driver.quit(); } @Test public void waitTest(){ driver.get("https://ceshiren.com/"); By locator = By.linkTest("开源项目"); //加入显式等待 WebDriverWait wait = new WebDriverWait(driver,10); Wait.until(ExpectedConditions.elementToBeClickable(locator)); //点击开源项目 driver.findElement(locator).click(); }}
复制代码
搜索微信公众号:TestingStudio 霍格沃兹的干货都很硬核
评论