Selenium中的隐式等待和显式等待

Selenium中,“等待”在执行测试中起着重要作用。在本文中,您将学习Selenium中“隐式”和“显式”等待的各个方面。

在本文中,您将学习 -

1. 为什么我们需要在selenium中等待?

2. 隐瞒等待

3. 明确等待

4. 流利的等待

为什么我们需要在Selenium中等待?

大多数Web应用程序都是使用Ajax和Javascript开发的。当浏览器加载页面时,我们想要与之交互的元素可能以不同的时间间隔加载。

它不仅难以识别元素,而且如果元素未定位,它将抛出“ ElementNotVisibleException ”异常。使用Waits,我们可以解决此问题。

让我们考虑一个场景,我们必须在测试中使用隐式和显式等待。假设隐式等待时间设置为20秒,显式等待时间设置为10秒。

假设我们试图找到一个具有一些“ExpectedConditions ”(显式等待)的元素,如果该元素不在显式等待(10秒)定义的时间范围内,它将使用由隐式等待定义的时间帧(在抛出“ ElementNotVisibleException ” 之前20秒)。

Selenium Web驱动程序等待

  1. 隐含的等待
  2. 明确等待

隐含的等待

Selenium Web Driver借用了Watir隐式等待的想法。

隐式等待将告诉Web驱动程序在它抛出“No Such Element Exception”之前等待一定的时间。默认设置为0.一旦我们设置了时间,Web驱动程序将在抛出异常之前等待该时间。

在下面的示例中,我们声明了一个隐含的等待,时间范围为10秒。这意味着如果元素在该时间范围内不位于网页上,则会引发异常。

声明隐式等待:

语法

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
package guru.test99;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class AppTest {
	
	protected WebDriver driver;
	@Test
	public void guru99tutorials() throws InterruptedException 
	{
	System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );
	driver = new ChromeDriver(); 
	driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
	String eTitle = "Demo Guru99 Page";
	String aTitle = "" ;
	// 启动Chrome并将其重定向到基本网址
	driver.get("http://demo.guru99.com/test/guru99home/" );
	//最大化浏览器窗口
	driver.manage().window().maximize() ;
	//获取标题的实际值
	aTitle = driver.getTitle();
	//将实际标题与预期标题进行比较
	if (aTitle.equals(eTitle))
	{
	System.out.println( "Test Passed") ;
	}
	else {
	System.out.println( "Test Failed" );
	}
	//关闭浏览器
	driver.close();
}
}

代码说明

在上面的例子中,

考虑以下代码:

driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;

隐式等待将接受2个参数,第一个参数将接受时间作为整数值,第二个参数将接受时间测量,包括SECONDS,MINUTES,MILISECOND,MICROSECONDS,NANOSECONDS,DAYS,HOURS等。

 

明确等待

显式等待用于告诉Web驱动程序在抛出“ ElementNotVisibleException ”异常之前等待某些条件(预期条件)或超过最大时间。

显式等待是一种智能的等待,但它只能应用于指定的元素。显式等待提供比隐式等待更好的选项,因为它将等待动态加载的Ajax元素。

一旦我们声明显式等待,我们必须使用“ ExpectedCondtions ”,或者我们可以配置我们想要使用Fluent Wait检查条件的频率。这些天在实现我们使用Thread.Sleep()时通常不建议使用

在下面的示例中,我们创建引用等待“ WebDriverWait ”类并使用“ WebDriver ”引用进行实例化,并且我们给出的最大时间帧为20秒。

句法:

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
package guru.test99;

import java.util.concurrent.TimeUnit;
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 org.testng.annotations.Test;

public class AppTest2 {
	protected WebDriver driver;
	@Test
	public void guru99tutorials() throws InterruptedException 
	{
	System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );
	driver = new ChromeDriver(); 
	WebDriverWait wait=new WebDriverWait(driver, 20);
	String eTitle = "Demo Guru99 Page";
	String aTitle = "" ;
	// 启动Chrome并将其重定向到Base URL
	driver.get("http://demo.guru99.com/test/guru99home/" );
	//最大化浏览器窗口
	driver.manage().window().maximize() ;
	//获取标题的实际值
	aTitle = driver.getTitle();
	//将实际标题与预期标题进行比较
	if (aTitle.contentEquals(eTitle))
	{
	System.out.println( "Test Passed") ;
	}
	else {
	System.out.println( "Test Failed" );
	}
	WebElement guru99seleniumlink;
	guru99seleniumlink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")));
	guru99seleniumlink.click();
	}
	
}

代码说明

考虑以下代码:

WebElement guru99seleniumlink;
guru99seleniumlink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(                   "/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i")));
 	guru99seleniumlink.click();

在上面的示例中,等待“ WebDriverWait ”类或“ ExpectedConditions ”中定义的时间量,以先发生者为准。

