Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a fluentwait wait for two conditions

Tags:

java

selenium

In the Java Selenium Framework, is it possible for a FluentWait to check for two different expected conditions? Where it could be either one or the other.

To give more context, I need to check a website's title, but depending on the circumstances it could redirect to one of two sites (imagine you fail to log in and are sent to a log in page, but if you do log in you're redirected to the homepage).

Would an Implicit Wait be better for this case? Because the site seems to load rather slowly a lot of times and I'm forced to continuously increase the wait time on it.

like image 781
Danyx Avatar asked Apr 12 '16 17:04

Danyx


2 Answers

You may manipulate ExpectedConditions to form a suitable complex condition. There're or(...) and urlContains(...) methods useful for your needs. Something like this:

WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.or(
    ExpectedConditions.urlContains("http://first.si.te/url"),
    ExpectedConditions.urlContains("http://second.si.te/url")
));
like image 167
user3159253 Avatar answered Oct 22 '22 09:10

user3159253


You can create you own waiting condition with a lambda expression. This example waits until the title starts with "abcd" or contains "1234":

WebDriverWait wait = new WebDriverWait(driver, 20);

String page_title = wait.until((WebDriver wd)->{
    String title = wd.getTitle();
    return title.startsWith("abcd") || title.contains("1234") ? title : null;
});

System.out.println("Page title is: " + page_title); 
like image 38
Florent B. Avatar answered Oct 22 '22 07:10

Florent B.