Is there a way to wait to use wait until condition for two elements using Or condition

5,177 views
Skip to first unread message

Pavithra

unread,
Aug 31, 2012, 3:39:18 AM8/31/12
to webd...@googlegroups.com
I am trying to verify my signin is successful - for this, I am using waituntilvisible (elementlocatedby locator) condition.

Code:
WebElement inboxVisibility = new WebDriverWait(driver, 300).until(ExpectedConditions
                .visibilityOfElementLocated(driver.findElement(By.id("value")));

Depending upon the state of application, above element changes. Basically both elements are present in dom structure but visiblity appears based on the state.
So I wanted to wait for either X or Y element to be visible.
How can I achieve this?

Language: JAVA

Thanks,
Pavithra

Peter Gale

unread,
Aug 31, 2012, 3:43:02 AM8/31/12
to webd...@googlegroups.com
You'd have to write a loop with a time delay and test separately for the existence of either method as a means of indicating that your loop should end.

I don't think there is an ExpectedConditons method that will test for either of two elements being present.


Date: Fri, 31 Aug 2012 00:39:18 -0700
From: pav...@gmail.com
To: webd...@googlegroups.com
Subject: [webdriver] Is there a way to wait to use wait until condition for two elements using Or condition
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/SPLAzhV-VJYJ.
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.

pna...@gmail.com

unread,
Aug 31, 2012, 3:48:09 AM8/31/12
to webd...@googlegroups.com
Why can't you use the same code but instead ID you xpath to locate element based on the text of the element.

Sent from my BlackBerry® wireless device

From: Pavithra <pav...@gmail.com>
Date: Fri, 31 Aug 2012 00:39:18 -0700 (PDT)
Subject: [webdriver] Is there a way to wait to use wait until condition for two elements using Or condition

Krishnan Mahadevan

unread,
Aug 31, 2012, 3:49:40 AM8/31/12
to webd...@googlegroups.com
Pavithra,

Why not define your own ExpectedCondition implementation for this ?

public class MyCondition implements ExpectedCondition<Boolean>{

  private By locator1, locator2;
  public MyCondition(By locator1, By locator2){
 this.locator1 = locator1;
 this.locator2 = locator2;
  }

@Override
public Boolean apply(WebDriver input) {
return input.findElement(this.locator1).isDisplayed() || input.findElement(this.locator2).isDisplayed();
}  
}

Once you have such a class you can very well do :

By foo1 = By.name("name1");
By foo2 = By.id("id2");
new WebDriverWait(driver, 20).until(new MyCondition(foo1, foo2));

Would that help ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Pavithra

unread,
Aug 31, 2012, 5:34:30 AM8/31/12
to webd...@googlegroups.com
Thanks guys for the clue!!

I implemented below way based on above clue and it is working fine.

public static WebElement displayElement(WebDriver driver) throws Exception {
        if (driver.findElement(By.locator_x).isDisplayed()) {
            return
driver.findElement(By.locator_x);
        } else if (
driver.findElement(By.locator_y).isDisplayed()) {
            return
driver.findElement(By.locator_y);
        } else {
            return null;
        }
    }

WebElement checkElement = displayElement(driver);
        if (checkElement != null ) {
            WebElement inboxVisibility = new WebDriverWait(driver, 300).until(ExpectedConditions.visibilityOf(checkElement));
            MainTest.logger.info("Sign in succeeded");
        } else {
            Assert.fail("Signin not succeeded or application not loaded completely");
        }

Thanks
Pavithra
Reply all
Reply to author
Forward
0 new messages