基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待

时间:2022-12-20 16:50:44

一、隐式等待


package com.automation.waits;

import java.util.concurrent.TimeUnit;

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;

/**
* 类说明:隐式等待
* <br/>
* @version 1.0
* 2016年11月22日 下午8:56:14
*/
public class ImplictWait {

public static void main(String[] args) {
//1.打开浏览器;
System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//2.;浏览器最大化
driver.manage().window().maximize();
/**
* 3.设置全局隐式等待时间;
* <br/>使用implicitlyWait方法,设定查找元素的等待时间;
* <br/>当调用findElement方法的时候,没有立刻找到元素,就会按照设定的隐式等待时长等待下去;
* <br/>如果超过了设定的等待时间,还没有找到元素,就抛出NoSuchElementException异常;
*/
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //设定隐式等待时间为10秒;
//3.打开搜狗首页;
driver.get("http://www.sogou.com/");

try {
//4.定位搜狗首页:输入框对象、搜索按钮;
WebElement inputBox = driver.findElement(By.id("query"));
WebElement searchButton = driver.findElement(By.id("stb"));

//5.在输入框中输入元素,然后点击搜索按钮。
inputBox.sendKeys("输入框元素被找到了。");
searchButton.click();

} catch (NoSuchElementException e) { //如果元素没有找到,则抛出NoSuchElementException异常。
e.printStackTrace();
}
}
}

隐式等待可以设定,但是有一个缺点:
缺点:如果我们在代码中设定了隐式等待时间,当使用driver.findElement(By.*) 方法去查找页面元素的时候,如果没有第一时间找到元素,程序会等待下去。例如设置了隐式等待时间为10秒,某个元素没有一开始就出现,而是在第5秒的时候出现了,程序依然会等待10秒,然后才向下执行;
所以,推荐使用显示等待。

二、显式等待


显示等待比隐式等待,更加节约测试执行的时间;
优势:
1. 等待的方法多,都是ExpectedConditions类中的自带方法,可以进行不同的等待判断;
2. 等待灵活,等设置显示等待10秒,而在第5秒元素出现了,那么就会立即向下执行,而不会继续等待;只有超过了10秒,才抛出NoSuchElementException异常;


ExpectedConditions类自带的等待方法:



最常用的是第三个,判断元素在页面中是否存在:presenceOfElementLocated(By locator)


package com.automation.waits;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
* 类说明:显示等待
* <br/>
* @version 1.0
* 2016年11月22日 下午9:38:12
*/
public class explictWait {

public static void main(String[] args) {
//1.打开浏览器;
System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//2.;浏览器最大化
driver.manage().window().maximize();
//3.打开搜狗首页;
driver.get("http://www.baidu.com/");

/*
* 4.设置显示等待时长:10秒;
*/
WebDriverWait wait = new WebDriverWait(driver, 10);

//5.显示等待:标题是否出现:
try {
wait.until(ExpectedConditions.titleContains("百度一下,你就知道"));
System.out.println("百度首页的标题出现了:百度一下,你就知道");
} catch (NoSuchElementException e) { //如果标题没有找到,则抛出NoSuchElementException异常。
e.printStackTrace();
System.out.println("百度首页的标题没有找到");
}

//6.显示等待:搜索框是否出现-------最常用的显示等待;
try {
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("su")));
System.out.println("百度首页的搜索输入框出现了");
} catch (NoSuchElementException e) { //如果标题没有找到,则抛出NoSuchElementException异常。
e.printStackTrace();
System.out.println("百度首页的输入框没有找到");
}

//7.显示等待:页面搜索按钮是否可以被点击;
try {
wait.until(ExpectedConditions.elementToBeClickable(By.id("kw")));
System.out.println("百度首页的搜索按钮可以被点击");
} catch (NoSuchElementException e) { //如果标题没有找到,则抛出NoSuchElementException异常。
e.printStackTrace();
System.out.println("百度首页的搜索按钮找不到");
}
}
}

三、自定义显式等待

package com.automation.waits;

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.WebDriverWait;

/**
* 类说明:自定义显示等待
* <br/>
* @version 1.0
* 2016年11月22日 下午10:00:12
*/
public class CustomExplictWait {

public static void main(String[] args) {
//1.打开浏览器;
System.setProperty("webdriver.chrome.driver", "D:\\workspace\\A_Test\\resource\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//2.;浏览器最大化
driver.manage().window().maximize();
//3.打开搜狗首页;
driver.get("http://www.baidu.com/");

/*
* 4.自定义显示等待,在等待代码中找到某个元素;
*/
WebElement textInputBox =
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver){
return driver.findElement(By.xpath("//*[@type='text']"));
}
});
textInputBox.sendKeys("自定义显式等待1");

/*
* 5.自定义显示等待,获取页面元素//a[text()='关于搜狗']的文本值
*/
String text =
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<String>() {
@Override
public String apply(WebDriver driver){
return driver.findElement(By.xpath("//a[text()='关于搜狗']")).getText();
}
});
System.out.println(text); //打印文本值
}
}

自定义显式等待,要注意:

期望值是WebElement类型,那么返回值一定也是WebElement类型;

 

我们专注于持续集成,更多原创请关注:www.hordehome.com