51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1527|回复: 1
打印 上一主题 下一主题

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

[复制链接]
  • TA的每日心情
    无聊
    4 天前
  • 签到天数: 941 天

    连续签到: 3 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-10-21 12:04:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
     在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秒。这意味着如果元素在该时间范围内不位于网页上,则会引发异常。
      声明隐式等待:
      语法:
    1.  driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
    2.   package guru.test99;
    3.   import java.util.concurrent.TimeUnit;
    4.   import org.openqa.selenium.By;
    5.   import org.openqa.selenium.WebDriver;
    6.   import org.openqa.selenium.chrome.ChromeDriver;
    7.   import org.testng.annotations.Test;
    8.   public class AppTest {
    9.   
    10.   protected WebDriver driver;
    11.   @Test
    12.   public void guru99tutorials() throws InterruptedException
    13.   {
    14.   System.setProperty ("webdriver.chrome.driver",".\\chromedriver.exe" );
    15.   driver = new ChromeDriver();
    16.   driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
    17.   String eTitle = "Demo Guru99 Page";
    18.   String aTitle = "" ;
    19.   // 启动Chrome并将其重定向到基本网址
    20.   driver.get("http://demo.guru99.com/test/guru99home/" );
    21.   //最大化浏览器窗口
    22.   driver.manage().window().maximize() ;
    23.   //获取标题的实际值
    24.   aTitle = driver.getTitle();
    25.   //将实际标题与预期标题进行比较
    26.   if (aTitle.equals(eTitle))
    27.   {
    28.   System.out.println( "Test Passed") ;
    29.   }
    30.   else {
    31.   System.out.println( "Test Failed" );
    32.   }
    33.   //关闭浏览器
    34.   driver.close();
    35.   }
    36.   }
    复制代码
    代码说明
      在上面的例子中,考虑以下代码:
    隐式等待将接受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秒。
      考虑以下代码:
    1. public WebElement apply(WebDriver driver ) {
    2.   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"));
    3.   }
    复制代码
     我们创建了一个新功能来识别页面上的Web元素。(例如:这里的Web元素只不过是网页上的selenium链接)。
      频率设置为5秒,最大时间设置为30秒。因此,这意味着它将每隔5秒检查网页上的元素,最长时间为30秒。如果元素位于此时间范围内,它将执行操作,否则将抛出“ ElementNotVisibleException ”
      隐式等待与显式等待的区别

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








    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏1
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-4 21:43 , Processed in 0.068283 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表