Advertisement
SeleniumETrainR

How to Handle Javascript Alert, Prompt, Confirmation dialog

Dec 14th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. import org.openqa.selenium.Alert;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.firefox.FirefoxDriver;
  5.  
  6.  
  7. public class JavaScriptAlertTest {
  8.  
  9.     public static void main(String[] args) throws InterruptedException {
  10.          
  11.        
  12.         WebDriver myTestDriver = new FirefoxDriver();
  13.         myTestDriver.get("http://tinyurl.com/d3lz94p");
  14.        
  15.         myTestDriver.manage().window().maximize();
  16.  
  17.         myTestDriver.findElement(By.xpath("//input[@value = 'alert']")).click();
  18.         Thread.sleep(2000L);
  19.         Alert javascriptAlert = myTestDriver.switchTo().alert();
  20.         System.out.println(javascriptAlert.getText()); // Get text on alert box
  21.         javascriptAlert.accept();
  22.        
  23.        
  24.         System.out.println("*************prompt******************************************");
  25.        
  26.         myTestDriver.findElement(By.xpath("//input[@value = 'prompt']")).click();
  27.         Thread.sleep(2000L);
  28.         Alert javascriptprompt = myTestDriver.switchTo().alert();
  29.         javascriptprompt.sendKeys("This is Selenium Training");
  30.  
  31.         System.out.println(javascriptprompt.getText()); // Get text on alert box
  32.        
  33.         javascriptprompt.accept();
  34.         javascriptprompt = myTestDriver.switchTo().alert();
  35.  
  36.         System.out.println(javascriptprompt.getText()); // Get text on alert box
  37.         javascriptprompt.accept();
  38.        
  39.        
  40.         myTestDriver.findElement(By.xpath("//input[@value = 'prompt']")).click();
  41.         Thread.sleep(2000L);
  42.         javascriptprompt = myTestDriver.switchTo().alert();
  43.        
  44.         System.out.println(javascriptprompt.getText()); // Get text on alert box
  45.  
  46.         javascriptprompt.dismiss();
  47.         javascriptprompt = myTestDriver.switchTo().alert();
  48.  
  49.  
  50.         System.out.println(javascriptprompt.getText()); // Get text on alert box
  51.         javascriptprompt.accept();
  52.        
  53.         Thread.sleep(2000L);
  54.         System.out.println("***********************************confirm dialog box****************************");
  55.         myTestDriver.findElement(By.xpath("//input[@value = 'confirm']")).click();
  56.         Thread.sleep(2000L);
  57.         Alert javascriptconfirm = myTestDriver.switchTo().alert();
  58.         javascriptconfirm.accept();
  59.        
  60.         javascriptconfirm = myTestDriver.switchTo().alert();
  61.         System.out.println(javascriptconfirm.getText()); // Get text on alert box
  62.         javascriptconfirm.accept();
  63.        
  64.         myTestDriver.findElement(By.xpath("//input[@value = 'confirm']")).click();
  65.         Thread.sleep(2000L);
  66.         javascriptconfirm = myTestDriver.switchTo().alert();
  67.        
  68.         javascriptconfirm.dismiss();
  69.         javascriptconfirm = myTestDriver.switchTo().alert();
  70.         System.out.println(javascriptconfirm.getText()); // Get text on alert box
  71.         javascriptconfirm.accept();
  72.        
  73.         myTestDriver.quit();
  74.  
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement