Reputation: 15
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<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();
}
});
}
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?
Upvotes: 0
Views: 728
Reputation: 35
You can use return this
, it return the instance of the current class that is using the FluentWait and you can just ignore it.
Upvotes: 0
Reputation: 25744
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("foo"))).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("foo"), 30);
if (ele != null)
{
ele.click();
}
Upvotes: 1
Reputation: 808
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.
Upvotes: 1