Thursday, 14 January 2016

How to handle model popups or multiple windows handling

package Other;

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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Modelpopuporseparatewindow {
    WebDriver driver;
    @BeforeMethod
    public void init()
    {
        driver=new FirefoxDriver();
        //System.setProperty("webdriver.chrome.driver","D:\\Pravanjan\\ppr_lib_jars\\chromedriver.exe");
        //driver=new ChromeDriver();
    }
    @AfterMethod
    public void stop()
    {
        driver.quit();
    }
    @Test       
   
        public void testMultiwindow() throws InterruptedException
        {
            driver.get("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm");
            driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
             String parent = driver.getWindowHandle();
                WebDriverWait wait = new WebDriverWait(driver, 10);
                WebElement push_to_create = wait.until(ExpectedConditions
                        .elementToBeClickable(By
                                .cssSelector("input[value='Push To Create']")));
                push_to_create.click();
                waitForWindow(driver);
                switchToModalDialog(driver, parent);

            }

            public static void waitForWindow(WebDriver driver)
                    throws InterruptedException {
                //wait until number of window handles become 2 or until 6 seconds are completed.
                int timecount = 1;
                do {
                    driver.getWindowHandles();
                    Thread.sleep(200);
                    timecount++;
                    if (timecount > 30) {
                        break;
                    }
                } while (driver.getWindowHandles().size() != 2);

            }

            public static void switchToModalDialog(WebDriver driver, String parent) {
                    //Switch to Modal dialog
                if (driver.getWindowHandles().size() == 2) {
                    for (String window : driver.getWindowHandles()) {
                        if (!window.equals(parent)) {
                            driver.switchTo().window(window);
                            System.out.println("Modal dialog found");
                            break;
                        }
                    }
                }
            }
}
           





    //share|improve this ans
            /*String mainWinHander = d.getWindowHandle();

            // code for clicking button to open new window is ommited

            //Now the window opened. So here reture the handle with size = 2
            Set<String> handles = d.getWindowHandles();

            for(String handle : handles)
            {
                if(!mainWinHander.equals(handle))
                {
                    // Here will block for ever. No exception and timeout!
                    WebDriver popup = d.switchTo().window(handle);
                    // do something with popup
                   // popup.getTitle();
                    popup.close();
                }
            }
        }
}*/

No comments:

Post a Comment