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.



ADVERTISEMENT
ADVERTISEMENT