Reputation: 11
When alert pop up opens a new window, executions continues and doesn't wait for the new window fully load. Irony is code execution passes the test which it shouldn't as many web elements are still left to executed. I am baffled. I tried implicitly wait, explicitly wait, fluent wait ... Only working one is Thread.sleep which is not an efficient way as it should be dynamic wait and thread.sleep will slow the test in selenium here. I believe tests passes because rest of the code lies in while statement. Been learning coding for almost 3 year myself without a degree. Please don't discourage as in my 40's I have found something that keeps me up all night and I still don't feel it.
SOMEONE MENTIONED """""Before opening the popup window get the handle of main window and save it.
String Parent=driver.getWindowHandle();
DIDN'T WORK FOR ME.
Thread,sleep
is the one which works...(not efficient way)...
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
DINDN'T WORK
--Explicit wait with many expected conditions didn't work... //code....BaseClass (Using testNG)
System.setProperty("webdriver.chrome.driver", "Drivers/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.usda.gov/nutrition-security");
package testNGpckg;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
public class WindowHandles extends BaseClass {
@Test
public void handleWindow() throws InterruptedException {
// System.out.println(driver.getTitle());
String parent = driver.getWindowHandle(); // getting parent window as a string...
Set <String> setOfWindows = driver.getWindowHandles(); // getting all other windows
Iterator <String> iterating = setOfWindows.iterator();//Now iterate using iterator
driver.findElement(By.xpath("/html/body/div/footer/div[2]/div[2]/div/div/div/div/a")).click();
driver.switchTo().alert().accept();// alert handling here
//Thread.sleep(4000); //this need to be replaced with implicit wait i think
//The new window needs to be opened before the code below should run
while (iterating.hasNext() ) {
String child = iterating.next();
if (!parent.equalsIgnoreCase(child)) {
driver.switchTo().window(child); //switching to child window
System.out.println(driver.getTitle()+ " (This is the Title of child window)");
driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/form/div/div[2]/div[2]/fieldset/div[3]/label")).click();
WebElement email = driver.findElement(By.xpath("//*[@id=\"inputProp0\"]"));
email.sendKeys("[email protected]");
driver.findElement(By.xpath("//*[@id=\"update-profile-submit-btn\"]")).click();
System.out.println("\n" + driver.findElement(By.xpath("//*[@id=\"optinSuccess\"]")).getText());
// switching back to main window
System.out.println(" \n LETS TRY GOING BACK TO MAIN WINDOW AND GET TITLE AGAIN. \n ");
driver.switchTo().window(parent);
System.out.println(driver.getTitle() +" (We are back to main window and this is the Title of main window)");
}
}
}
}
Upvotes: 0
Views: 128
Reputation: 11
Finally found the answer in some other stack overflow post...Declaring WebDriverWait and then waiting for both windows to open before execution worked.
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.numberOfWindowsToBe(2));//here 2 represents the current window and the new window to be opened
Upvotes: 1