I click a button from parent window and it loads a viewer in the new(child) window, then i do some action in the second window. Here before loading of my second window itself, the script tries to do some action and it fails. If i put Thred.sleep
it goes successfully. But i do not want to use thread.sleep. Is there any way we can do to wait for SECOND/CHILD window to load its page completely. Here my second window(browser) is a PDF kind of viewer. Below is the code i tried.
WebElement row = ele.findElement(By.cssSelector("tr[data-ri=\"0\"]"));
row.findElement(By.className("ui-selection-column")).click();
browser.findElement(By.id("frmResults:btnViewer")).click();
Thread.sleep(5000);
Set<String> AllWindowHandles = browser.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
scenario.write("Currently in Parent Window = "+ AllWindowHandles.toArray()[0]);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
String window2 = (String) AllWindowHandles.toArray()[1]; // out of bounds error thrown here
scenario.write("Switching to Child (Viewer) window = "+ AllWindowHandles.toArray()[1]);
browser.switchTo().window(window2);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
WebElement viewer = browser.findElement(By.id("outerDiv"));
assertThat(viewer.isDisplayed()).isTrue();
//browser.close();
scenario.write("Again Switching back to Parent window = "+ AllWindowHandles.toArray()[0]);
browser.switchTo().window(window1);
scenario.write(browser.getCurrentUrl());
scenario.write(browser.getTitle());
Updated
I get the error when i try to get the child window number on this line.
String window2 = (String) AllWindowHandles.toArray()[1];
Error:
java.lang.ArrayIndexOutOfBoundsException: 1
at Steps.Steps.verify_the_test_records_are_displayed_in_the_results_table_in_Search_Results_page(Steps.java:115)
at ?.Then Verify the test records are displayed in the results table in Search Results page(Test.feature:10)
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 30.552 s <<< FAILURE! - in Runner.TestRunner
[ERROR] feature(Runner.TestRunner) Time elapsed: 29.389 s <<< FAILURE!
cucumber.runtime.CucumberException: java.lang.ArrayIndexOutOfBoundsException: 1
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
Instead of thread.sleep()
, you can use the ExpectedConditions
class to add a targeted explicit wait on the number of expected windows :
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfWindowsToBe;
// we need the above using statements to use WebDriverWait and ExpectedConditions
// first wait for number of windows to be 2:
WebDriverWait wait = new WebDriverWait(browser, 10);
wait.until(ExpectedConditions.numberOfWindowsToBe(2)));
// switch to a new window
String window2 = (String) AllWindowHandles.toArray()[1];
scenario.write("Switching to Child (Viewer) window = "+ AllWindowHandles.toArray()[1]);
browser.switchTo().window(window2);
// wait on some element on the page to be fully loaded
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("outerDiv")));
ExpectedConditions
should wait on 2 windows to exist before calling String window2 = (String) AllWindowHandles.toArray()[1];
-- this should ensure browser.getWindowHandles()
returns 2
before attempting to switch to the second window.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.