How to Handle Alert using Selenium WebDriver?
package pkg_selenium;
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HandleAlert {
public static WebDriver driver;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// Initialize WebDriver
driver = new FirefoxDriver();
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://testwisely.com/demo/popups");
// Maximize Window
driver.manage().window().maximize();
// Click on 'Buy Now' Button
driver.findElement(By.xpath("//input[@value=' Buy Now ' and @type='button']")).click();
// Wait For Alert To Come
WebDriverWait wait = new WebDriverWait(driver,30);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
// Get the Text From the Alert
String alertText = alert.getText();
System.out.println("Alert Text :"+alertText);
// To Click on 'OK' Button of Alert
alert.accept();
// To Click on 'Cancel' Button of Alert
alert.dismiss();
// Close the driver
driver.quit();
}
}
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HandleAlert {
public static WebDriver driver;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// Initialize WebDriver
driver = new FirefoxDriver();
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Go to URL
driver.get("http://testwisely.com/demo/popups");
// Maximize Window
driver.manage().window().maximize();
// Click on 'Buy Now' Button
driver.findElement(By.xpath("//input[@value=' Buy Now ' and @type='button']")).click();
// Wait For Alert To Come
WebDriverWait wait = new WebDriverWait(driver,30);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
// Get the Text From the Alert
String alertText = alert.getText();
System.out.println("Alert Text :"+alertText);
// To Click on 'OK' Button of Alert
alert.accept();
// To Click on 'Cancel' Button of Alert
alert.dismiss();
// Close the driver
driver.quit();
}
}
Comments
Post a Comment