Chapter 13: Popup and window Handling in selenium webdriver

In this chapter we will see how to handle alerts and popup window handling in selenium.

Alert is a pop up window that comes up on screen.Alerts are popup boxes that take your focus away from the current browser and forces you to read the alert message. You need to do some action such as accept or dismiss the alert box to resume your task on the browser.For e.g. user clicked on a button that displayed a message or may be when you entered a form, HTML page asked you for some extra information. 

To handle alerts popups we need to do switch to the alert window and call Selenium WebDriver Alert API methods.

, There are two types of alerts.

  1. Windows Based
  2. Web Based/Browser Based

As we know that handling windows based pop-ups is beyond WebDriver’s capabilities,so we used Browser based Alerts (Web based alert popups), Alert Interface. The Alert Interface provides some methods to handle the popups.

While running the WebDriver script, the driver control will be on the browser even after the alert generated which means the driver control will be behind the alert pop up. In order to switch the control to alert pop up, we use the following command :

There are the four methods that we would be using along with the Alert interface.Selenium provides us an interface called Alert. It is present in the org.openqa.selenium.Alert package. 

 void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.

driver.switchTo().alert().dismiss();

 void accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears.

driver.switchTo().alert().accept();

String getText() – The getText() method returns the text displayed on the alert box.

driver.switchTo().alert().getText

void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box

driver.switchTo().alert().sendKeys("Text");

We can easily switch to alert from the main window  by using Selenium’s .switchTo() method. These are the functions associated to alert command.

Lets take an html example

<html>
<head>
<title>Selenium Easy Alerts Sample </title>
</head>
<body>
<h2>Alert Box Demo</h2>
<fieldset>
<legend>Alert Box</legend><p>Click the button to display an alert box.</p>
<button onclick="alertFunction()">Click on me</button>
<script>
function alertFunction()
{
alert("I am example for alert box!");
}
</script>
</fieldset>
</body>
</html>

The output of above code is:

Complete webdriver code:

package testology;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class Alertexample 
{
WebDriver driver=new ChromeDriver();
	@Test
public void ExampleForAlert() throws InterruptedException
	{
driver.manage().window().maximize();
driver.get("file:///C:/Users/admin/Documents/selenium/html%20example/alert.html");
Thread.sleep(2000);
driver.findElement(By.xpath
("//button[@onclick='alertFunction()']")).click();
Alert alert=driver.switchTo().alert();
System.out.println(alert.getText());
		alert.accept();
	}
 }

How To Handle Multiple Windows Using Selenium WebDriver

In this topic, we will see how to handle multiple windows using Selenium WebDriver.We use ‘Switch To’ method which allows us to switch control from one window to other.Some web applications have many frames or multiple windows.Selenium WebDriver assigns an unique id to each window as soon as the WebDriver object is called. This unique  id is called window handle. Selenium uses this unique id to switch control among several windows.

There are 2 method through which we can switch from one window to other.

Driver.getWindowHandles();

When we open any website and it automatically open the requested function in new windows we used “Driver.getWindowHandles()” and then we can switch window from one window to another in a web application. Its return type is Iterator<String>.

Driver.getWindowHandle();

When we open any site, we need to handle the main window.this window is handled by driver.getWindowHandle(). This will handle the current window that uniquely identifies it within a driver instance. Its return type is String.

we use the following command on WebDriver – WebDriver.SwitchTo().window(String windowHandle);

This command takes in a window handle and switches the driver context on that window. 

Complete webdriver code:

package testology;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class windowHandle {
	WebDriver driver = new ChromeDriver();
   @Test
public void ExampleForwindowHandle() throws InterruptedException {
driver.manage().window().maximize();
driver.get("https://demoqa.com/automation-practice-switch-windows/");
		WebElement button = driver.findElement(By.cssSelector("button[onclick='newBrwTab()']"));
button.click();
String mainWindow = driver.getWindowHandle(); // It returns no. of windows opened by WebDriver and will return Set of Strings
Set<String> set = driver.getWindowHandles();// Using Iterator to iterate with in windows
Iterator<String> itr = set.iterator();
while (itr.hasNext()) 
    {
String childWindow = itr.next();// Compare whether the main windows is not equal to child window. If not equal,// we will close.
if (!mainWindow.equals(childWindow))
  {
driver.switchTo().window(childWindow);
System.out.println(driver.switchTo().window(childWindow).getTitle());
driver.close();
driver.switchTo().window(mainWindow);
	  }
      }
   }
}

See below video to understand more about iframe and new window handles in selenium.

You can download this html file used in the video to practice .