Saturday, November 24, 2018

How To Handle Multiple Windows Using Selenium WebDriver | Multiple Browser Windows In Selenium

In this tutorial, I am going to discuss how to handle multiple windows using Selenium WebDriver.




Topics to be covered - 
  • Multiple windows concept.
  • Handle multiple windows using Selenium WebDriver.


Handle Multiple Windows In Selenium






Let's take the example of Login page of Facebook Application.






There is one link 'Cookie Policy' as highlighted in the above image. 

If we click on the link, what will happen?

It will open in the new window. That's mean we have two opened windows now.

This is the concept of multiple windows. 

Now the question is, how to go to that second window and perform actions as required while automating the script?

Solution - We need to switch to that second window like we do while handling alert/frames and then we can perform required actions.


There are two methods in Selenium WebDriver to handle multiple windows - 
  • driver.getWindowHandle() - It returns the control of current opened window.
          Return type - String

  • driver.getWindowHandles() - It returns the control of all the opened windows so that we can iterate over the all the windows.
           Return Type - Set<String>


Selenium WebDriver provides one command in order to switch to the expected window -  switchTo().window("window_name");


Scenario - 
  1. Open the Login Page of the Facebook application (Main Window).
  2. Get the control of the currently opened window.
  3. Click on the "Cookie Policy" link.
  4. Now get the control of all the opened windows.
  5. Switch to the second window (Second Window).
  6. Get the title of the page.
  7. Switch back to the main window.

 package com.sessions;  
   
 import java.util.Iterator;  
 import java.util.Set;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
   
 public class MultipleWindows {  
   
      public static void main(String[] args) throws InterruptedException {  
             
     System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
           WebDriver driver = new ChromeDriver();  
             
           //Facebook application opened  
           driver.get("http://www.facebook.com/");  
             
           //window is maximized  
           driver.manage().window().maximize();  
             
           //control of main window is taken  
           String parentwindow = driver.getWindowHandle();  
             
           //"Cookie Policy" link is identified and clicked on it  
           driver.findElement(By.partialLinkText("Cookie")).click();  
             
           //control of all the opened window is taken  
           Set<String> windows = driver.getWindowHandles();  
             
           //logic to switch to the the expected window  
           Iterator<String> iterator = windows.iterator();  
           while(iterator.hasNext())  
           {  
                  
           String chid = iterator.next();  
           if(!parentwindow.equalsIgnoreCase(chid));  
           {  
                driver.switchTo().window(chid);  
           }  
                  
           }  
             
           //Waiting on the expected window for the specified time to observe the flow  
           //Thread.sleep() - Throws expection. It will ask to handle it while writting the command.   
           Thread.sleep(5000);  
             
           //Title of the expected page is displayed on the console  
           System.out.println(driver.getTitle());  
             
           //switching back to the main window  
           driver.switchTo().window(parentwindow);  
             
   
      }  
   
 }  
   


Explanation of below code

  • 'windows' - refers to all the opened windows. 
  • Using the concept of Iterator interface and its iterator() method, to iterate over all the opened windows.
  • Checking whether newly opened window apart from the main window(parent window).
  • If yes, then switching to the newly opened window(child window).
Note - To understand the concept of Iterator Interface, refer the link - Iterator




           Set<String> windows = driver.getWindowHandles();  
             
           Iterator<String> iterator = windows.iterator();  
           while(iterator.hasNext())  
           {  
                  
           String chid = iterator.next();  
           if(!parentwindow.equalsIgnoreCase(chid));  
           {  
                driver.switchTo().window(chid);  
           }  


Point to Note 

If there are multiple windows opened and the user wants to go to the 3rd window, above logic won't work.

How to achieve it?

Solution - We have to convert Set<String> to ArrayList<String>.

If you are not aware of the concept of ArrayList, refer the link - ArrayList

We will do the above program using ArrayList.


 package com.sessions;  
   
 import java.util.ArrayList;  
 import java.util.Iterator;  
 import java.util.Set;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
   
   
 public class MultipleWindows {  
   
      public static void main(String[] args) throws InterruptedException {  
             
     System.setProperty("webdriver.chrome.driver", "F:\\software\\chromedriver_win32\\chromedriver.exe");  
           WebDriver driver = new ChromeDriver();  
             
           //Facebook application opened  
           driver.get("http://www.facebook.com/");  
             
           //window is maximized  
           driver.manage().window().maximize();  
             
           //control of main window is taken  
           String parentwindow = driver.getWindowHandle();  
             
           //"Cookie Policy" link is identified and clicked on it  
           driver.findElement(By.partialLinkText("Cookie")).click();  
             
           //control of all the opened window is taken  
           Set<String> windows = driver.getWindowHandles();  
             
           //Converting List to ArrayList  
           ArrayList<String> tabs = new ArrayList<String>(windows);  
             
           /*Switching to the expected window  
            get(index) -> Here index starts from 0,1,2,3,4 and so on.  
            '0' represents parent window.*/  
           driver.switchTo().window(tabs.get(1));  
             
           //Waiting on the expected window for the specified time to observe the flow  
           //Thread.sleep() - Throws exception. It will ask to handle it while writing the command.   
           Thread.sleep(5000);  
             
           //Title of the expected page is displayed on the console  
           System.out.println(driver.getTitle());  
             
           //switching back to the parent window  
           driver.switchTo().window(tabs.get(0));  
             
   
      }  
   
 }  
   



Similarly, if we want to go to the 3rd window, simply write -

-> driver.switchTo().window(tabs.get(2));



That's all guys!!! This is all about how to handle multiple windows using Selenium WebDriver.


If you like this post, please share your views in the comment box. If you have any doubt, I will be grateful to help you all.


2 comments:

  1. Excellent and detailed explanation.
    Thanks,
    https://www.flowerbrackets.com/arraylist-in-java/

    ReplyDelete
  2. Thank you for sharing wonderful information with us to get some idea about that content.
    Selenium Online Courses
    Selenium Online Training Hyderabad

    ReplyDelete