一、隐式等待
以https://www.yahoo.com/的登录为例,需要在用户名输入框中输入“test”
我们先用未设置隐式等待的代码看看执行效果:
1 package waittyps; 2 3 import org.junit.jupiter.api.AfterEach; 4 import org.junit.jupiter.api.BeforeEach; 5 import org.junit.jupiter.api.Test; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.ie.InternetExplorerDriver; 9 10 class ImplicitWaitDemo { 11 private WebDriver driver; 12 private String url; 13 14 @BeforeEach 15 void setUp() throws Exception { 16 driver = new InternetExplorerDriver(); 17 url = "https://www.yahoo.com/"; 18 driver.manage().window().maximize(); 19 } 20 21 @Test 22 void test() { 23 driver.get(url); 24 driver.findElement(By.id("uh-signin")).click(); 25 driver.findElement(By.cssSelector("#login-username")).sendKeys("test"); 26 } 27 28 @AfterEach 29 void tearDown() throws Exception { 30 Thread.sleep(2000); 31 driver.quit(); 32 } 33 }
执行结果:执行失败,提示未定位到输入框元素(元素路径是没有问题的,因为未设置等待时间,所以没有找到“login-username”,导致控制台直接报错)
现在我们设置隐式等待时间3秒,然后运行代码:
1 package waittyps; 2 3 import java.util.concurrent.TimeUnit; 4 5 import org.junit.jupiter.api.AfterEach; 6 import org.junit.jupiter.api.BeforeEach; 7 import org.junit.jupiter.api.Test; 8 import org.openqa.selenium.By; 9 import org.openqa.selenium.WebDriver; 10 import org.openqa.selenium.ie.InternetExplorerDriver; 11 12 class ImplicitWaitDemo { 13 private WebDriver driver; 14 private String url; 15 16 @BeforeEach 17 void setUp() throws Exception { 18 driver = new InternetExplorerDriver(); 19 url = "https://www.yahoo.com/"; 20 driver.manage().window().maximize(); 21 // 隐式等待3秒 22 driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 23 } 24 25 @Test 26 void test() { 27 driver.get(url); 28 driver.findElement(By.id("uh-signin")).click(); 29 driver.findElement(By.cssSelector("#login-username")).sendKeys("test"); 30 } 31 32 @AfterEach 33 void tearDown() throws Exception { 34 Thread.sleep(2000); 35 driver.quit(); 36 } 37 }
运行结果:成功
二、显式等待
1 package waittyps; 2 3 import org.junit.jupiter.api.AfterEach; 4 import org.junit.jupiter.api.BeforeEach; 5 import org.junit.jupiter.api.Test; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.WebElement; 9 import org.openqa.selenium.ie.InternetExplorerDriver; 10 import org.openqa.selenium.support.ui.ExpectedConditions; 11 import org.openqa.selenium.support.ui.WebDriverWait; 12 13 class ImplicitWaitDemo { 14 private WebDriver driver; 15 private String url; 16 17 @BeforeEach 18 void setUp() throws Exception { 19 driver = new InternetExplorerDriver(); 20 url = "https://www.yahoo.com/"; 21 driver.manage().window().maximize(); 22 // 隐式等待3秒 23 // driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 24 } 25 26 @Test 27 void test() { 28 driver.get(url); 29 WebElement el = driver.findElement(By.id("uh-signin")); 30 el.click(); 31 // 实例化WebDriverWait对象,括号中内容表示(作用于谁,显式等待时长) 32 WebDriverWait wait = new WebDriverWait(driver,2); 33 // 用emailField接受查找到的元素,visibilityOfElementLocated表示元素可见后再执行输入操作 34 // 使用显式等待3秒,直到#login-username元素可用,如果在3秒内该元素可用就执行后面的操作,否则超过3秒直接抛出异常 35 WebElement emailField = wait.until( 36 ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#login-username"))); 37 emailField.sendKeys("test"); 38 // driver.findElement(By.cssSelector("#login-username")).sendKeys("test"); 39 } 40 41 @AfterEach 42 void tearDown() throws Exception { 43 Thread.sleep(2000); 44 driver.quit(); 45 } 46 }