0

I am trying to implement a method 'waitForNewWindow' in Java using selenium WebDriver. This method is all about waiting to check if a new window is opened. If a new window is opened in the specified time, i need to return true, else return false.

public boolean waitForNewWindow(String target) {

    try {

        Thread.sleep(30000);
        if(driver.switchTo().window(target)!=null) {
            log.info("New window is opened");
            return true;
        }


    }catch(Exception e) {
        log.debug(e);
        return false;
    }
    return true;
}

But here, I don't want to use thread.sleep(time). The waiting time needs to be specified as below:

WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);

Moreover, in the above code, the control is switching to the new window, which is not expected. Can someone please provide your answers on how to implement my requirement?

0

3 Answers 3

1

the below mentioned code checks for the number of windows to appear with time out

public void waitForNumberOfWindows(final int length){
    new WebDriverWait(driver, 30) {
    }.until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driver) {
            return driver.getWindowHandle().length()==length;
        }
    });
}

it will check for the expected number of windows to be present at that instance and will return true if the count matches with in the specified timeout(30 in above code)

Sign up to request clarification or add additional context in comments.

Comments

0

You can not specify a timeout like you want. You have to use Thread.sleep().

Regarding your control moving to new window because of your below line the control is moving to new tab

driver.switchTo().window(target)

If you want to simply check if there is two windows open or not, you can write something like below

while( driver.getWindowHandle().length() != 2){
    Thread.sleep(2000);
}

Comments

0

Finally got the implementation of waitForNewWindow method, using the WebDriverWait object as below:

   try {
    ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
      public Boolean apply(WebDriver wd) {
        if (wd.switchTo().window(newTarget) != null) {
            log.info("New window is opened with the window id : "
                            + newTarget);
            driver.switchTo().window(parentHandle);
            return true;
        } else {
            return false;
        }

      }
    };
    WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);

       if (wait.until(e)) {
        log.info("the wait for the expected condition is successful");
                    return true;
       }

   } catch (Exception e1) {
    log.debug(e1);
    return false;
   }

Tested the same and its working fine.

3 Comments

Just a thought instead of taking the control of the driver to a new tab and then check null on it can you do driver.getWindowHandle().length() > 1. If you can that will give you some good throughput.
I tried using the driver.getWindowHandle().length(), but not sure why it was returning the value as '38'. For the statement 'log.info("Value of current num of windows is : " + wd.getWindowHandle().length());', i was getting the result as 'Value of current num of windows is : 38'.
Sorry it will be : driver.getWindowHandles().size(). It should return two if you have two windows. Can you please give it a try?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.