一、模態框的定義:
模態對話框(Modal Dialogue Box , 又叫做模式對話框),是指在用戶想要對話框以外的應用程序進行操作時候,必須先對該對話框進行響應.如單擊【確定】或者【返回】按鈕等關閉該對話框!
1.警告框
警告框經常用於確保用戶可以得到某些信息。
當警告框出現后,用戶需要點擊確定按鈕才能繼續進行操作。
語法:
代碼如下:
alert("文本")
2.確認框
確認框用於使用戶可以驗證或者接受某些信息。
當確認框出現后,用戶需要點擊確定或者取消按鈕才能繼續進行操作。
如果用戶點擊確認,那么返回值為 true。如果用戶點擊取消,那么返回值為 false。
語法:
confirm("文本")
3.提示框
提示框經常用於提示用戶在進入頁面前輸入某個值。
當提示框出現后,用戶需要輸入某個值,然后點擊確認或取消按鈕才能繼續操縱。
如果用戶點擊確認,那么返回值為輸入的值。如果用戶點擊取消,那么返回值為 null。
語法:
prompt("文本","默認值")
二、測試頁面准備
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>模態框</title> </head> <script type="text/javascript"> window.onload = function(){ document.getElementById("input_1").onclick = function(){ alert("充值成功!"); }; document.getElementById("input_2").onclick = function(){ confirm("確認充值50元?") }; document.getElementById("input_3").onclick = function(){ prompt("請輸入充值金額?","100"); }; } </script> <body> 測試練習模態框的處理:<br><br> 1.警告框 <input type="button" id="input_1" value="點擊彈出警告框"><br><br> 2.確認框 <input type="button" id="input_2" value="點擊彈出確認框"><br><br> 3.提示框 <input type="button" id="input_3" value="點擊彈出提示框"><br><br> </body> </html>
三、代碼實現
package cn.test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Test01 { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe"); WebDriver driver =null; try { driver = new ChromeDriver(); driver.get("file:///C:/Users/Administrator/Desktop/test/ModalDialogueBox.html"); driver.manage().window().maximize(); //1.點擊彈出警告框 driver.findElement(By.id("input_1")).click(); Thread.sleep(3000); //1.1 處理彈出警告框 System.out.println("獲取警告框文本值:"+driver.switchTo().alert().getText()); driver.switchTo().alert().accept();//模擬確認操作 //2. 點擊彈出確認框 driver.findElement(By.id("input_2")).click(); Thread.sleep(3000); //2.1 處理彈出確認框 System.out.println("獲取確認框文本值:"+driver.switchTo().alert().getText()); driver.switchTo().alert().accept();//模擬確認操作 //2.2 再次點擊彈出確認框演示取消操作 driver.findElement(By.id("input_2")).click(); Thread.sleep(3000); driver.switchTo().alert().dismiss();//模擬取消操作 //3.0 點擊彈出提示框 driver.findElement(By.id("input_3")).click(); System.out.println("獲取提示框文本值:"+driver.switchTo().alert().getText()); //3.1 處理彈出提示框 driver.switchTo().alert().sendKeys("500"); Thread.sleep(3000); driver.switchTo().alert().accept();//模擬確認操作 //3.2 再次點擊彈出提示框演示取消操作 driver.findElement(By.id("input_3")).click(); Thread.sleep(3000); driver.switchTo().alert().dismiss();//模擬取消操作 Thread.sleep(3000); } catch (Exception e) { e.printStackTrace(); } finally{ System.out.println("執行結束,關閉瀏覽器"); driver.quit(); } } }
四、學習后總結。不足之處后續補充修正!