selenium之ExpectedConditions类

API中对于该类的介绍:Canned ExpectedConditions which are generally useful within webdriver tests。很笼统,大概意思就是在webdriver的测试中会有用,那到底有什么用呢,下面我们就来一探究竟。

该类没有构造函数,所有的方法都是静态的,所以我们可以直接用类名调用,我只介绍里面几个方法,其它方法的用法都类似,具体查api

 

  • 1.alertIsPresent()判断alert弹框出现了没,返回值是ExpectedCondition<Alert>,我们可以这样用
  • 复制代码
        public void testalertIsPresent() {
            
            dr.findElement(By.id("alert")).findElement(By.className("alert")).click();
            ExpectedCondition<Alert> e = ExpectedConditions.alertIsPresent();
            Alert alert = e.apply(dr);
            alert.accept();
        }
    复制代码

    也可以这样用

    复制代码
        public void testalertIsPresentW() {
            WebDriverWait wait = new WebDriverWait(dr,10);
            dr.findElement(By.id("alert")).findElement(By.className("alert")).click();
            ExpectedCondition<Alert> e = ExpectedConditions.alertIsPresent();
            Alert alert = wait.until(e);
            alert.accept();
        }
    复制代码

    apply用了com.google.common.base.Function的方法,而until用了FluentWait的方法。那么这两者有和区别呢,通过下面这个例子介绍,

  • 2. textToBePresentInElementLocated():文本是否出现在所定位的元素中
  • 复制代码
        public void testtextToBePresentInElementLocated() {
            WebDriverWait wait = new WebDriverWait(dr,10);
            dr.findElement(By.className("wait")).click();
            ExpectedCondition<Boolean> e1 = ExpectedConditions.textToBePresentInElementLocated(By.className("red"),"wait for display");
            System.out.println(e1.apply(dr));//false
    //System.out.println(wait.until(e1)); //报true }
    复制代码

    apply不会去等待所找元素是否存在,直接false报错no such elment。而until会在等待10秒,如果出现则返回true,否则超时。

    • 3.visibilityOfElementLocated(),该方法是判断定位的元素是否存在,使用的方法时(),apply也会去等待

      复制代码
      public void testvisibilityOfElementLocated() {
              WebDriverWait wait = new WebDriverWait(dr,10);
              dr.findElement(By.className("wait")).click();
              ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOfElementLocated(By.className("red"));
              
              System.out.println(wait.until(e1).getText());    //wait for display
              System.out.println(e1.apply(dr).getText());    //wait for display
              
          }
      复制代码

       

    • 4.visibilityOf(),判断元素是否可见

      复制代码
          public void testvisibilityOf() {
              WebDriverWait wait = new WebDriverWait(dr,10);
              ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOf(dr.findElement(By.className("wait")));
              
              System.out.println(e1.apply(dr).getAttribute("value"));    //wait
              //System.out.println(wait.until(e1).getAttribute("value"));//wait
          }
      复制代码

5.visibilityOfElementLocated(),定位的元素是否可见

复制代码
    public void testvisibilityOfElementLocated() {
        WebDriverWait wait = new WebDriverWait(dr,10);
        dr.findElement(By.className("wait")).click();
        ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOfElementLocated(By.className("red"));
        
        System.out.println(wait.until(e1).getText());    //wait for display
        System.out.println(e1.apply(dr).getText());    //wait for display
        
    }
复制代码

以上列举了TestExpectedConditions的用法,鉴于apply会存在失败的情况,建议使用until。

 

PS:小插曲

下面的例子是api中关于FluentWait的用法

复制代码
Sample usage:


   // Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait&lt;WebDriver&gt; wait = new FluentWait&lt;WebDriver&gt;(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function&lt;WebDriver, WebElement&gt;() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });
复制代码

详细用法:

复制代码
    @Test(enabled=false)
    public void testuntilWebElement() {
        WebDriverWait wait = new WebDriverWait(dr,10);
        WebElement we = wait.until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return arg0.findElement(By.id("user"));
            }}) ;
        we.sendKeys("test");
    }
    
    @Test(enabled=false)
    public void testuntilBoolean() {
        WebDriverWait wait = new WebDriverWait(dr,10);
        boolean we = wait.until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return arg0.findElement(By.id("user"));
            }}).isDisplayed();
        System.out.println(we);
    }
复制代码


两者用法目的差不多,可以根据具体情况选择使用哪种方式。

 文中所用到的html例子:

 http://pan.baidu.com/s/1i3jjo3b

posted on   乔叶叶  阅读(5910)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示