Web 開發人員一般須要利用JavaScript彈出對話框來給用戶一些信息提示, 包括如下幾種類型javascript
1. 警告框: 用於提示用戶相關信息的驗證結果, 錯誤或警告等測試
2. 提示框: 用於提示用戶在當前對話框中輸入數據,通常須要用戶單擊取消或者確認按鈕url
3. 確認框: 用於提示用戶確認或者取消某個操做,通常須要用戶單擊取消或者確認按鈕spa
用以下頁面爲例進行講解, 包括了警告框,提示框,確認框code
http://sislands.com/coin70/week1/dialogbox.htmhtm
public static void testAlert(WebDriver driver) { String url="http://sislands.com/coin70/week1/dialogbox.htm"; driver.get(url); WebElement alertButton = driver.findElement(By.xpath("//input[@value='alert']")); alertButton.click(); Alert javascriptAlert = driver.switchTo().alert(); System.out.println(javascriptAlert.getText()); javascriptAlert.accept(); } public static void testPrompt(WebDriver driver) throws Exception { String url="http://sislands.com/coin70/week1/dialogbox.htm"; driver.get(url); WebElement promptButton = driver.findElement(By.xpath("//input[@value='prompt']")); promptButton.click(); Thread.sleep(2000); Alert javascriptPrompt = driver.switchTo().alert(); javascriptPrompt.sendKeys("This is learning Selenium"); javascriptPrompt.accept(); System.out.println(javascriptPrompt.getText()); javascriptPrompt=driver.switchTo().alert(); javascriptPrompt.accept(); Thread.sleep(2000); promptButton.click(); javascriptPrompt=driver.switchTo().alert(); javascriptPrompt.dismiss(); Thread.sleep(2000); javascriptPrompt=driver.switchTo().alert(); javascriptPrompt.accept(); } public static void testConfirm(WebDriver driver) throws Exception { String url="http://sislands.com/coin70/week1/dialogbox.htm"; driver.get(url); WebElement confirmButton = driver.findElement(By.xpath("//input[@value='confirm']")); confirmButton.click(); Thread.sleep(2000); Alert javascriptConfirm = driver.switchTo().alert(); javascriptConfirm.accept(); Thread.sleep(2000); javascriptConfirm = driver.switchTo().alert(); javascriptConfirm.accept(); }