Tuesday, November 4, 2014


A JavaScript alert box is used to provide some information to the user. Alert box provides only "OK" button. So, the user will have to click "OK" to proceed.





JavaScript Alert Box


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AlertExample {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http:// www.w3schools. com/js/tryit.asp?filename=tryjs_alert");

  driver.switchTo().frame(0);

  Thread.sleep(5000);
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();

 }

}