首頁 > 後端開發 > php教程 > 使用phpunit的Selenium Web驅動程序API

使用phpunit的Selenium Web驅動程序API

William Shakespeare
發布: 2025-02-17 12:20:14
原創
303 人瀏覽過

>本文使用Facebook的WebDriver軟件包進行瀏覽器仿真探討了PHP中的瀏覽器仿真,這是基於上一篇文章(此處不包括在此),該文章涵蓋了Phpunit的Selenium。 它重點介紹接受測試和自動化瀏覽器交互。

>

Using the Selenium Web Driver API with PHPUnit

>>與phpunit的硒擴展相關的密鑰差異:>

與Phpunit的自動處理不同,
    Facebook的網絡驅動器需要使用
  • 的手動瀏覽器會話封閉。 tearDown()>
  • 它利用
  • 類用於硒服務器交互。 RemoteWebDriver
實現步驟:

  1. 安裝:

    使用Composer安裝Facebook Web Driver軟件包:> composer require facebook/webdriver --dev

  2. 測試類設置:

    創建一個phpunit測試類(例如,)擴展UserSubscriptionTestFB.php方法初始化了PHPUnit_Framework_TestCase>實例,指定硒服務器地址(默認情況下setUp())和所需的瀏覽器功能(例如Firefox或Chrome)。 RemoteWebDriver> http://localhost:4444/wd/hub

    public function setUp()
    {
        $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', DesiredCapabilities::firefox());
    }
    登入後複製
  3. >瀏覽器閉合:

    方法對於每次測試後關閉瀏覽器會話至關重要: tearDown()

    public function tearDown()
    {
        $this->webDriver->quit();
    }
    登入後複製
  4. 形式互動:

    方法使用fillFormAndSubmit()findElement()WebDriverBy一起定位形式元素並與它們進行交互。 >

    public function fillFormAndSubmit($inputs)
    {
        $this->webDriver->get('http://vaprobash.dev/'); // Replace with your URL
        $form = $this->webDriver->findElement(WebDriverBy::id('subscriptionForm')); // Replace with your form ID
    
        foreach ($inputs as $input => $value) {
            $form->findElement(WebDriverBy::name($input))->sendKeys($value);
        }
    
        $form->submit();
    }
    登入後複製
  5. >

    測試用例:測試方法使用數據提供商(從上一篇文章中假定)來提供測試輸入。斷言驗證預期結果(成功或錯誤消息)。 示例:

    /**
     * @dataProvider validInputsProvider
     */
    public function testValidFormSubmission(array $inputs)
    {
        $this->fillFormAndSubmit($inputs);
        $content = $this->webDriver->findElement(WebDriverBy::tagName('body'))->getText();
        $this->assertEquals('Everything is Good!', $content); // Replace with your success message
    }
    登入後複製

Using the Selenium Web Driver API with PHPUnit

Using the Selenium Web Driver API with PHPUnit

  1. 屏幕截圖捕獲: 方法允許在測試執行過程中捕獲屏幕截圖:takeScreenshot()

    $this->webDriver->takeScreenshot(__DIR__ . "/../../public/screenshots/screenshot.jpg");
    登入後複製
  2. 等待元素:>帶有wait()的方法until() hands ashynchronous page loading:> WebDriverExpectedCondition

    $this->webDriver->wait(10, 300)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::name('username')));
    登入後複製
  3. >
  4. 高級交互:文章涵蓋了更高級的交互,例如拖放,警報處理和鍵盤快捷鍵。
  5. 無頭測試:文章說明瞭如何在沒有圖形顯示的情況下使用XVFB(x Virtual Framebuffer)進行無頭瀏覽器測試。 兩種方法(單獨運行XVFB並使用)均已詳細介紹。 xvfb-run

    Using the Selenium Web Driver API with PHPUnit Using the Selenium Web Driver API with PHPUnit

  6. >
有用的鏈接(從原始重複):

>

https://www.php.cn/link/5847ac0c855552d1b7c4c42a42a4c3f2418
  • https://www.php.cn/link/676bc6cef834fe54277b1954f6cd4cd4c5c
  • https://www.php.cn/link/f31bad56425d6425dd6d172c786a1bffe4a7
  • >>>>
  • >本文通過強調硒的更廣泛的實用程序,包括瀏覽器自動化任務。 FAQ部分提供了有關安裝,基本測試,異常處理,斷言,瀏覽器選擇,元素交互,等待元素,屏幕截圖捕獲,警報處理和並行測試執行的進一步指南。

以上是使用phpunit的Selenium Web驅動程序API的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板