×

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup

 An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser.

The Alert popup will be implemented with the help of JavaScript, so we cannot inspect any alert popup.

An Alert popup maximum contains two buttons inside it and with an error message.

To handle Alert popup, we need to take the help of an alert interface.

As you can see the below snapshot of alert popup,

snapshot of alert popup

Selenium web driver does not have the feature of switching to alert directly, to take control of alert popup, we will use the statement which is given below.

driver.switchTo().alert()

driver.switchTo().alert()

Alert is an interface which contains following methods-

  • accept()
  • dismiss()
  • getText()
  • sendKeys()
Alert
Alert Method Description Syntax
accept() This method is used to click on OK button. driver.switchTo().alert().accepts();
dismiss() This method is used to click on the CANCEL button. driver.switchTo().alert().dismiss();
getText() It is used to get the error message present in the popup, and the return type of getText() method is String. driver.switchTo().alert().getText();        
sendKeys() This method is used to send the data into the alert box.   driver.switchTo().alert().sendKeys("Text");  

Let us take one example to give you a better understanding of Alert Popup, and we created a sample test script which is as follows;

For our testing purpose, we are using the demo.automationtesting.in website to perform all the actions for alert popup handling in selenium web driver.

In this test case, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1. Open the Firefox Browser. System.setProperty()   The Firefox browser must be opened.
2. Navigate to the website. get() http://demo.automationtesting.in/Windows.html The home page window must be displayed.
3. Handling the alert popup with the OK and CANCEL button.     Ok and cancel button of the alert popup should be handled.
4. Create the Alert object     Alert object should be created.
5. Validating and printing the alert message. gettext()   The alert message should be printed and validated.
6. Click on the OK button to accept the alert popup. accept()   The Ok button should be clicked.
7. Move towards the main button to press the CANCEL button.     The CANCEL button should be pressed.
8. Click on the CANCEL button to dismiss the alert popup. dismiss()   The CANCEL button should be clicked.
9. Closing the browser window. close()   The browser window should be closed.

Open the Eclipse IDE and the existing test suite new_test, which we have created in WebDriver Installation section of the WebDriver tutorial.

Then, right-click on the src folder and create a new Class File from New ? Class.

created in WebDriver Installation
  • Give the Class name as Alert_popup and click onthe Finish button.
click on the Finish button

Step1:

To launch the Firefox browser, we need to download the geckoDriver.exe file and set the system property to the path of geckoDriver.exe file.

Here is the code for setting the system property of the Firefox Browser:

// System Property for gecko Driver   System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-v0.25.0-win64\geckodriver.exe");
// create an object for FirefoxDriver class.      
WebDriver driver=new FirefoxDriver(); 

Step2:

Now, we will get to the desired URL:

//navigate to the website.
driver.get("http://demo.automationtesting.in/Alert.html"); 

Step3:

After that, we will move to our next step, where we are handling the alert popup with the OK and CANCEL button, which is presented on the alert window of the website.

To identify the Alert popup first, right-click on the button called to Alert with OK & Cancel and select the Inspect element field.

alert window of the website.

The developer tool window will be launched, with all the specific codes used in the development of the particular button.

specific codes used in the development

Here the sample code,

//handling alert with OK and CANCEL button
 driver.findElement(By.xpath("//a[contains(text(),'Alert with OK & Cancel')]")).click();
 driver.findElement(By.id("CancelTab")).click();
 Thread.sleep(3000); 

Step4:

Now, we are creating the alert object with the help of the switchTo() method.

Here the sample code,

//creating the alert object
Alert act=driver.switchTo().alert();

Step5:

After that, we will be printing and validating the alert message using the getText() method and the switch.To() method.

Here the sample code,

//print the alert message
 System.out.println(act.getText());
 //validating the alert message
 String msg=act.getText();
 if(msg.equals("Press a Button !"))
 {
 System.out.println("actual alert message");
 } 
 else{
 System.out.println("in-correct alert message");
 }
 act= driver.switchTo().alert(); 

Step6:

In our next step, we are clicking on the OK button to accept the alert popup.

clicking on the OK button to accept the alert popup.

Here the sample code,

//click on OK button
 act.accept(); 
 msg=driver.findElement(By.xpath("//p[@id='demo']")).getText();
 System.out.println("click on the OK button:" + msg);
 Thread.sleep(5000);  

Step7:

Now again, we are moving towards the main button, i.e. display a confirm box and press the CANCEL button.

To identify the display a confirm box first, right-click on the button and select the Inspect element field.

identify the display a confirm box

