How to Handle Javascript Alert, Prompt, Confirmation dialog using selenium webdriver?
2012-10-29 10:06
639 查看
import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class JavaScriptAlertTest { public static void main(String[] args) { WebDriver myTestDriver = new FirefoxDriver(); myTestDriver.get("http://sislands.com/coin70/week1/dialogbox.htm"); myTestDriver.manage().window().maximize(); myTestDriver.findElement(By.xpath("//input[@value = 'alert']")).click(); Alert javascriptAlert = myTestDriver.switchTo().alert(); System.out.println(javascriptAlert.getText()); // Get text on alert box javascriptAlert.accept(); System.out.println("*************prompt******************************************"); myTestDriver.findElement(By.xpath("//input[@value = 'prompt']")).click(); Alert javascriptprompt = myTestDriver.switchTo().alert(); javascriptprompt.sendKeys("This is Selenium Training"); System.out.println(javascriptprompt.getText()); // Get text on alert box javascriptprompt.accept(); javascriptprompt = myTestDriver.switchTo().alert(); System.out.println(javascriptprompt.getText()); // Get text on alert box javascriptprompt.accept(); myTestDriver.findElement(By.xpath("//input[@value = 'prompt']")).click(); javascriptprompt = myTestDriver.switchTo().alert(); System.out.println(javascriptprompt.getText()); // Get text on alert box javascriptprompt.dismiss(); javascriptprompt = myTestDriver.switchTo().alert(); System.out.println(javascriptprompt.getText()); // Get text on alert box javascriptprompt.accept(); System.out.println("***********************************confirm dialog box****************************"); myTestDriver.findElement(By.xpath("//input[@value = 'confirm']")).click(); Alert javascriptconfirm = myTestDriver.switchTo().alert(); javascriptconfirm.accept(); javascriptconfirm = myTestDriver.switchTo().alert(); System.out.println(javascriptconfirm.getText()); // Get text on alert box javascriptconfirm.accept(); myTestDriver.findElement(By.xpath("//input[@value = 'confirm']")).click(); javascriptconfirm = myTestDriver.switchTo().alert(); javascriptconfirm.dismiss(); javascriptconfirm = myTestDriver.switchTo().alert(); System.out.println(javascriptconfirm.getText()); // Get text on alert box javascriptconfirm.accept(); } }
相关文章推荐
- How can selenium web driver get to know when the new window has opened and then resume its execution
- Using Selenium 2 ( WebDriver ) to run qUnit test page
- How to handle javascript alerts, confirmation and prompts?
- selenium webdriver学习(一)---如何处理alert、confirm、prompt对话框
- How to get browser name, version and operating system detail in selenium 2(Web driver)
- How to Configure Selenium Webdriver in Eclipse and execute some simple test script.
- How to set Selenium Python WebDriver default timeout?
- An Introduction to WebDriver Using the JavaScript Bindings
- Using NUnit to Execute Selenium WebDriver Tests
- How to handle your webdriver exceptions
- Selenium Web Driver : Handle Confirm Box using Java
- How to Locate Web Elements with Selenium WebDriver?
- selenium webdriver处理alert、confirm、prompt
- How to run Selenium webdriver on IE
- Using XPath to identify Web objects from Selenium WebDriver
- No Alert is present/No modal dialog found — WebDriver unable to catch JS error
- selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框( 转)
- selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框
- selenium(webdriver)学习笔记6--Alert/Confirm/Prompt的处理(Java版)
- How to do web automation test using Selenium