<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 9X WebDriver – 處理警報/彈出框 > 原文: [https://javabeginnerstutorial.com/selenium/9x-webdriver-handling-alerts-popup-box/](https://javabeginnerstutorial.com/selenium/9x-webdriver-handling-alerts-popup-box/) 嗨呀愛好者! 想知道為什么 Selenium WebDriver 中存在警報界面? 好吧,我的朋友,對于您所尋找的信息,僅此帖子即可。 我們將討論基本上屬于三種類型的彈出框-警報框,確認框和提示框。 與往常一樣,我們有[演示站點](https://chandanachaitanya.github.io/selenium-practice-site/)來說明這些概念。 **注意:** *如果網頁上出現一個彈出框,則只要不接受或關閉警報,用戶就無法對基礎頁面執行任何操作。 請注意,如果您嘗試使用警報訪問網頁上的任何元素,則可能會碰到“`UnhandledAlertException`:存在模態對話框”。* ## 1.警報框 一個簡單的警報框通常用于將信息傳遞給用戶或發出一些警告。 除非單擊“確定”按鈕,否則用戶無法繼續進行操作。 JavaScript 代碼: ```java window.alert("This is a simple alert box! \nYou can't escape from me until you click 'OK'!"); ``` ![Alert Box](https://img.kancloud.cn/dc/1a/dc1ab17db5e0ebcfca4943323e2af141_780x231.png) ## 2\. 確認框 確認框為用戶提供了兩個選項,以驗證/接受或關閉某些內容。 用戶必須單擊“確定”或“取消”才能繼續。 確認彈出框返回布爾值。 當用戶點擊“確定”時返回`true`,而當點擊“取消”時返回`false`。 JavaScript code: ```java window.confirm("Click 'OK' or 'Cancel'."); ``` ![Confirm Box](https://img.kancloud.cn/2c/33/2c33779475a49c5da403d8a5848cd562_473x231.png) ## 3\. 提示框 在我們希望用戶輸入值的情況下,將使用提示框。 與其他警報框類似,用戶必須單擊“確定”或“取消”按鈕才能繼續操作。 提示框在單擊“確定”時返回輸入值,而在單擊“取消”時返回空值。 JavaScript 代碼: ```javascript window.prompt("Which Selenium Tool do you like the most?","e.g. Selenium IDE"); ``` ![Prompt Box](https://img.kancloud.cn/d9/33/d9337f4ef9782982ed08497be8c9b206_478x231.png) 希望您現在對基于 Web 的彈出窗口的不同類型有清楚的了解。 ## 處理彈出框: Selenium WebDriver 尚未提供任何處理這些彈出框的方法。 你相信我剛才說的嗎? 哈! 我知道沒有騙你的! WebDriver 總是對我們所有問題都有答案。 **警報接口**隨`org.openqa.selenium.Alert`*包*一起提供! 經常使用的一些最有用的方法是: ### 1\. `WebDriver switchTo()` 這是用于從主窗口切換到彈出窗口或顯示的警報的方法。 ```java driver.switchTo().alert(); ``` ### 2\. `void accept()` 此方法用于接受警報。 點擊“確定”按鈕。 ```java driver.switchTo().alert().accept(); ``` ### 3\. `void dismiss()` 此方法用于消除警報。 點擊“取消”按鈕。 ```java driver.switchTo().alert().dismiss(); ``` ### 4\. `String getText()` 要使文本顯示在彈出框中,請使用此方法,它將文本作為字符串返回。 ```java driver.switchTo().alert().getText(); ``` ### 5\. `void sendKeys(String textToSend)` 此方法用于在顯示的彈出框中(通常是提示框)輸入特定的`String`。 ```java driver.switchTo().alert().sendKeys(“sampleText”); ``` ## 場景 讓我們來看一個實現這些方法的測試用例,以更深入地了解這些概念, 1. 打開 Firefox 瀏覽器 2. 導航到[演示站點](https://chandanachaitanya.github.io/selenium-practice-site/) 3. 使用 ID 找到“警告框”按鈕 4. 單擊按鈕以彈出警報框 5. 獲取警報彈出窗口的顯示文本并將其打印到控制臺 6. 接受警報彈出窗口 7. 使用 ID 找到“確認框”按鈕 8. 單擊按鈕以確認框彈出 9. 將警報消息打印到控制臺 10. 關閉確認彈出窗口 11. 使用 XPath 找到“提示框”按鈕 12. 單擊按鈕,彈出提示框 13. 獲取提示框消息并將其顯示到控制臺 14. 接受提示彈出窗口 15. 驗證 Eclipse IDE 控制臺輸出屏幕和 JUnit 窗格是否成功 此方案的 JUnit 代碼是, ```java package com.blog.junitTests; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; 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 PopupBoxes { // Declaring variables private WebDriver driver; private String baseUrl; @Before public void setUp() throws Exception { // Selenium version 3 beta releases require system property set up System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\" + "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe"); // Create a new instance for the class FirefoxDriver // that implements WebDriver interface driver = new FirefoxDriver(); // Implicit wait for 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Assign the URL to be invoked to a String variable baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/"; } @Test public void testPageTitle() throws Exception { // Open baseUrl in Firefox browser window driver.get(baseUrl); // Locate 'Alert Box' button using id WebElement alertBoxBtn = driver.findElement(By.id("alertBox")); // Click the button for alert box popup alertBoxBtn.click(); // Switch the control to 'Alert Box' popup Alert alertPopUp = driver.switchTo().alert(); // Print the alert message to console System.out.println("Alert Box message: " + alertPopUp.getText()); // Accept the alert popup alertPopUp.accept(); Thread.sleep(5000); // Locate 'Confirm Box' button using id WebElement confirmBoxBtn = driver.findElement(By.id("confirmBox")); // Click the button for confirm box popup confirmBoxBtn.click(); // Switch control to 'Confirm Box' popup Alert confirmPopUp = driver.switchTo().alert(); // Dismiss the popup confirmPopUp.dismiss(); System.out.println("Confirm box popup dismissed!"); Thread.sleep(5000); // Locate 'Prompt Box' button using XPath WebElement promptBoxBtn = driver.findElement(By.xpath("/html/body/form/div[4]/div[3]/button")); // Click the button for prompt box popup promptBoxBtn.click(); // Switch control to 'Prompt Box' popup Alert promptPopUp = driver.switchTo().alert(); // Display the prompt message to console System.out.println(promptPopUp.getText()); // Click 'OK' promptPopUp.accept(); } //End of @Test @After public void tearDown() throws Exception { // Close the Firefox browser driver.close(); } } ``` *代碼說明:* 1\. 為了實例化一個彈出框,我們將必須導入`import openqa.selenium.Alert`包。 鍵入上述代碼后,“`Alert`”一詞下方會出現一條彎曲的線。 懸停時,蝕將建議所有可能的快速修復。 單擊建議導入“警報”包的第一個修補程序。 ![Import alert package](https://img.kancloud.cn/a9/4e/a94e84e35686602dcc66d965b7715c7f_393x197.png) ```java // Switch the control to 'Alert Box' popup Alert alertPopUp = driver.switchTo().alert(); ``` 該包指定了`Alert`接口,該界面用于處理基于 Web 的彈出框。 “`alertPopUp`”是為引用顯示的彈出窗口而創建的新實例變量。 2\. 要將控件從主窗口切換到彈出窗口, ```java driver.switchTo().alert(); ``` 3\. 要使文本顯示在彈出框中,請在引用所生成警報的實例變量上使用`getText()`方法。 ```java // Print the alert message to console System.out.println("Alert Box message: " + alertPopUp.getText()); ``` 4\. 要接受警報,請使用`accept()`方法。 ```java // Accept the alert popup alertPopUp.accept(); ``` 5\. 要關閉警報,請使用`dismiss()`方法。 ```java // Dismiss the popup confirmPopUp.dismiss(); ``` *執行結果:* 在 JUnit 窗口中,綠色條顯示測試用例已成功執行。 控制臺窗口顯示沒有任何錯誤。 它還按預期顯示所有打印的消息。 ![Alert Eclipse output](https://img.kancloud.cn/62/c8/62c85ff4f301af1cd3d1e965bf795949_677x342.png) 是時候練習了,我很快會在另一篇文章中見。 祝你有美好的一天!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看