The developer tool window will be launched, with all the specific codes used in the development of the particular button.

all the specific codes used in the development

Here the sample code,

//again move to main button to press cancel button
 driver.findElement(By.cssSelector("#CancelTab")).click();
 act= driver.switchTo().alert();
 Thread.sleep(5000);  

Step8:

Click on the CANCEL button to dismiss the alert popup.

Click on the CANCEL button

Here the sample code,

//click on CANCEL Button
 act.dismiss();  
 msg=driver.findElement(By.cssSelector("#demo")).getText();
 System.out.println("click on the CANCEL button:" + msg);
 Thread.sleep(3000); 

Step9:                                                                                 

Finally, we close the browser window.

 //close the browser
  driver.close();

After completing all the given steps, our final text script will look like this:

package testpackage;
 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.Alert;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver; 
 public class Alert_popup_handling {
 public static void main(String[] args) throws InterruptedException {
 // System Property for gecko Driver   
 System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-v0.25.0-win64\geckodriver.exe");
 // create an object for FirefoxDriver class.       
 WebDriver driver=new FirefoxDriver();
 //maximize the browser 
 driver.manage().window().maximize(); 
 //navigate to the url
 driver.get("http://demo.automationtesting.in/Alerts.html");
 driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
 //handling alert with OK and CANCEL button
 driver.findElement(By.xpath("//a[contains(text(),'Alert with OK & Cancel')]")).click();
 driver.findElement(By.id("CancelTab")).click(); 
 Thread.sleep(3000);
 //creating the alert object
 Alert act=driver.switchTo().alert();
 //print the alert message
 System.out.println(act.getText());
 //validating the alert message
 String msg=act.getText(); 
 if(msg.equals("Press a Button !"))
 {
 System.out.println("actual alert message");
 }
 else{
 System.out.println("in-correct alert message");
 }
 act= driver.switchTo().alert(); 
 //click on OK button
 act.accept(); 
 msg=driver.findElement(By.xpath("//p[@id='demo']")).getText();
 System.out.println("click on the OK button:" + msg);
 Thread.sleep(5000); 
 //again move to main button to press cancel button
 driver.findElement(By.cssSelector("#CancelTab")).click();
 act= driver.switchTo().alert();
 Thread.sleep(5000);  
 //click on CANCEL Button
 act.dismiss();  
 msg=driver.findElement(By.cssSelector("#demo")).getText();
 System.out.println("click on the CANCEL button:" + msg);
 Thread.sleep(3000);
 //closing the broswer window                     
 driver.close();
 }
 }
  • To run the test script in Eclipse, right-click on the window and then click Run as ? Java application and the test script will be launched in the chrome browser, and automate all the test scenarios.

Related Topics

Selenium WebDriver-CSS Selector: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class We will locate the web element with the help of tag and class CSS selector in this section of the tutorials Where Tag and class are...

4 minutes read.

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

4 minutes read.

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

WebDriver-Link Text locator

WebDriver-Link Text locator The Link text locator is used to identify only web link elements based on the value of the link. The Link text locators cannot be used for other than...

4 minutes read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

WebDriver-Partial Link Text locator

WebDriver-Partial Link Text locator In selenium WebDriver, there is an additional way to find a web link element with the help of partial link text locator. The partial link text is used...

4 minutes read.

CSS Selector: Inner text in Selenium IDE

CSS Selector: Inner text The inner text is used to identify and create a CSS Selector using a string pattern of the HTML tag which is displayed on the web page. Syntax...

1 minute read.

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 minutes read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver In this tutorial, we will learn how to identify the web element using the substring method. A Sub-string contains three methods for identifying the web element...

9 minutes read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute read.

WebDriver Interface | Class Diagram

WebDriver interface/class diagram: The interface of WebDriver includes the following stages, which is as follows: Search Context Web driver Remote web driver Driver class Search Context  The first interface of the web driver class is search Context,...

2 minutes read.

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

2 minutes read.

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 minutes read.

Selenium Assertion

Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver. An Assertion is a feature available in TestNG, which is used to verify the expected result of...

7 minutes read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

Selenium WebDriver-XPath Locator

Selenium WebDriver-XPath Locator The locators can make the object identification of all the selenium tools. There are eight locators available in selenium web driver, but most of the time we go for...

14 minutes read.

WebDriver-Class Name Locator

WebDriver-Class Name Locator In this tutorial, we will learn about the Class name locator of selenium WebDriver. The Class Name locator is used to identify any element in UI based on the...

4 minutes read.