Waiting for One of Two Things To Appear on a Page in Java Selenium

I’ve been working on a Java Selenium test automation project, teaching myself Java and Selenium WebDriver along the way.

I ran into a problem that I didn’t see solved on the Internet elsewhere, but my skill at searching StackOverflow is not that good.

The problem is conducting an operation that might yield one of two results, such as success or an error message. Say you’ve got an registration form or something that can succeed if you put the right data in it or will fail if you put the wrong data in it. If the user successfully registers, a welcome page displays. If the user fails, an error message displays. And, given this is the Web, it might take some time for one or the other to display. And the implicit waits didn’t look like they’d handle branching logic.

So here’s what I did:


public int waitForOne(WebDriver driver, Logger log,
                      String lookFor1, String lookFor2){
  WebDriverWait wait = new WebDriverWait(driver, 1);
	
   for (Integer iterator = 1; iterator < 60; iterator++){
     try{
         WebElement element = 
            wait.until(ExpectedConditions.elementToBeClickable(By.id(lookFor1)));
         return 1;
        }catch (Exception e){
				
      }  // try 1
      try{
          WebElement element = 
             wait.until(ExpectedConditions.elementToBeClickable(By.id(lookFor2)));
          return 2;
         }catch (Exception e){
      } //try 2
  } //for loop
}// waitForOne

You could even create a longer list of events to wait for one of them to occur by passing in an array of strings and then using a For-Each loop to run through the list.

This sample looks for a Web element by its ID, but you could change it to use another By parameter, such as CSS Selector (cssSelector). Or, if you're feeling dangerous, you could pass in the By parameter as a string and parse it in the method to determine whether to use ID, CSS Selector, or a mix therein. But that's outside the simplicity of this example.

Also note that the For loop that limits the waiting for a total of sixty iterations, which in this case will max out at 120 seconds (at 1 second per attempt for 2 items a maximum of 60 times). You could pass the max in as a parameter when calling this method if you want. That's especially important if you're using a list of possible elements to look for. If you're passing in five elements, suddenly you're at a maximum time of five minutes if it times out completely. You might not want your tests to wait that long, especially if you're using the check multiple times per test.

I'm sure there are more elegant solutions for this. Let's hear them. Because, frankly, I'm not very good at searching StackOverflow, and I'd prefer if you'd just correct my foolishness here in the comments.

One Response to “Waiting for One of Two Things To Appear on a Page in Java Selenium”

  1. Testing Bits – 1/18/15 – 1/24/15 | Testing Curator Blog Says:

    […] Waiting for One of Two Things To Appear on a Page in Java Selenium – The Director – http://qahatesyou.com/wordpress/2015/01/waiting-for-one-of-two-things-to-appear-on-a-page-in-java-se&#8230; […]


wordpress visitors