Tuesday, November 4, 2014


A JavaScript prompt box is used when you want the user to enter a value before going to a page.

When a prompt box pops up, the user will have to enter a value by using WebElement sendkeys("String str") methond and then click either "OK" or "Cancel" to proceed after entering an input value.

If the user clicks "OK" the prompt box will return the value. If the user clicks "Cancel" the box returns null.





JavaScript Prompt Box Example



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 PromptExample {

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

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

  driver.switchTo().frame(0);

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

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

  alert.sendKeys("Hello Selenium");
  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();

 }

}