How To Handle Popups

What is Alert?

An Alert in Selenium is a small window or message box which appears on screen to displays some information or warning on the screen. It asks for permission to do some type of operation. Some alerts also asks some input from the user.

How to handle Alert in Selenium WebDriver:

To handle alert in selenium, we need to shift focus of selenium from main page to alert.
To shift focus to alert we use:

driver.switchTo().alert();

After switching on alert we can perform different operations using different methods as below:

void sendKeys(String stringToSend): This method is use to send some data to alert box.

void accept(): This method is use to click on the ‘OK’ button of the alert.

void dismiss(): This method is use to click on the ‘Cancel’ button of the alert.

String getText(): This method is use to capture the alert message.

Example:

CODE
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertDemo {

	public static void main(String[] args) throws InterruptedException {

		System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Files\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://demoqa.com/alerts");
		driver.manage().window().maximize();

		// opening an alert
		WebElement btn = driver.findElement(By.xpath("//button[@id=`confirmButton`]"));
		btn.click();
		
		// switching to alert 
		Alert alt = driver.switchTo().alert();
		
		// accepting alert
		alt.accept();
		
		// capturing alert message that is shown after clicking ok button		
		String confirmResult = driver.findElement(By.xpath("//span[@id=`confirmResult`]")).getText();
		
        // Displaying alert message		
		System.out.println(confirmResult);
		
	}

}

What is Popup?

Popup is nothing but new tab or new window which are generated from the main page actions. In automation, when we have multiple windows in any web application, the activity may need to switch control among several windows from one to other in order to complete the operation.
To handle multiple windows in Selenium web driver there are methods:

Driver.getWindowHandle();

This method gives the main page address. So when the site opens, we need to handle the main window by driver.getWindowHandle(). Its return type is String.

Driver.getWindowHandles();

This method gives addresses of all the windows. So to handle all opened windows by web driver, we can use this method and then we can switch from one window to another in a web application. Its return type is Set of String.

Example:

CODE
import java.util.Set;
import java.util.Iterator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WindowHandles {

	public static void main(String[] args) throws InterruptedException {

		System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Files\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://demoqa.com/browser-windows");
		driver.manage().window().maximize();

		// opening new tab
		WebElement btn = driver.findElement(By.xpath("//button[@id=`tabButton`]"));
		btn.click();
		
		// To handle main window --> getWindowHandle()
		String MainWindow=driver.getWindowHandle();		
		
        // To handle all new opened window --> getWindowHandles()			
        Set<String> s =driver.getWindowHandles();		
        Iterator<String> it =s.iterator();		
        		
        while(it.hasNext())			
        {		
            String ChildWindow = it.next();		
            		
            if(!MainWindow.equalsIgnoreCase(ChildWindow))			
            {    		
                // Switching to Child window
                driver.switchTo().window(ChildWindow);	                                                                                                           
               
                // Getting text from child window
                String text = driver.findElement(By.id("sampleHeading")).getText();			
                   
                // Displaying text
                System.out.println(text);
                
                // Closing the Child Window.
                driver.close();		
            }		
        }	
        
        // Switching to Parent window i.e Main Window.
            driver.switchTo().window(MainWindow);
		
	}

}
#How_to_handle_popups_in_selenium_with_example #How_to_handle_Alert_in_Selenium_WebDriver_with_example #Driver.getWindowHandle #Driver.getWindowHandles

(New page will open, for Comment)

Not yet commented...