一、強(qiáng)制等待
名詞解釋:強(qiáng)制線程休眠一定時(shí)間,適合腳本調(diào)試時(shí)使用。
常用方法:Thread.sleep(long millis);
- 優(yōu)點(diǎn):適合腳本調(diào)試時(shí)使用;
- 缺點(diǎn):不能準(zhǔn)確把握需要等待的時(shí)間,如果用例中大量使用,會浪費(fèi)不必要的等待時(shí)間,影響用例的執(zhí)行效率;
- 操作未完成,等待結(jié)束,導(dǎo)致系統(tǒng)報(bào)錯(cuò);
- 操作完成,時(shí)間未到,導(dǎo)致浪費(fèi)時(shí)間;
二、隱式等待
名詞解釋:設(shè)置一個(gè)最長等待時(shí)間,輪詢查看頁面是否加載完成(默認(rèn) 0.5 秒),如果超過最長等待時(shí)間頁面未加載完成則拋出異常。
常用方法:driver.manage().timeouts().implicitlyWait(Duration duration);
- 優(yōu)點(diǎn):隱式等待對整個(gè) WebDriver 生命周期都起作用,在開始時(shí)設(shè)置一次即可。
- 缺點(diǎn):使用隱式等待,程序會一直等待頁面加載完成,才會執(zhí)行下一步操作(有時(shí)候頁面想要的元素早已加載完成了,但是頁面上個(gè)別元素還沒有加載完成,仍要等待頁面全部加載完成才能執(zhí)行下一步,使用也不是很靈活)
三、顯示等待
名詞解釋:定義等待條件,當(dāng)條件發(fā)生時(shí)才執(zhí)行后續(xù)代碼。程序會輪詢查看條件是否發(fā)生(默認(rèn) 0.5 秒),如果條件成立則執(zhí)行下一步,否則繼續(xù)等待,直到超過設(shè)置的最長時(shí)間,程序拋出異常。
Wait 接口有兩個(gè)實(shí)現(xiàn)類:WebDriverWait 和 FluentWait
常用方法1:FluentWait 流暢等待
源碼解釋:
- Wait 接口的一個(gè)實(shí)現(xiàn),可以動(dòng)態(tài)配置它的超時(shí)和輪詢間隔。
- 每個(gè) FluentWait 實(shí)例定義等待條件的最長時(shí)間,以及檢查條件的頻率。
- 可配置等待時(shí)忽略特定類型的異常,例如在頁面上搜索元素時(shí)的NoSuchElementExceptions。
- 線程不安全
示例用法:等待一個(gè)元素出現(xiàn)在頁面上 30 秒,每 5 秒檢查一次它的存在。
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(30L))
.pollingEvery(Duration.ofSeconds(5L))
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
示例用法:登錄測試人網(wǎng)站
package com.sunskblue.selenium.waitTest;
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.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.*;
import java.time.Duration;
import java.util.function.Function;
public class LoginTest {
public static WebDriver driver;
@BeforeAll
public static void SetUp() {
driver = new ChromeDriver();
}
@Test
public void LoginTest() throws InterruptedException {
driver.get("https://ceshiren.com/");
// 顯示等待核心邏輯
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
// 顯示等待核心邏輯
WebElement loginElement = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver webDriver) {
return driver.findElement(By.xpath("http://span[contains(text(),'登錄')]"));
}
});
loginElement.click();
driver.findElement(By.id("login-account-name")).clear();
driver.findElement(By.id("login-account-name")).sendKeys("961370624@qq.com");
driver.findElement(By.id("login-account-password")).clear();
driver.findElement(By.id("login-account-password")).sendKeys("*******");
driver.findElement(By.id("login-button")).click();
}
@AfterAll
public static void TearDown() {
driver.quit();
}
}
常用方法2:WebDriverwait
- WebDriverWait 繼承了 FluentWait,可以使用 FluentWait 中的特性;
- WebDriverWait 中有兩個(gè)有參構(gòu)造器,都調(diào)用了本類中的一個(gè)全參構(gòu)造器
- 全參構(gòu)造器中使用了 ignoring(NotFountException.class) 方法,使得 WebDriverWait 等待時(shí)自動(dòng)忽略NotFountException;
第一個(gè)參數(shù): WebDriver 對象
第二個(gè)參數(shù):最長等待時(shí)間
第三個(gè)參數(shù):輪詢時(shí)間
優(yōu)點(diǎn):等待判斷準(zhǔn)確,不會浪費(fèi)多余的等待時(shí)間,在用例中使用,可以提高執(zhí)行效率。
缺點(diǎn):
1、使用相對比較復(fù)雜;
2、和強(qiáng)制等待類似,每一行等待只執(zhí)行一次,如果要進(jìn)行多個(gè)元素的等待,則需要多次寫入。
示例用法:等待一個(gè)元素出現(xiàn)在頁面上 30 秒
package com.sunskblue.selenium.waitTest;
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.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
/**
* 顯示等待
*/
public class DisplayWaitTest {
public static WebDriver driver;
@BeforeAll
public static void SetUp() {
driver = new ChromeDriver();
}
@Test
public void LoginTest() throws InterruptedException {
driver.get("https://ceshiren.com/");
// 核心邏輯
WebElement loginElement = new WebDriverWait(driver, Duration.ofSeconds(30)).until(
new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver webDriver) {
return webDriver.findElement(By.xpath("http://span[contains(text(),'登錄')]"));
}
});
loginElement.click();
driver.findElement(By.id("login-account-name")).clear();
driver.findElement(By.id("login-account-name")).sendKeys("961370624@qq.com");
driver.findElement(By.id("login-account-password")).clear();
driver.findElement(By.id("login-account-password")).sendKeys("********");
driver.findElement(By.id("login-button")).click();
}
@AfterAll
public static void TearDown() {
driver.quit();
}
}
四、ExpectedCondition 模塊常用方法
https://blog.csdn.net/zyooooxie/article/details/84561783文章來源:http://www.zghlxwxcb.cn/news/detail-401822.html
五、隱式等待和顯示等待公用優(yōu)先級
Selenium官網(wǎng)明確說明說兩者不建議一同使用;
共用時(shí),兩種等待取決于誰的時(shí)間更長;文章來源地址http://www.zghlxwxcb.cn/news/detail-401822.html
到了這里,關(guān)于五、Selenium 三種等待方式:強(qiáng)制等待、隱式等待、顯示等待的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!