WEB自動化(JAVA版)——三大等待

硬性等待

  • 線程休眠,強制等待
  • Thread.sleep(long millis);

隱式等待

  • 在設置的超時時間範圍內不斷查找元素,直到找到元素或者超時
  • 設置方式:
    driver.manage.timeouts().implicitlyWait(long time, TimeUnit unit);

優點:相對靈活
缺點:設置是針對全局的,在WebDriver實例整個生命週期有效,但並不是所有的元素都需要等待。

顯式等待

  • 用來等待某個條件發生後再繼續執行後續代碼(如找到元素、元素可點擊、元素已顯示等)
  • 設置方式:
    WebDriverWait wait = new WebDriverWait();
    WebElement element = wait.until(expectCondition);
方法 等待條件
visibilityOfElementLocated(By locator) 頁面元素在頁面存在並且可見
elementToBeClickable(By locator) 頁面元素是否在頁面上可用和可被點擊
elementToBeSelected(WebElement element) 頁面元素處於被選中狀態
textToBePresentInElement(By locator) 在頁面元素中是否包含特定的文本
presenceOfElementLocated(By locator) 頁面元素在頁面中存在

代碼示例

硬性等待代碼示例

package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ElementWait {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {
		openChrome();
		chromeDriver.get("http://www.baidu.com");
		chromeDriver.findElement(By.id("kw")).sendKeys("自動化測試");
		chromeDriver.findElement(By.id("su")).click();
		//代碼執行速度太快UI元素沒有立馬顯示出來,造成兩者不同步,從而元素找不到
		Thread.sleep(3000);
		chromeDriver.findElement(By.xpath("//a[text()='資訊']")).click();
	}
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打開Chrome瀏覽器
		chromeDriver = new ChromeDriver();
	}
}

隱式等待代碼示例

package com.test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ElementWait {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {		
		//隱式等待
		openChrome();
		//在driver實例化完成之後設置隱式等待,設置超時的時間爲5S
		chromeDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		chromeDriver.get("http://www.baidu.com");
		chromeDriver.findElement(By.id("kw")).sendKeys("自動化測試");
		chromeDriver.findElement(By.id("su")).click();
		//代碼執行速度太快UI元素沒有立馬顯示出來,造成兩者不同步,從而元素找不到
		//Thread.sleep(3000);
		chromeDriver.findElement(By.xpath("//a[text()='--NI自動化測試方案']")).click();		
	}
	
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打開Chrome瀏覽器
		chromeDriver = new ChromeDriver();
	}
}

顯示等待代碼示例

package com.test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
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;

public class ElementWait {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {
		//顯式等待
		openChrome();
		chromeDriver.get("http://www.baidu.com");
		chromeDriver.findElement(By.id("kw")).sendKeys("自動化測試");
		chromeDriver.findElement(By.id("su")).click();
		//代碼執行速度太快UI元素沒有立馬顯示出來,造成兩者不同步,從而元素找不到
		//Thread.sleep(3000);
		WebDriverWait webDriverWait = new WebDriverWait(chromeDriver,5);
		webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='--NI自動化測試方案']")));
		chromeDriver.findElement(By.xpath("//a[text()='--NI自動化測試方案']")).click();			
	}
	
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打開Chrome瀏覽器
		chromeDriver = new ChromeDriver();
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章