>本文使用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
<code class="language-php">public function setUp() { $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', DesiredCapabilities::firefox()); }</code>
方法对于每次测试后关闭浏览器会话至关重要:
tearDown()
<code class="language-php">public function tearDown() { $this->webDriver->quit(); }</code>
方法使用fillFormAndSubmit()
findElement()
与WebDriverBy
一起定位形式元素并与它们进行交互。
<code class="language-php">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(); }</code>
测试用例:测试方法使用数据提供商(从上一篇文章中假定)来提供测试输入。断言验证预期结果(成功或错误消息)。 示例:
<code class="language-php">/** * @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 }</code>
屏幕截图捕获: 方法允许在测试执行过程中捕获屏幕截图:takeScreenshot()
<code class="language-php">$this->webDriver->takeScreenshot(__DIR__ . "/../../public/screenshots/screenshot.jpg");</code>
等待元素:>带有或wait()
的方法until()
hands ashynchronous page loading:WebDriverExpectedCondition
<code class="language-php">$this->webDriver->wait(10, 300)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::name('username')));</code>
无头测试:xvfb-run
>
https://www.php.cn/link/5847ac0c855552d1b7c4c42a42a4c3f2418以上是使用phpunit的Selenium Web驱动程序API的详细内容。更多信息请关注PHP中文网其他相关文章!