>本文使用Facebook的WebDriver軟件包進行瀏覽器仿真探討了PHP中的瀏覽器仿真,這是基於上一篇文章(此處不包括在此),該文章涵蓋了Phpunit的Selenium。 它重點介紹接受測試和自動化瀏覽器交互。
>
>>與phpunit的硒擴展相關的密鑰差異:
tearDown()
>
RemoteWebDriver
使用Composer安裝Facebook Web Driver軟件包:>
composer require facebook/webdriver --dev
創建一個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()); }
方法對於每次測試後關閉瀏覽器會話至關重要:
tearDown()
public function tearDown() { $this->webDriver->quit(); }
方法使用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(); }
測試用例:測試方法使用數據提供商(從上一篇文章中假定)來提供測試輸入。斷言驗證預期結果(成功或錯誤消息)。 示例:
/** * @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 }
屏幕截圖捕獲: 方法允許在測試執行過程中捕獲屏幕截圖:takeScreenshot()
$this->webDriver->takeScreenshot(__DIR__ . "/../../public/screenshots/screenshot.jpg");
等待元素:>帶有或wait()
的方法until()
hands ashynchronous page loading:WebDriverExpectedCondition
$this->webDriver->wait(10, 300)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::name('username')));
無頭測試:xvfb-run
>
https://www.php.cn/link/5847ac0c855552d1b7c4c42a42a4c3f2418以上是使用phpunit的Selenium Web驅動程序API的詳細內容。更多資訊請關注PHP中文網其他相關文章!