上面的Java代码声明我们正在等待网页上“ WebDriverWait ”类中定义的20秒时间帧的元素,直到满足“ ExpectedConditions ”并且条件为“ visibilityofElementLocated ”。

以下是可以在显式等待中使用的预期条件

·  alertIsPresent()

·  elementSelectionStateToBe()

·  elementToBeClickable()

·  elementToBeSelected()

·  frameToBeAvaliableAndSwitchToIt()

·  invisibilityOfTheElementLocated()

·  invisibilityOfElementWithText()

·  presenceOfAllElementsLocatedBy()

·  presenceOfElementLocated()

·  textToBePresentInElement()

·  textToBePresentInElementLocated()

·  textToBePresentInElementValue()

·  titleIs()

·  titleContains()

·  visibilityOf()

·  visibilityOfAllElements()

·  visibilityOfAllElementsLocatedBy()

·  visibilityOfElementLocated()

流利的等待

流畅的等待用于告诉Web驱动程序等待条件,以及在抛出“ElementNotVisibleException”异常之前我们想要检查条件的频率

频率:设置具有时间范围的重复循环,以定期验证/检查条件

让我们考虑一个场景,其中元素以不同的时间间隔加载。如果我们声明显式等待20秒,该元素可能会在10秒,20秒甚至更长时间内加载。在抛出异常之前它会等到指定的时间。在这种情况下,流畅的等待是理想的等待使用,因为这将尝试以不同的频率找到元素,直到它找到它或最终的计时器用完为止。

句法:

Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);
package guru.test99;

import org.testng.annotations.Test;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

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.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class AppTest3 {
	protected WebDriver driver;
	@Test
	public void guru99tutorials() throws InterruptedException 
	{
	System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );
	String eTitle = "Demo Guru99 Page";
	String aTitle = "" ;
	driver = new ChromeDriver();
	// 启动Chrome并将其重定向到Base URL 
	driver.get("http://demo.guru99.com/test/guru99home/" );
	//最大化浏览器窗口
	driver.manage().window().maximize() ;
	//获取标题的实际值
	aTitle = driver.getTitle();
	//将实际标题与预期标题进行比较
	if (aTitle.contentEquals(eTitle))
	{
	System.out.println( "Test Passed") ;
	}
	else {
	System.out.println( "Test Failed" );
		}
	
	Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)							
			.withTimeout(30, TimeUnit.SECONDS) 			
			.pollingEvery(5, TimeUnit.SECONDS) 			
			.ignoring(NoSuchElementException.class);
	WebElement clickseleniumlink = wait.until(new Function<Webdriver, WebElement>(){
	
		public WebElement apply(WebDriver driver ) {
			return driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"));
		}
	});
	//点击selenium链接
	clickseleniumlink.click();
	//关闭浏览器
	driver.close() ;
	}
}

代码说明

考虑以下代码:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)							
	.withTimeout(30, TimeUnit.SECONDS) 			
	.pollingEvery(5, TimeUnit.SECONDS) 			
	.ignoring(NoSuchElementException.class);

在上面的例子中,我们通过忽略“ NoSuchElementException ” 来声明一个流畅的等待,超时为30秒,频率设置为5秒。

考虑以下代码:

public WebElement apply(WebDriver driver ) {
	return 		driver.findElement(By.xpath("/html/body/div[1]/section/div[2]/div/div[1]/div/div[1]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/div/div/a/i"));
}	

我们创建了一个新功能来识别页面上的Web元素。(例如:这里的Web元素只不过是网页上的selenium链接)。

频率设置为5秒,最大时间设置为30秒。因此,这意味着它将每隔5秒检查网页上的元素,最长时间为30秒。如果元素位于此时间范围内,它将执行操作,否则将抛出“ ElementNotVisibleException ”

隐式等待与显式等待的区别

隐含的等待

明确等待

  • 隐式等待时间应用于脚本中的所有元素
  • 显式等待时间仅适用于我们想要的那些元素
  • 在隐等待,我们需要指定元素的“ExpectedConditions”被定位
  • 在显式等待中,我们需要在要定位的元素上指定“ExpectedConditions”
  • 建议在使用隐式等待中指定的时间帧定位元素时使用
  • 建议在元素花费很长时间加载时使用,也用于验证元素的属性,如(visibilityOfElementLocated,elementToBeClickable,elementToBeSelected)


结论:

隐式,显式流利等待selenium中使用的不同等待。这些等待的使用完全基于以不同时间间隔加载的元素。在测试我们的应用程序或构建我们的框架时,始终不建议使用Thread.Sleep()

如果你需要更多的selenium自动化的相关知识,欢迎继续关注我!当然你也可以加入我的群:175317069,一起进行学习交流,不仅仅只有自动化哦~

发布了46 篇原创文章 · 获赞 453 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/qq_41248484/article/details/81188298