Use of Switch to commands for handling JavaScript pop ups and Pop up windows using Selenium WebDriver
While automating a web application we would encounter multiple windows, JavaScript pop up alerts, etc. This article is about how to deal with these while using Selenium WebDriver. Selenium commands by default gets executed on your parent window only always. During execution, if you encounter any pop up alerts or windows, you would have to switch the control to the corresponding window to perform any action on it. This is done in Selenium using the switchTo command. Let’s see in detail how this command can be used to handle pop up alerts and multiple windows.
Handling JavaScript Pop ups/Alerts
Alerts are small pop up boxes that you might have encountered while working with various web applications. These message boxes display some kind of message and ask the user to perform some action. When a pop up box appears, only if the user performs the action, you can proceed with any other operation in the application. There are different types of pop ups.
1.Simple Alerts box
This is often used to let the user know some information or a warning. The users need to click OK to proceed.
2.Prompt box
This type of box asks the user to input some value before proceeding in the application. It will have OK and Cancel buttons and te text field to enter the value. If the user clicks Ok , this box returns the input value and returns null if user clicks cancel.
3.Confirmation box
This type of pop up box is displayed when the application wants to confirm something from user. It has Ok and Cancel buttons. The user has to click Ok or Cancel to proceed.
Now, we have seen the different types of pop up alerts that we might encounter. So from this we can see that the various actions that we would have to perform for such pop-ups are Click Ok, Click Cancel, Input some information or Capture the alert message from the pop up.
Selenium WebDriver has an alert interface which provides various methods to handle the alerts. You can switch to an alert pop up from the main or parent window by using the switchTo method.
driver.switchTo.alert();
Based on the different actions that we want to perform on the pop up, we can select appropriate methods available in the alert interface in WebrRiver. Some of the common methods that you would have to use while automating applications are:
accept()
This method can be used to simulate the action of clicking ‘OK’ button in the pop up.
driver.switchTo.alert.accept();
dismiss()
This method can be used to simulate the action of clicking ‘Cancel’ buton of the pop up. It can be done as:
driver.switchTo.alert.dismiss();
getText()
This method can be used to capture the text message in the pop up.
driver.switchTo.alert.getText();
sendKeys(String string to send)
This method can be used to send some input to be entered in the pop up box.
driver.switchTo.alert.sendKeys(“hello”);
Handling multiple windows using Selenium WebDriver
This is another common scenario while working with any web application. Multiple windows gets opened as we navigate through the application. Selenium WebDriver assigns an alphanumeric ID called window handle to every new window that gets opened during the execution. This ID can be used to switch control between different windows. To get the window handles, we use the getWindowHandle() command.
getWindowHandle command- This command returns the window handle of the current window as a string. So it can be used as:
String handle = driver.getWindowHandle();
getWindowHandles command
This command can be used to get the window handles of all the windows open at a point of time. It returns a set of window handles. It can be used as:
Set<String> handles = driver.getWindowHandles();
Once you get the WindowHandles, you can switch to the required window using the switchTo command.
The switchTo().window() command supports moving between windows using the window name or window handles. The format is:
driver.switchTo().window(“windowname”);
driver.switchTo().window(handle);
If the window names are known , we can easily use it to switch between windows. Otherwise, we need to get the window handles and switch to it. When there may be multiple windows opened, we would have to use an iterator to get the windowhandles one by one as in the below case.
Consider the website www.monsterindia.com . Usually once you open this jobsite, one or many child windows opens up . We will try to close those and get the control back to the parent window.
Find below the code to close child windows which gets opened. For this we need to switch the control to child window and then close it and finally and return the control to the parent window.
driver.get("http://www.monsterindia.com/");//Launch the website String parent=driver.getWindowHandle(); //Get window handle of the parent window Set<String>s1=driver.getWindowHandles(); //Get window handles of all open windows at the time Iterator<String> I1= s1.iterator(); //Use the iterator to iterate all the window handles one by one while(I1.hasNext()) { String child_window=I1.next(); if(!parent.equals(child_window)) //Compare if the child window handle is not equal to parent window handle { driver.switchTo().window(child_window); //Switch to the child window System.out.println(driver.switchTo().window(child_window).getTitle()); //Get title of child window driver.close(); //Close the current child windoe } } driver.switchTo().window(parent); //Once all child windows are closed, switch to parent window
Hope, the above example gives you an idea of using getWindowHandles and switchTo commands to handle multiple windows.