How To Handle Multiple Window in Selenium?

Introduction:
As Technology is growing rapidly everything in the world is run by machines. These machines are controlled by the software inbuilt in it. In the field of software development, testing plays a vital role in making the process defect free. Selenium is one of the software testing tools that help in finding bugs and resolving them. This article is about handling multiple windows in Selenium and I hope this will give you an idea about how you can handle multiple windows while testing an application using selenium. Before starting to know about handling multiple windows first let’s take a look at Selenium.
What is selenium?
Selenium is an automation tool whose main purpose is to test web applications developed by any organization. Selenium is an open-source and costs free Web User Interface automation testing which was developed by Jason Huggins in 2004 as an internal tool at Thought Works. Selenium supports automation across different browsers, programming languages, and all platforms. Selenium tools can be deployed on platforms such as Windows, Linux, Solaris, and Macintosh easily. It also supports the Operating System for mobile applications like iOS, windows mobile, and android.
Importance of selenium:
Selenium is a web automation testing suite and WebDriver is a tool that is a part of Selenium Suite. Some of the features of selenium are listed below:
- Selenium is an Open-source testing suite and free of cost which gives the flexibility to automate any web application.
- It is used to develop any test automation framework using any programming language like java,c#, etc. to implement the Webdriver features.
- There are a lot of plug-ins and integrations available for Selenium Webdriver to test the web application.
Types of selenium:
Selenium is not a single tool but it is a suite of software, each used for different testing needs of an organization. It has four main components:
- Selenium Integrated Development Environment (IDE)
- Selenium Remote Control (RC)
- Selenium Grid
- Selenium WebDriver
Handling Multiple Windows in Selenium:
Many questions may arise in your mind. How to Handle Multiple Windows?, What is a window handle function?, How to handle multiple windows in a web application using a selenium tool for testing?. Well, here is the answer to all your questions.
Basics of multiple windows:
The only way to get multiple windows via Selenium web driver, that is by clicking on a link will open the page in a new browser window. Selenium web drivers must keep a track of how many windows have been opened during a session in the browser. This means that it will not keep track of any browser window which is opened manually on using the browser or opened by a previous session of Selenium Webdriver. Selenium WebDriver notices the duration from the time a WebDriver instance has been started to the time we need to destroy it via WebDriver. The task of the WebDriver must handle the windows used during a session in the web browser.
Selenium Web Driver:
Selenium Webdriver is a framework that is used for automation testing. It allows testing across various browsers on the internet. It is capable of executing multiple tests over multiple browsers on multiple Operating Systems. Web Driver can write a test script in Linux and execute it in Windows. The multiple programming languages that are supported by Web Driver are Java, Python, Ruby, .Net, PHP which helps in writing test scripts.
The tasks of WebDriver are listed below:
- WebDriver can make direct calls to the browser using each browser’s native support for automation using the browser driver.
- It contributes to its object-oriented API for Document Object Model interaction and browser control.
Window Handle:
A window handle is a unique identifier that holds the address of all the windows. This is a pointer to a window, which returns the string value. This window handle function helps in getting the handles of all the windows. Each browser will have a unique window handle. Testing workflows involving multiple windows became a usual thing. Selenium WebDriver helps to switch between these open windows. Whenever a new WebDriver object is instantiated, a unique alphanumeric Id is assigned to each unique window that is opened. This unique Id is known as the ‘window handle’. Thus, with the help of unique ids, we can easily switch controls between windows and perform required activities in the application.
Methods used in handling multiple windows are listed below:
- get.windowhandle(): It helps in getting the window handle of the current window in the application.
- get.windowhandles(): It helps in getting the handles of all the windows opened in the application
- set: It helps to set the window handles which are in the form of a string.
set<string> set= driver.get.windowhandles()
- switch to: It helps in switching between the windows while running an application
- action: It helps to perform certain actions on the windows in the application.
Let us first consider a few situations when dealing with multiple windows.
- While filling forms when a filling date may require to select the date from a separately opened window.
- Clicking on some link or button can move to another window.
- Handling Advertisement windows
Hence, we faced many situations depending upon the application.
Consider the example:
Step 1: Click Link1 on Window A.
As a result, a new Window B is opened.
Step 2: Moving Focus from Window A to Window B
Now, Window B is active
Step 3: Perform any Actions on Window B
Complete all Actions
Step 4: Moving Focus from Window B to Window A
Again, Window A is active now
Let’s understand the same concept with a small coding example.
public class MultiWindowHandle {
WebDriver driver;
public void setup() throws Exception {
driver=new FirefoxDriver();
String URL=”https://www.xyz.co.in/”;
driver.get(URL);
driver.manage().window().maximize();
}
//Testing
public void test() throws Exception {
// Opening Calendar
driver.findElement(By.xpath(“//image[@alt=’Calender’]”)).click();
// Storing parent window reference in a String Variable
String Parent_Window = driver.getWindowHandle();
// Switching from parent window to child window
for (String Child_Window : driver.getWindowHandles())
{
driver.switchTo().window(Child_Window);
// Performing actions on child window
driver.findElement(By.id(“calendar_month_txt”)).click();
List Months=driver.findElements(By.xpath(“//div[@id=’monthDropDown’]//div”));
int Months_Size=Months.size();
System.out.println(“Month size is:”+Months_Size);
Months.get(1).click();
driver.findElement(By.xpath(“//*[@id=’calendarDiv’]/div/table/tbody/tr/td[contains(text(),’18’)]”)).click();
}
//Switching back to Parent Window
driver.switchTo().window(Parent_Window);
//Performing some actions on Parent Window
driver.findElement(By.className(“btn_style”)).click();
}
public void close() {
driver.quit();
}
}
Conclusion:
Thus, this coding is executed to solve the above problem in handling multiple windows. I hope this helps to understand the concept of handling multiple windows. With selenium, it is easy to handle multiple windows. Selenium has a huge scope and demand for software testing. This article will help you in working with the selenium tool to handle multiple windows.