Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Fluent wait vs WebDriver wait_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java Fluent wait vs WebDriver wait

Java Fluent wait vs WebDriver wait,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我把FluentWait和WebDriverWait搞混了 FluentWait和WebDriverwait都使用相同的功能,如忽略异常、更改轮询时间间隔、预期条件等 据我所知,两者都实现了Wait接口。此外,WebDriverWait扩展了FluentWait(这意味着所有的功能也出现在WebDriverWait中) 什么是WebDriverWait中没有的额外功能FluentWait?FluentWait和WebDriverWait都是Wait接口的实现 使用Fluent WebDriver

我把
FluentWait
WebDriverWait
搞混了

FluentWait
WebDriverwait
都使用相同的功能,如忽略异常、更改轮询时间间隔、预期条件等

据我所知,两者都实现了
Wait
接口。此外,
WebDriverWait
扩展了
FluentWait
(这意味着所有的功能也出现在
WebDriverWait
中)


什么是
WebDriverWait
中没有的额外功能
FluentWait

FluentWait和WebDriverWait都是Wait接口的实现

使用Fluent WebDriver显式等待和WebDriver显式等待的目标大致相同。然而,在少数情况下,FluentWait可以更加灵活。由于这两个类都是同一Wait接口的实现,因此它们或多或少都具有相同的功能,除了之外,FluentWait有一个功能,可以接受谓词或函数作为until方法中的参数。另一方面,WebDriverWait只接受函数作为in-until方法,该方法限制您仅使用布尔返回。当您在FluentWait中使用谓词时,它允许您从until方法返回任何对象

public void exampleOfFluentWait() {
            WebElement foo = driver.findElement(By.id("foo"));
            new FluentWait<WebElement>(foo)
                .withTimeout(10, TimeUnit.SECONDS)
                .pollingEvery(2, TimeUnit.SECONDS)
                        .until(new Function<WebElement, Boolean>() {
                            @Override
                            public Boolean apply(WebElement element) {
                                return element.getText().contains("foo");
                            }
                        });
        }
仔细看这里:

示例: 一种FluentWait,其函数在until中作为参数,返回字符串:

public void exampleOfFluentWait() {
        WebElement foo = driver.findElement(By.id("foo"));
        new FluentWait<WebElement>(foo)
            .withTimeout(10, TimeUnit.SECONDS)
            .pollingEvery(2, TimeUnit.SECONDS)
                    .until(new Function<WebElement, String>() {
                        @Override
                        public String apply(WebElement element) {
                            return element.getText();
                        }
                    });
    }

实际上,两者之间几乎没有什么区别。根据
WebDriverWait
源代码,它说:

它将忽略遇到的
NotFoundException
实例 (抛出)默认情况下处于<代码>状态,直到<代码>状态,并立即 传播所有其他信息。您可以通过以下方式向忽略列表添加更多内容: 调用
忽略(要添加的异常)


唯一的区别是默认情况下,在
WebDriverWait
中忽略元素未找到异常。其他功能与
FluentWait
完全相同,因为
WebDriverWait
扩展了它。

主要区别在于,在Webdriver wait中,我们无法执行等待池场景,在Fluent wait中,我们可以设置Webdriver wait中无法实现的池时间

Webdriver等待示例

WebElement dynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("dynamicElement")));
流畅等待示例 下面的代码是等待30秒,让一个元素出现在页面上,每5秒检查一次它的存在

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

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
    {   
      public WebElement apply(WebDriver driver) {
      driver.findElement(By.id("foo"));    
    }
    });
Wait Wait=new FluentWait(驱动程序)
.带超时(30秒)
.每(5秒)轮询一次
.忽略(NoSuchElementException.class);
WebElement foo=wait.until(新函数()
{   
公共WebElement应用(WebDriver){
驱动程序文件(按id(“foo”));
}
});

在FluentWait中,轮询周期是受控的,而在显式等待中,轮询周期是250毫秒


用户还可以灵活地使用IgnoreExceptionTypes命令忽略轮询期间可能发生的异常。

您也可以在WebDriverWait中使用和定义轮询时间。@Deepakkarkarhave更改了我的答案。@PriyanshuShekhar我对您的答案有点怀疑,我认为谓词不支持
apply()
method,应该是
test()
method。其次,谓词只返回布尔值,但您提到它将返回任何对象。你能澄清一下吗?
WebElement dynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("dynamicElement")));
 Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(30, SECONDS)
        .pollingEvery(5, SECONDS)
        .ignoring(NoSuchElementException.class);

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
    {   
      public WebElement apply(WebDriver driver) {
      driver.findElement(By.id("foo"));    
    }
    });