0

Is there an api for waiting for a window to load before switching to it or the presence of a window title in the new window? We have ExpectedConditions.frameToBeAvailableAndSwitchToIt for a frame to be available, however, would make life easier if we had an equivalent one for a window as well. Any response would be much appreciated. Thanks.

cooler8020002000
  • 39
  • 1
  • 1
  • 4

2 Answers2

2

I use this approach:

/**
 * Wait for an emergent windows to be present.
 */
public final void waitForWindowsPresent() {
    WebDriverWait wait = new WebDriverWait(getDriverProvider().get(),
            TIMEOUT);
    wait.until(new Predicate<WebDriver>() {
        public boolean apply(final WebDriver d) {
            return getDriverProvider().get().getWindowHandles().size() > 1;
        }
    });
}

I check if the number of windowHandles is greater than 1, but if you are expecting to open more windows you could save the state of windowHandles and then compare against that.

Alejandro
  • 21
  • 2
1

I posted something similar here:

Wait For Window By URL with Timeout Value

The code is in groovy, but groovy is pretty readable so redoing it in another language should be easy. I used it extensively with FF, IE, & Chrome.

The usage was something like:

def url = "/page/ContactUs"
if (!selectWindow(serverBaseUrl + url)) {Log.Error("Cannot get to Contact Us")}

And if you need to wait on something on a page:

public boolean waitOnXPathDisplayed(String xpath) {
    int tries = 0;
    while (true) {
        try {
            Log.logger.info("Trying to find element " + xpath + ", try = " + tries)
            WebElement el = driver.findElement(By.xpath(xpath))
            if(el.isDisplayed()) {
                Log.logger.info("Found element")
                return true
            } else {
                Log.logger.info("Element not displayed yet")
                tries++
                Thread.sleep(1000)
            }
            if (tries > 20) {
                Log.logger.error("Timeout")
                return false
            }
        } catch (Exception ex) {
            Log.logger.info("Exception while trying to find element")
            tries++
            Thread.sleep(1000)
            if (tries > 20) {
                Log.logger.error("Timeout")
                return false
            }
        }
    }
}

With a usage like:

waitOnXPathDisplayed("//div[@id='some div id']/ul[1]/li[3]")
Community
  • 1
  • 1
chrismead
  • 2,163
  • 3
  • 24
  • 36
  • Hi Chris, Thanks for your reply. The problem that I can see with the waitOnXPathDisplayed is that the usage of Thread.sleep leads to other type of problems while running the tests. – cooler8020002000 Jun 06 '12 at 22:48
  • I was using Thread.Sleep to give the browser time to continue loading. You might want to give it a try without sleeping, but I'd expect that the test would interfere with the browser. I even use this type of construct in windows app automation where i could use an event handler, but all the extra code for each registration, callback, etc. makes it prohibitive. – chrismead Jun 07 '12 at 01:57