Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Alerts and Popups in Selenium

The next topic that we need to discuss in the selenium testing tutorial is Alert handling. And now we will initially start with

what is an Alert()?

An alert is a small message box that displays an on-screen notification. This gives the user some kind of information (or) ask permission for certain kind of options. An alert usually gives a warning to the user.

Alerts are classified into the following types:

Simple Alert():

A simple Alert displays some information (or) warning on the screen.

alerts and popups in selenium

Prompt alert():

A prompt alert usually asks some input from the user and selenium web driver can enter the text using send keys ("input")

 alerts and popups in selenium 

Confirmation alert():

A confirmation alert asks some input from the user to do some kind of operation

alerts and popups in selenium

In learn selenium online we will discuss

How to handle alerts in Selenium Webdriver?

Selenium web driver provides an alert () interface that contains the following methods

Method nameFunction Description
Void dismiss()driver.switchTo().alert().dismiss();This is used to cancel the alert button
Void accept()driver.switchTo().alert().alert();This is used to click OK button of the alert()
StringgetText()driver.switchTo().alert().getText();It is used to capture the alert message
Void sendKeys()driver.switchTo().alert().sendkeys(“text”);It is used to send some data to the alert box

We can implement these methods in Selenium web driver using the following code

import org.openqa.selenium.By;//import web driversimport org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;// import exception classesimport org.openqa.selenium.NoAlertPresentException;import org.openqa.selenium.Alert;public class AlertDemo {public static void main(String[] args) throws NoAlertPresentException,InterruptedException {System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");WebDriver driver = new ChromeDriver();// Alert Message handlingdriver.get("https://onlineitguru.com/onlinetutorials/selenium");driver.findElement(By.name("cusid")).sendKeys("53920");driver.findElement(By.name("submit")).submit();// Switching to AlertAlert alert = driver.switchTo().alert();// Capturing alert message.String alertMessage= driver.switchTo().alert().getText();// Displaying alert messageSystem.out.println(alertMessage);Thread.sleep(5000);// Accepting alertalert.accept();}}And the next topic in Selenium training is Pop- up

what is a pop- up?

It is an ability to interact with multiple windows including alerts.  This allows switching control to pop-up while keeping the browser in the background. And soon after the operation completion, this would return to the main window.

How to handle Selenium pop-ups using Selenium Web driver?

it training says the following methods can handle multiple windows

Driver.getWindowHandles():

This method is used to handle all the web driver, browser windows. In a web application, this allows you to switch from one window to the other. It usually returns the string Iterator.

Driver. getWindowHandle():

When the site is opened this method is used to handle the main window. This will usually handle the current window that uniquely identifies within the driver instance.

And by executing the following code, we handle the Selenium popp-ups:

import java.util.Iterator;import java.util.Set;//import Selenium packagesimport org.openqa.selenium.By;import org.openqa.selenium.WebDriver;// import borwser driversimport org.openqa.selenium.firefox.FirefoxDriver;public class WindowHandle_Demo {public static void main(String[] args) throws InterruptedException {WebDriver driver=new FirefoxDriver();//Launching the site.driver.get("https://onlineitguru.com/onlinetutorials/selenium");driver.manage().window().maximize();driver.findElement(By.xpath("//*[contains(@href,'popup.php')]")).click();String MainWindow=driver.getWindowHandle();// To handle all new opened window.Set s1=driver.getWindowHandles();Iterator i1=s1.iterator();while(i1.hasNext()){String ChildWindow=i1.next();if(!MainWindow.equalsIgnoreCase(ChildWindow)){// Switching to Child windowdriver.switchTo().window(ChildWindow);driver.findElement(By.name("emailid")).sendKeys("gaurav.3n@gmail.com");driver.findElement(By.name("btnLogin")).click();// Closing the Child Window.driver.close();}}// Switching to Parent window i.e Main Window.driver.switchTo().window(MainWindow);}}

Example 2 :

Scenario:

In real time projects, there are multiple windows. And these multiple windows throws multiple pop-ups. And have you think about how to handle all these pop-ups? Strictly speaking, have you think about how to handle these multiple windows. If so, I come with a solution for you. \

Solution:

selenium Web driver has the capability to handle multiple windows using the Switch Case. Execute the following code in your Eclipse IDE to handle multiple pop-ups

package softwareTestingMaterial;import org.testng.annotations.Test;//import JAVA packagesimport java.util.Iterator;import java.util.Set;//import web driversimport org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class MultipleWindowsClass{@Testpublic void testMultipleWindows() throws InterruptedException{System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\\src\\test\\java\\drivers\\geckodriver.exe");// To open browserWebDriver driver=new FirefoxDriver();// To maximize browserdriver.manage().window().maximize();// To open slenium tutorial with multiple windowsdriver.get("https://onlineitguru.com/onlinetutorials/selenium");// It will return the parent window name as a StringString mainWindow=driver.getWindowHandle();// It returns no. of windows opened by WebDriver and will return Set of StringsSet set =driver.getWindowHandles();// Using Iterator to iterate with in windowsIterator 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();}}// This is to switch to the main windowdriver.switchTo().window(mainWindow);}}

While  training at online educationmany people asked me what is the difference between  quit() and close()?

driver.quit()  is used to quit the whole browser session along with all the associated browser windows , tabs ad pop-ups.driver.close() is used to close the current browser window. if there is only one browser open, it quick to whole browser session.