我想使用Fluent等待返回void,怎么做?

huangapple go评论65阅读模式
英文:

I want to return void using Fluent wait, how to do that?

问题

我正在使用 Fluent Wait我想要返回 void而不是 Web 元素或布尔值我该如何做呢我已经尝试过像这样的方式以下是代码片段

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(30))
                .pollingEvery(Duration.ofSeconds(3)).ignoring(NoSuchElementException.class);

wait.until(new Function<WebDriver, Void>() {
        public void apply(WebDriver driver) {
            if (driver.findElement(By.id("foo")).isDisplayed())
                driver.findElement(By.id("foo")).click();
        }
    });
}
然而void 处它给我报错
>The return type is incompatible with Function<WebDriver,Void>.apply(WebDriver)

**注意**我只想返回 void这可能吗
英文:

I am using Fluent wait. I want to return void instead of Web Element or Boolean. How can I do it? I have tried like this. Code snippet is below

Wait&lt;WebDriver&gt; wait = new FluentWait&lt;WebDriver&gt;(driver).withTimeout(Duration.ofSeconds(30))
				.pollingEvery(Duration.ofSeconds(3)).ignoring(NoSuchElementException.class)

	wait.until(new Function&lt;WebDriver, Void&gt;() {
			public void apply(WebDriver driver) {
            if(	driver.findElement(By.id(&quot;foo&quot;)).isDisplayed())
				driver.findElement(By.id(&quot;foo&quot;)).click();
			}
		});
	}

However, it gives me error at void:
>The return type is incompatible with Function<WebDriver,Void>.apply(WebDriver)

NOTE: I only want return void. Is that possible?

答案1

得分: 1

答案是否定的,我认为。


我们可以从api文档中找到一些有用的信息。

它解释了Returns,如下所示:
> 如果函数在超时之前返回的内容与null或false不同,则返回函数的返回值。

因此,函数不可能同时正确处理void和null。

英文:

The answer is no, I think.


We can find some useful info from api docs.

It explains the Returns like this:
> The function's return value if the function returned something different from null or false before the timeout expired.

So, it is impossible for the function to handle void and null correctly at the same time.

答案2

得分: 1

我认为你误解了wait函数的用法。它应该在条件满足时返回一个布尔值或WebElement。你试图在方法内部进行点击操作,这不是它预期的用法。

在这里,你其实并不需要FluentWait。你可以使用WebDriverWait来简化操作。

new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.id("foo"))).click();

这会等待最多30秒,直到元素可点击为止,如果没有超时,将会点击返回的元素。


关于你的评论... 在这种情况下,我会编写一个单独的方法ElementExists,返回一个WebElement。如果返回的WebElement不为null,就对它进行点击操作。

辅助方法

public static WebElement ElementExists(By locator, int timeout)
{
    try
    {
        return new WebDriverWait(driver, timeout).until(ExpectedConditions.visibilityOfElementLocated(locator));
    } catch (TimeoutException e)
    {
        return null;
    }
}

脚本代码

WebElement ele = ElementExists(By.id("foo"), 30);
if (ele != null)
{
    ele.click();
}
英文:

I think you have misunderstood the use of the wait Function. It's supposed to return a Boolean or WebElement once a condition is met. You are trying to click inside of the method which is not the way it's intended to be used.

You don't really need FluentWait here. You can use a WebDriverWait to make this much simpler.

new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.id(&quot;foo&quot;))).click();

This will wait for up to 30s for the element to be clickable and if it doesn't time out, the returned element will be clicked.


To your comment... in that case, I would write a separate method ElementExists that returns a WebElement. If the returned WebElement is not null, click it.

Helper method

public static WebElement ElementExists(By locator, int timeout)
{
    try
    {
        return new WebDriverWait(driver, timeout).until(ExpectedConditions.visibilityOfElementLocated(locator));
    } catch (TimeoutException e)
    {
        return null;
    }
}

Script code

WebElement ele = ElementExists(By.id(&quot;foo&quot;), 30);
if (ele != null)
{
    ele.click();
}

答案3

得分: 0

你可以使用 return this,它会返回正在使用 FluentWait 的当前类的实例,你可以忽略它。

英文:

You can use return this, it return the instance of the current class that is using the FluentWait and you can just ignore it.

huangapple
  • 本文由 发表于 2020年10月10日 07:59:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64288683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定