Skip to content

Instantly share code, notes, and snippets.

@udayanem
Created February 4, 2015 17:43
Show Gist options
  • Save udayanem/8b4672c13845b523af3b to your computer and use it in GitHub Desktop.
Save udayanem/8b4672c13845b523af3b to your computer and use it in GitHub Desktop.
How to handle JavaScript alerts or dialog boxes
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JavaScriptPopups
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
//Demonstration of Alert Box
driver.get("file:///E:/Programming%20Samples/HTML%20Samples/AlertBoxEx.html");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name("clickMe")).click();
Alert alert=driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
driver.close();
//Demonstration of Conformation Box
driver.get("file:///E:/Programming%20Samples/HTML%20Samples/ConformationBoxEx.html");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name("clickMe")).click();
Alert alert=driver.switchTo().alert();
System.out.println(alert.getText());
// alert.accept();
alert.dismiss();
// driver.close();
//Demonstration of Popup
driver.get("file:///E:/Programming%20Samples/HTML%20Samples/PopUpEx.html");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.name("clickMe")).click();
Alert alert=driver.switchTo().alert();
System.out.println(alert.getText());
alert.sendKeys("Uday");
// alert.accept();
alert.dismiss();
// driver.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment