SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Selenium WebDriver : Handling Javascript Alerts, Confirmations And Prompts

Alerts, Confirmation and Prompts are very commonly used elements of any software webpage and you must know how to handle all these popups In selenium webdriver software automation testing tool. If you know, Selenium IDE has many commands to handle alerts, confirmations and prompt popups like assertAlertassertConfirmationstoreAlertverifyAlertPresent, etc.. First of all, Let me show you all three different popup types to remove confusion from your mind and then we will see how to handle them In selenium webdriver.

Alert Popup
Generally alert message popup display on page of software web application with alert text and Ok button as shown In bellow given Image.



Confirmation Popup
Confirmation popup displays on page of software web application with confirmation text, Ok and Cancel button as shown In bellow given Image.



Prompt Popup
Prompts will have prompt text, Input text box, Ok and Cancel buttons.



Selenium webdriver software testing tool has Its own Alert Interface to handle all above different popups. Alert Interface has different methods like accept(), dismiss(), getText(), sendKeys(java.lang.String keysToSend) and we can use all these methods to perform different actions on popups.


Look at the bellow given simple example of handling alerts, confirmations and prompts In selenium webdriver software automation testing tool. Bellow given example will perform different actions (Like click on Ok button, Click on cancel button, retrieve alert text, type text In prompt text box etc..)on all three kind of popups using different methods of Alert Interface.

Run bellow given example In eclipse with testng and observe the result.
package Testng_Pack;

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;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class unexpected_alert {
 WebDriver driver;

 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
 }

 @AfterTest
 public void tearDown() throws Exception {
  driver.quit();
 }

 @Test
 public void Text() throws InterruptedException {
  //Alert Pop up Handling.
  driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
  //To locate alert.
  Alert A1 = driver.switchTo().alert();
  //To read the text from alert popup.
  String Alert1 = A1.getText();
  System.out.println(Alert1);
  Thread.sleep(2000);
  //To accept/Click Ok on alert popup.
  A1.accept();
  
  //Confirmation Pop up Handling.
  driver.findElement(By.xpath("//button[@onclick='myFunction()']")).click();
  Alert A2 = driver.switchTo().alert();
  String Alert2 = A2.getText();
  System.out.println(Alert2);
  Thread.sleep(2000);
  //To click On cancel button of confirmation box.
  A2.dismiss();
  
  //Prompt Pop up Handling.
  driver.findElement(By.xpath("//button[contains(.,'Show Me Prompt')]")).click();
  Alert A3 = driver.switchTo().alert();
  String Alert3 = A3.getText();
  System.out.println(Alert3);
  //To type text In text box of prompt pop up.
  A3.sendKeys("This Is John");
  Thread.sleep(2000);
  A3.accept();  
 }
}

This way you can handle different kind of alerts very easily using Alert Interface of selenium webdriver software automation testing tool. Next post will show you how to handle unexpected alerts In selenium webdriver.

6 comments:

  1. contains(.,'Show Me Prompt')]"
    why you used dot and coma after contains .ie,
    contains(.,

    ReplyDelete
  2. What if prompt dialog has two input area? example is the Authentication required dialog. This prompt needs both username and password.
    How do you set username and password to both textbox? What ID should be use?

    ReplyDelete
    Replies
    1. You need to use AutoIT tool with selenium webdriver to handle Authentication required dialog. View this page for more detail -> http://software-testing-tutorials-automation.blogspot.in/2014/12/how-to-handle-http-proxy-authentication.html

      Delete
  3. Im getting error for Alert code

    Alert A1 = driver.switchTo().alert();
    //To read the text from alert popup.
    String Alert1 = A1.getText();
    System.out.println(Alert1);
    Thread.sleep(2000);
    //To accept/Click Ok on alert popup.
    A1.accept();
    its giving red line for Alert

    ReplyDelete
    Replies
    1. you need to import this package
      "import org.openqa.selenium.Alert;"

      Delete
  4. Could not locate element on pop up ,i want to click save button on pop up window to save the record but the issue is both parent and pop up window have unique id of save button .

    ReplyDelete