Handling different types of alerts in selenium

Alerts are basically popup box that takes the focus away from the current browser screen and forces you to read the alert message and do some action,
Once you take any action (accept or dismiss), it allows to resume performing the task on browser.

There are basically 3 types of alerts
Alert box, Confirm box, and Prompt box.

We will see how to handle each of these diff types of alerts in selenium

A simple alert:

in this either you can accept or dismiss an alert

package controls;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Alerts1 {
   static WebDriver driver;
   public static void main(String[] args) {
      driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.get("http://www.qavalidation.com/p/demo1.html");
      driver.findElement(By.id("alert")).click();
      if(IsAlertPresent()==1) //simply written a logic to chk if alert present
    {
        Alert alert=driver.switchTo().alert();
        alert.accept();   
   }    
   }
   public static int IsAlertPresent() {
      WebDriverWait wait = new WebDriverWait(driver, 300 /*timeout in seconds*/);
  if(wait.until(ExpectedConditions.alertIsPresent())==null)
     return 0;
  else
     return 1;
   }
}

Prompt alert

Here we can enter text in alert textbox, check the checkboxes and can accept / dismiss...

public static void main(String[] args) throws AWTException {
   driver=new FirefoxDriver();
   driver.manage().window().maximize();
   driver.get("http://www.qavalidation.com/p/demo1.html");
   //driver.findElement(By.id("alert")).click();
        driver.findElement(By.id("prompt")).click();
 if(IsAlertPresent()==1) //simply written a logic to chk if alert present
 {
    Alert alert=driver.switchTo().alert();
    alert.sendKeys("Kumar");
    alert.accept();   
    alert.sendKeys("Ramuk");
    alert.accept();   
    Robot r = new Robot();
    r.keyPress(KeyEvent.VK_TAB);
    Thread.sleep(2000);
    r.keyPress(KeyEvent.VK_SPACE);
    Thread.sleep(2000);
    alert.accept();
 }    
}

Confirm alert

Either you can accept and dismiss the alert, if u click on "prevent this from..." checkbox, you won't get the alert again for that browser instance...
public static void main(String[] args) throws AWTException, InterruptedException {
   driver=new FirefoxDriver();
   driver.manage().window().maximize();
   driver.get("http://www.qavalidation.com/p/demo1.html");
   driver.findElement(By.id("confirm")).click();
   if(IsAlertPresent()==1) //simply written a logic to chk if alert present
   {
        Alert alert=driver.switchTo().alert();
 alert.dismiss();
 Robot r = new Robot();
 r.keyPress(KeyEvent.VK_TAB);
 r.keyPress(KeyEvent.VK_TAB);
 Thread.sleep(1000);
 r.keyPress(KeyEvent.VK_SPACE);
 alert.accept();   
  }    
}
public static int IsAlertPresent()
   {
      WebDriverWait wait = new WebDriverWait(driver, 300 /*timeout in seconds*/);
      if(wait.until(ExpectedConditions.alertIsPresent())==null)
         return 0;
      else
         return 1;
   }

No comments:

Post a Comment