web 自动化测试 I-强制等待与隐式等待

代码实现

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.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class WaitSetTest {

public static WebDriver driver;

//前置
@BeforeAll
static void  setUp(){
    driver = new ChromeDriver();
}
//后置
@AfterAll
static void dearDown(){
    driver.close();
}

//强制等待
@Test
void withThread() throws InterruptedException {
    driver.get("https://vip.ceshiren.com");
    Thread.sleep(3000);
    driver.findElement(By.xpath("//*[text()='个人中心']")).click();

}
//隐式等待
@Test
void withImplictlyWait(){
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    driver.get("https://vip.ceshiren.com");
    driver.findElement(By.xpath("//*[text()='个人中心']")).click();
}

//显式等待
@Test
void withWebdriverWait(){
    driver.get("https://vip.ceshiren.com/#/layout/section");
    //等待//*[text()='个人中心'],为一个可点击的状态
    WebElement massage_btn = new WebDriverWait(driver,Duration.ofSeconds(10))
            .until(ExpectedConditions.elementToBeClickable(By.id("//*[text()='个人中心']")));
    massage_btn.click();
}

}