Web 開發人員通常需要利用JavaScript彈出對話框來給用戶一些信息提示, 包括以下幾種類型
閱讀目錄
- 對話框類型
- 測試頁面
- Selenium 操作對話框的代碼
對話框類型
1. 警告框: 用于提示用戶相關信息的驗證結果, 錯誤或警告等
2. 提示框: 用于提示用戶在當前對話框中輸入數據,一般需要用戶單擊取消或者確認按鈕
3. 確認框: 用于提示用戶確認或者取消某個操作,一般需要用戶單擊取消或者確認按鈕
測試頁面
用如下頁面為例進行講解, 包括了警告框,提示框,確認框
http://sislands.com/coin70/week1/dialogbox.htm
Selenium 操作對話框的代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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(); } |
以上就是對 java selenium操作彈出對話框的資料整理,后續繼續補充,謝謝大家對本站的支持!