目录
为什么要添加等待
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.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
/**
 * @Author: 霍格沃兹测试开发学社
 * @Desc: '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
 */
public class WaitTest {
    public static WebDriver driver;
    @BeforeAll
    static void setUpAll() {
        driver = new ChromeDriver();
    }
    @AfterAll
    static void tearDownAll() {
        driver.quit();
    }
    @Test
    void waitSleep() {
        driver.get("https://vip.ceshiren.com/");
        driver.findElement(By.xpath("//*[text()='个人中心']"));
    }
}
直接等待
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.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
/**
 * @Author: 霍格沃兹测试开发学社
 * @Desc: '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
 */
public class WaitTest {
    public static WebDriver driver;
    @BeforeAll
    static void setUpAll() {
        driver = new ChromeDriver();
    }
    @AfterAll
    static void tearDownAll() {
        driver.quit();
    }
    @Test
    void waitSleep() throws InterruptedException {
        driver.get("https://vip.ceshiren.com/");
        Thread.sleep(3000);
        driver.findElement(By.xpath("//*[text()='个人中心']"));
    }
}
隐式等待
- 问题:难以确定元素加载的具体等待时间。
- 解决方案:针对于寻找元素的这个动作,使用隐式等待添加配置。
- 演练环境:https://vip.ceshiren.com/
- 原理:设置一个等待时间,轮询查找(默认0.5秒)元素是否出现,如果没出现就抛出异常
 #设置一个等待时间,轮询查找(默认0.5秒)元素是否出现,如果没出现就抛出异常
 driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
隐式等待无法解决的问题
- 元素可以找到,使用点击等操作,出现报错
- 原因:
- 页面元素加载是异步加载过程,通常html会先加载完成,js、css其后
- 元素存在与否是由HTML决定,元素的交互是由css或者js决定
- 隐式等待只关注元素能不能找到,不关注元素能否点击或者进行其他的交互
 
- 解决方案:使用显式等待
显式等待基本使用(初级)
- 示例: WebDriverWait(driver实例, 最长等待时间).until(结束条件);
- 原理:在最长等待时间内,是否满足结束条件
- 演练环境: 霍格沃兹测试开发
- 注意:在初级时期,先关注使用
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.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class WaitTest {
    public static WebDriver driver;
    @BeforeAll
    static void setUpAll() {
        driver = new ChromeDriver();
        //显式等待
        driver.manage().timeouts().implicitlyWait(
            Duration.ofSeconds(10));
    }
    @AfterAll
    static void tearDownAll() {
        driver.quit();
    }
    @Test
    void wait_untils(){
        driver.get("https://vip.ceshiren.com/#/ui_study");
        WebElement resElement = new WebDriverWait(driver, 
        Duration.ofSeconds(10))
        .until(ExpectedConditions.elementToBeClickable(
            By.cssSelector("#success_btn")));
        resElement.click();
    }
}