Selenium WebDriver - Multi Windows Testing



Selenium Webdriver can be used to handle multi-windows testing. All windows launched are identified by a unique identifier for a session. As another window opens, the context of the driver continues on the parent window. To execute tasks on a child window, the context of the driver needs to be shifted from the parent to the child.

Basic Methods to Handle Multiple Windows in Selenium

There are various methods in Selenium that can be used to automate tests dealing with multiple windows. To work on the child windows, the driver context needs to be shifted from the parent window to the child windows.

Example 1

In the below page, click on New Tab.

Selenium Multi Windows 1

Post clicking the New Tab, we would move to another tab with text New Tab.

Selenium Multi Windows 2

Code Implementation

 package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import java.util.Set; import java.util.concurrent.TimeUnit; public class TabsHandling { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage with new tab driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php"); // click link then navigate to next tab WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]")); b.click(); // Get original window handle id String oW = driver.getWindowHandle(); // get every windows handle ids Set<String> windows = driver.getWindowHandles(); // loop through all window handles for (String w : windows) { if(!oW.equalsIgnoreCase(w)) { // switch to the child tab driver.switchTo().window(w); // get element in new tab WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1")); System.out.println("Text in new tab: " + e.getText()); break; } } // quit the browser driver.quit(); } } 

Output

 Text in new tab is: New Tab Process finished with exit code 0 

Here, we had obtained the text on the newly opened tab and got the message in the console - Text in new tab: New Tab.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 2

In the below page, click on New Window Message.

Selenium Multi Windows 3

Post clicking the New Window Message, we would move to another window with text New Window Message.

Selenium Multi Windows 4

Close the new window, shift back to the original window, and obtain the text - Browser Windows there. At last, quit the session.

Selenium Multi Windows 5

Code Implementation

 package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import java.util.Set; import java.util.concurrent.TimeUnit; public class WindowsOpen { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 20 secs driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Opening the webpage to open a new window driver.get("https://www.tutorialspoint.com/selenium/practice/browser-windows.php"); // click link to next window WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[3]")); b.click(); // Obtain original window handle id String oW = driver.getWindowHandle(); // obtain all opened windows handle ids Set<String> windows = driver.getWindowHandles(); // Loop through all window handles for (String w : windows) { if(!oW.equalsIgnoreCase(w)) { // switch to child window driver.switchTo().window(w); // get element in new window WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1")); System.out.println("Text in new window: " + e.getText()); driver.close(); break; } } // switch to parent window driver.switchTo().window(oW); // get element in parent window WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text in parent window: " + e1.getText()); // quit browser session driver.quit(); } } 

Output

 Text in new window: New Window Message Text in parent window: Browser Windows 

Here, we had captured text on the new window and got the message in the console - Text in new window: New Window Message. Then, we ended the child window and moved back to the parent window. At last, we had obtained text on the parent window in the console - Text in parent window: Browser Windows.

Thus there is a minor difference between close() and quit() methods. The close() method only closes the active browser window, and the quit() method terminates all the opened browser windows at the same time.

Example 3

From the version 4, we can open a new window using the below method −

 driver.switchTo().newWindow(WindowType.WINDOW) 

Open an application in a browser window, and obtain text - Check Box as shown in the below image −

Selenium Multi Windows 6

Then open another new window and launch a different application with text - Radio Button.

Selenium Multi Windows 7

Code Implementation

 package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.WindowType; import org.openqa.selenium.edge.EdgeDriver; import java.util.concurrent.TimeUnit; public class NewWindowOpen { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Open a webpage in first window driver.get("https://www.tutorialspoint.com/selenium/practice/check-box.php"); // get text in first window WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text: " + e.getText()); // Initiate the another Webdriver WebDriver newDriver = driver.switchTo().newWindow(WindowType.WINDOW); // Opening another webpage in second window driver.get("https://www.tutorialspoint.com/selenium/practice/radio-button.php"); // get text in second window WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1")); System.out.println("Text in new window: " + e1.getText()); // quit the browser session driver.quit(); } } 

Output

 Text: Check Box Text in new window: Radio Button 

Here, we had captured text in the first window with the message in the console - Text: Check Box. Then opened another new window to launch an application there. At last, we had obtained text in the new window with a message in the console - Text in new window: Radio Button.

Example 4

From the version 4, we can open a new tab using the below method −

 driver.switchTo().newWindow(WindowType.TAB) 

Open an application in a browser, and obtain text - Check Box as shown in the below image −

Selenium Multi Windows 8

Then open another new tab and launch a different application with text - Radio Button.

Selenium Multi Windows 9

Code Implementation

 package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.WindowType; import org.openqa.selenium.edge.EdgeDriver; import java.util.concurrent.TimeUnit; public class NewTabsOpen { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new EdgeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Open a webpage in first window driver.get("https://www.tutorialspoint.com/selenium/practice/check-box.php"); // get text in first window WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text: " + e.getText()); // Initiate the another Webdriver WebDriver newDriver = driver.switchTo().newWindow(WindowType.TAB); // Open a webpage in new tab driver.get("https://www.tutorialspoint.com/selenium/practice/radio-button.php"); // obtain text in other tab WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1")); System.out.println("Text in other tab: " + e1.getText()); // quit the browser session driver.quit(); } } 

Output

 Text: Check Box Text in other tab: Radio Button 

Here, we had captured the text in the first window with the message in the console - Text: Check Box. Then opened another new tab to launch an application there. Atlast, we obtained text in the new tab with the message in the console - Text in other tab: Radio Button.

Conclusion

This concludes our comprehensive take on the tutorial on Selenium Webdriver Multi Windows Testing. Weve started with describing basic methods to handle multiple windows in Selenium, and walked through examples on how to handle multiple windows with Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver - Multi Windows Testing. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements