Friday, September 13, 2019

How to Handle Multiple windows in Selenium | Selenium Automation

How to Handle Multiple windows in Selenium




In Automation scripting many times when we click link, pages opens in new window. Selenium by default will not navigate to new window so we need to write a code to switch to new window.





Selenium WebDriver software testing tool has built in "WebDriver.switchTo().window()"  method available to switch from one window to another window so it is very easy to handle multiple windows in webdriver


WebDriver.getWindowHandles()  In WebDriver software testing tool, We can use "WebDriver.getWindowHandles()" to get the handles of all opened windows by webdriver and then we can use that window handle to switch from from one window to another window. Example Syntax for getting window handles is as bellow.


Set<String> AllWindowHandles = driver.getWindowHandles();


WebDriver.switchTo().window()
WebDriver.switchTo().window() method is useful to switch from one window to another window of software web application. Example syntax is as bellow.

driver.switchTo().window(window2);

Bellow given webdriver example of switching window will explain you it deeply. Execute it in your eclipse and try to understand how webdriver do it.
package practise;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class HandleMultpleWindows {

         static WebDriver driver;

         public static void main(String args[]) throws InterruptedException {
                   System.setProperty("webdriver.chrome.driver", "‎⁨path");
                   driver = new ChromeDriver();
                   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                   driver.get("url");

                   driver.findElement(By.xpath("xpath of the new link")).click();

                   // Get and store both window handles in array
                   Set<String> AllWindowHandles = driver.getWindowHandles();
                   String window1 = (String) AllWindowHandles.toArray()[0];
                   System.out.print("window1 handle code = " + AllWindowHandles.toArray()[0]);
                   String window2 = (String) AllWindowHandles.toArray()[1];
                   System.out.print("\nwindow2 handle code = " + AllWindowHandles.toArray()[1]);

                   // Switch to window2(child window) and performing actions on it.
                   driver.switchTo().window(window2);

                   Thread.sleep(5000);

                   // Switch to window1(parent window) and performing actions on it.
                   driver.switchTo().window(window1);

                   Thread.sleep(5000);

                   // Once Again switch to window2(child window) and performing actions on it.
                   driver.switchTo().window(window2);

                   Thread.sleep(5000);
                   driver.close();

                   // Once Again switch to window1(parent window) and performing actions on it.
                   driver.switchTo().window(window1);

                   Thread.sleep(5000);

         }
}






No comments:

Post a Comment

If any suggestions or issue, please provide