3

I am new to selenium. My application is very old application developed in 2004-2006 and every other action opens up a new window. I am using selenium web driver for internet explorer for writing the automated test cases.

I switch between opened windows using driver.switchTo().window(windowname);

however my test cases fail when my tests are run on other environments where the response time of my application differs from time to time. due to which sometimes my test cases pass and they fail other times. Currently i am using Thread.sleep(THRESHOLD) , and i know its bad practice. I am aware of WebDriverWait and the three waits Implicit, Explicit, Fluent. But my problem is none of them wait indefinitely till i get a response. Every wait needs a threshold value till which it will check if the page is ready and if the response is not within the threshold then it will fail. Is there anyway where i can dynamically wait for the response, where sometimes the response will be immediate and sometimes it will be delayed. I all cases i should not enter any threshold value, and test cases should wait for the response irrespective of whatever the response time might be.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Chetan
  • 4,735
  • 8
  • 48
  • 57

3 Answers3

4

As per your question how to wait until i get a ready state of a opened window it is worth to be mentioned that the AUT (Application Under Test) should have a benchmark on ideally how long it should take to open a new window/tab.

Now as @KDM indicated, the WebDriverWait constructors are:

  • WebDriverWait(WebDriver driver, java.time.Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut)
  • WebDriverWait(WebDriver driver, long timeOutInSeconds)
  • WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis)

In all the three constructors the timeOutInSeconds is of long type which have a MAX value of 9223372036854775807 which you can use too.


Long.MAX_VALUE

In the following example the WebDriver instance i.e. driver is successfull casted with the MAX_VALUE value of long type:

  • Code Block:

    System.out.println("Long.MAX = " + Long.MAX_VALUE);
    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    WebDriverWait wait_me = new WebDriverWait(driver, 9223372036854775807L);
    System.out.println("Application Opened");
    
  • Console Output:

    Long.MAX = 9223372036854775807
    Application Opened
    

numberOfWindowsToBe()

Finally, when you use the tailor-made Explicit Wait to access a newly opened window/tab always use the numberOfWindowsToBe() method from ExpectedConditions Class and then collect the windowHandles as follows:

  • Sample Code Block (Java):

    driver.get("http://www.google.com");
    System.out.println("Page Title is : "+driver.getTitle());
    String parent_window = driver.getWindowHandle();
    ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
    new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
    Set<String> allWindows_1 = driver.getWindowHandles();
    //Now you can initiate switching through windowHandles
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

This code waits till the particular id is visible. And is initialized with 30 seconds before it throws an exception:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ifmail")));
AnkDasCo
  • 1,439
  • 11
  • 16
0

you can use below code snippet :

 public boolean softWaitForPageToLoad() {
    try {
        new WebDriverWait(getDriver(), 60).until(
                webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").toString().matches("interactive|complete"));
        return true;
    } catch (Exception e) {
        return false;
    }
}

Here getDriver() function returns existing driver instance. Please replace it with your implementation.

Abhishek_Mishra
  • 4,551
  • 4
  • 25
  • 38