<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 功能強大 支持多語言、二開方便! 廣告
                # 硒無頭瀏覽器測試:HTMLUnitDriver & PhantomJS > 原文: [https://www.guru99.com/selenium-with-htmlunit-driver-phantomjs.html](https://www.guru99.com/selenium-with-htmlunit-driver-phantomjs.html) Selenium Web 驅動程序是一個 Web 自動化工具,使您可以在不同的瀏覽器上運行測試。 這些瀏覽器可以是 Internet Explorer,Firefox 或 Chrome。 要將特定的瀏覽器與 Selenium 一起使用,您需要相應的驅動程序。 在測試運行時,Selenium 將啟動在腳本中調用的相應瀏覽器并執行測試步驟。 您可以看到瀏覽器和正在執行的測試。 ## 什么是無頭瀏覽器? 無頭瀏覽器是沒有圖形用戶界面的 Web 瀏覽器**。 該程序的行為就像瀏覽器一樣,但是不會顯示任何 GUI。** 無頭驅動程序的一些示例包括 * HtmlUnit * 鬼 * 幻影 * 僵尸 * 水網驅動器 在本教程中,我們將重點介紹 HtmlUnit 和 PhatomJS ## HTMLUnitDriver HTML UnitDriver 是 WebDriver 的最輕巧,最快的實現無頭瀏覽器。 它基于 HtmlUnit。 它被稱為 **Headless Browser Driver** 。 它與 Chrome,IE 或 FireFox 驅動程序相同,但沒有 GUI,因此無法在屏幕上看到測試執行。 HTML 單元驅動程序的功能 * 支持 HTTPS 和 HTTP 協議 * 支持 HTML 響應(單擊鏈接,提交表單,遍歷 HTML 文檔的 DOM 模型等) * 支持 cookie * 代理服務器支持 * 支持基本和 NTLM 身份驗證 * 出色的 [JavaScript](/interactive-javascript-tutorials.html) 支持 * 支持提交方法 GET 和 POST * 能夠自定義發送到服務器的請求標頭 * 能夠確定來自服務器的失敗響應是應該引發異常還是應該作為適當類型的頁面返回 ## 將 HTMLUnit 驅動程序與 Selenium 結合使用的步驟 **步驟 1)**在 Eclipse 中,復制以下代碼。 將標準硒庫文件添加到項目中。 不需要其他的 jar 文件。 ``` package htmldriver; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.htmlunit.HtmlUnitDriver; public class htmlUnitYest { public static void main(String[] args) { // Creating a new instance of the HTML unit driver WebDriver driver = new HtmlUnitDriver(); // Navigate to Google driver.get("http://www.google.com"); // Locate the searchbox using its name WebElement element = driver.findElement(By.name("q")); // Enter a search query element.sendKeys("Guru99"); // Submit the query. Webdriver searches for the form using the text input element automatically // No need to locate/find the submit button element.submit(); // This code will print the page title System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } } ``` 步驟 2)運行代碼。 **您將觀察到沒有啟動瀏覽器,并且結果顯示在控制臺中。** ![HTMLUnitDriver & PhantomJS for Selenium Headless Testing](https://img.kancloud.cn/a7/b9/a7b91ed01a783902aaada36ab05bbca6_752x670.png "Selenium with HTMLUnitDriver & PhantomJS") ## HTML Unit Driver 的優點: * 由于它不使用任何 GUI 進行測試,因此您的測試將在后臺運行,而不會出現任何視覺中斷 * 與所有其他實例相比,執行速度更快 * 要通過 HtmlUnit 驅動程序運行測試,您還可以選擇其他瀏覽器版本 * 它是平臺獨立的,并且更容易同時運行多個測試。 [負載測試](/load-testing-tutorial.html)的理想選擇。 **局限性:** * 它無法模仿其他瀏覽器的 JavaScript 行為 ## 幻影 PhantomJS 是一款具有 JavaScript API 的無頭瀏覽器。 它是無頭網站測試,訪問和操作網頁&隨附的標準 DOM API 的最佳解決方案。 為了在 Seleniun 中使用 PhantomJS,必須使用 GhostDriver。 **GhostDriver** 是用于 PhantomJS 的簡單 JS 中的 Webdriver Wire 協議的實現。 PhatomJS 的最新版本具有**集成了** GhostDriver 和**,因此無需單獨安裝。** 系統運作方式如下: ![HTMLUnitDriver & PhantomJS for Selenium Headless Testing](https://img.kancloud.cn/1f/8c/1f8c39e7779d82a0d04fbde85d378ae8_985x355.png "Selenium with HTMLUnitDriver & PhantomJS") ## 使用 PhatomJS 運行 Selenium 的步驟 **步驟 1)**您需要安裝了 Selenium 的 Eclipse **步驟 2)**在此處下載 PhantomJS[](https://phantomjs.org/download.html) ![HTMLUnitDriver & PhantomJS for Selenium Headless Testing](https://img.kancloud.cn/11/77/1177febfef57e6404d5514a2876a8bec_743x747.png "Selenium with HTMLUnitDriver & PhantomJS") **步驟 3)**將下載的文件夾解壓縮到 Program Files ![HTMLUnitDriver & PhantomJS for Selenium Headless Testing](https://img.kancloud.cn/38/aa/38aa4f7d760186788c0df462068bc38e_552x672.png "Selenium with HTMLUnitDriver & PhantomJS") **步驟 4)**從[此處](http://mvnrepository.com/artifact/com.github.detro.ghostdriver/phantomjsdriver/1.1.0)下載 PhantomJS 驅動程序。 將罐子添加到您的項目 ![HTMLUnitDriver & PhantomJS for Selenium Headless Testing](https://img.kancloud.cn/39/1f/391f37c5330715a175a7b0f424451ef4_832x725.png "Selenium with HTMLUnitDriver & PhantomJS") **步驟 5)**將以下代碼粘貼到 Eclipse 中 ``` package htmldriver; import java.io.File; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.phantomjs.PhantomJSDriver; public class phantom { public static void main(String[] args) { File file = new File("C:/Program Files/phantomjs-2.0.0-windows/bin/phantomjs.exe"); System.setProperty("phantomjs.binary.path", file.getAbsolutePath()); WebDriver driver = new PhantomJSDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Guru99"); element.submit(); System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } } ``` **步驟 6)**運行代碼。 您會看到輸出顯示在控制臺中,并且沒有啟動瀏覽器。 **注意**:首次運行時,根據您的設置,您可能會從 Windows 收到安全警告,以允許運行 PhantomJS。 單擊允許訪問。 ![HTMLUnitDriver & PhantomJS for Selenium Headless Testing](https://img.kancloud.cn/f7/26/f72674f3e945109a10e6137b935f5cfc_693x672.png "Selenium with HTMLUnitDriver & PhantomJS") 許多組織將 Phantom.JS 用于各種目的,例如, * 無頭測試 * 屏幕截圖 * 頁面自動化 * 網絡監控 * 為用戶呈現儀表板屏幕截圖 * 在命令行上運行單元測試 * 從 HTML 到 PDF 生成員工手冊 * 與 QUnit 結合用于測試套件 **摘要** 為了在各種瀏覽器中快速測試應用程序并且沒有任何視覺中斷,使用了無頭瀏覽器[測試](/software-testing.html)。 由于其速度,準確性和易于訪問的功能,HTML 單元驅動程序和 PhantomJS 在無頭瀏覽器測試中越來越受歡迎。 通過執行一些簡單的步驟,您將了解這些工具與其他工具的集成以及執行測試代碼的難易程度。
                  <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>

                              哎呀哎呀视频在线观看