完整解析:Selenium中的隐式等待和显式等待

发表于:2021-10-15 09:30

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:厌倦    来源:知乎

  在Selenium中,“等待”在执行测试中起着重要作用。在本文中,您将学习Selenium中“隐式”和“显式”等待的各个方面。
  为什么我们需要在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 ”
  隐式等待与显式等待的区别
  结论:
  隐式,显式和流利等待是selenium中使用的不同等待。这些等待的使用完全基于以不同时间间隔加载的元素。在测试我们的应用程序或构建我们的框架时,始终不建议使用Thread.Sleep()。

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号