WebDriverWait wait.until(Function, isFalse) possible?

3,695 views
Skip to first unread message

confusa

unread,
Feb 8, 2011, 5:42:25 PM2/8/11
to webdriver
In selenium I used a lot of waiting until an element had left the page
until I moved on, essentially the reverse of waitForElementPresent. In
Webdriver I see we now have a handy WebDriverWait class, but I only
have the ability of doing a wait.(Function, "isTrue"), the isTrue is
implied. How can I get an isFalse in there? Make sense?

JohnMc

unread,
Feb 8, 2011, 6:37:03 PM2/8/11
to webdriver
When you implement the until() method just make it return true when
your condition is met.

so tweeking the example from the Wait JavaDoc page (See it here
http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html):

new Wait("Waiting for element to go away") {
boolean until() {
RenderedWebElement element =
driver.findElement(By.id("myID"));
return !element.isDisplayed;
}
};

Just make it return true when the element is not present.

Simon Stewart

unread,
Feb 9, 2011, 5:15:19 AM2/9/11
to webd...@googlegroups.com
The simplest thing to do is to write a "not" function, that inverts
the result of the Function.

Simon

> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.
>
>

confusa

unread,
Feb 9, 2011, 12:20:56 PM2/9/11
to webdriver
I thought about that as the cleanest solution, but couldn't quite
figure it out. The way I constructed a wait is like so:

protected WebElement waitForElementIdPresent(String id) {
WebDriverWait wait = new WebDriverWait(driver, 6000);
WebElement element =
wait.until(presenceOfElementLocated(By.id(id)));
return element;
}

protected Function<WebDriver, WebElement>
presenceOfElementLocated(final By locator) {
return new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
};
}

Not sure how to invert that function I tried this but no joy:

WebElement element =
wait.until(!presenceOfElementLocated(By.id(id)));

Simon Stewart

unread,
Feb 9, 2011, 5:30:17 PM2/9/11
to webd...@googlegroups.com
The "not" exepected condition would look something like:

public ExpectedCondition<Boolean> not(
final ExpectedCondition<?> toInvert) {
return new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
try {
Object result = toInvert.apply(driver);
return (result == null || result == Boolean.FALSE);
} catch (Exception e) {
// You may want to tweak this bit
return true;
}
}
};
}


Simon

Reply all
Reply to author
Forward
0 new messages