Handling Alert and Popup In Selenium Webdriver


Alert and  Popup in Selenium

There are three type of popup and alert in Selenium

1)  Simple Alert
 2)  Confirmation Alert.
3) Prompt Alert.

How to handle Alert and popu in Selenium WebDriver?

Alert interface provide various method to handle the alert in selenium

1)driver.switchTo().alert().dismiss();
Is Used to click on cancel button of alert msg

2)driver.switchTo().alert().accept();
Is Used to click on ok button of alert msg

3) driver.switchTo().alert().getText();
Is Used capture the alert msg

4) driver.switchTo().alert().sendKeys("Text");
Is Used enter the data in alet text box



     1)  Simple Alert.

driver.switchTo().alert().accept();

package automationtesting;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertandPopup {

       public static void main(String[] args) throws InterruptedException {
              // TODO Auto-generated method stub
             
              System.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");
              WebDriver driver= new ChromeDriver();
              driver.manage().window().maximize();
              driver.get("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html");
        driver.findElement(By.xpath("//*[@id=\"easycont\"]/div/div[2]/div[1]/div[2]/button")).click();
   Alert alrt = driver.switchTo().alert();
    driver.switchTo().alert().accept();
    driver.quit();
}

}
2) Confirmation Alert.

driver.switchTo().alert().dismiss();

package automationtesting;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertandPopup {

       public static void main(String[] args) throws InterruptedException {
              // TODO Auto-generated method stub
             
              System.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");
              WebDriver driver= new ChromeDriver();
              driver.manage().window().maximize();
              driver.get("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html");        driver.findElement(By.xpath("//*[@id=\"easycont\"]/div/div[2]/div[2]/div[2]/button")).click();
Alert alrt = driver.switchTo().alert();
   Thread.sleep(5000);
    driver.switchTo().alert().dismiss();
    driver.quit();
}

}
Prompt Alert.

driver.switchTo().alert().getText();
driver.switchTo().alert().sendKeys("Text");


package automationtesting;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertandPopup {

       public static void main(String[] args) throws InterruptedException {
              // TODO Auto-generated method stub
             
              System.setProperty("webdriver.chrome.driver","D:\\Sandeep\\Soft\\chromedriver.exe");
              WebDriver driver= new ChromeDriver();
              driver.manage().window().maximize();
              driver.get("https://www.seleniumeasy.com/test/javascript-alert-box-demo.html");
       driver.findElement(By.xpath("//*[@id=\"easycont\"]/div/div[2]/div[3]/div[2]/button")).click();
                 
       Alert alrt = driver.switchTo().alert();
           Thread.sleep(5000);
       String  gtxt =driver.switchTo().alert().getText();
   
           if (gtxt.equals("Please enter your name")) {
            System.out.println(gtxt);
           }
            else {
               System.out.println("false");
            }
           driver.switchTo().alert().sendKeys("Text");
    driver.switchTo().alert().dismiss();
           Thread.sleep(5000);
    driver.quit();
}

}


Post a Comment

0 Comments