首页 >后端开发 >php教程 >使用phpunit的Selenium Web驱动程序API

使用phpunit的Selenium Web驱动程序API

William Shakespeare
William Shakespeare原创
2025-02-17 12:20:14328浏览

>本文使用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

    <code class="language-php">public function setUp()
    {
        $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', DesiredCapabilities::firefox());
    }</code>
  3. >浏览器闭合:

    方法对于每次测试后关闭浏览器会话至关重要: tearDown()

    <code class="language-php">public function tearDown()
    {
        $this->webDriver->quit();
    }</code>
  4. 形式互动:

    方法使用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>
  5. >

    测试用例:测试方法使用数据提供商(从上一篇文章中假定)来提供测试输入。断言验证预期结果(成功或错误消息)。 示例:

    <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>

Using the Selenium Web Driver API with PHPUnit

Using the Selenium Web Driver API with PHPUnit

  1. 屏幕截图捕获: 方法允许在测试执行过程中捕获屏幕截图:takeScreenshot()

    <code class="language-php">$this->webDriver->takeScreenshot(__DIR__ . "/../../public/screenshots/screenshot.jpg");</code>
  2. 等待元素:>带有wait()的方法until() hands ashynchronous page loading:> WebDriverExpectedCondition

    <code class="language-php">$this->webDriver->wait(10, 300)->until(WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::name('username')));</code>
  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