【问题标题】:How to open and view on screen a browser running a PHPUnit + Selenium (webdriver) test?如何在屏幕上打开和查看运行 PHPUnit + Selenium (webdriver) 测试的浏览器?
【发布时间】:2021-11-06 23:26:18
【问题描述】:

我正在尝试使用 Laravel + PHPUnit + Selenium Webdriver(Facebook\Webdriver 类)在容器内执行演示测试。

这是我的 HomePageTest.php

<?php

 namespace Tests\Feature;

 use Illuminate\Foundation\Testing\RefreshDatabase;
 use Tests\TestCase;
 use Facebook\WebDriver\Remote\RemoteWebDriver;
 use Facebook\WebDriver\Remote\DesiredCapabilities;
 use Facebook\WebDriver\WebDriverBy;
 use Facebook\WebDriver\WebDriverExpectedCondition;

class HomePageTest extends TestCase
{


/**
 * @return void
 */
public function testHomePageNotLogged()
{
    $host = 'http://selenium-hub:4444/wd/hub';

    $capabilities = DesiredCapabilities::firefox();

    $driver = RemoteWebDriver::create($host, $capabilities, 60000, 60000);

    // navigate to Selenium page on Wikipedia
    $driver->get('https://en.wikipedia.org/wiki/Selenium_(software)');

    // write 'PHP' in the search box
    $driver->findElement(WebDriverBy::id('searchInput')) // find search input element
        ->sendKeys('PHP') // fill the search box
        ->submit(); // submit the whole form

    // wait until 'PHP' is shown in the page heading element
    $driver->wait()->until(
        WebDriverExpectedCondition::elementTextContains(WebDriverBy::id('firstHeading'), 'PHP')
    );

    // print title of the current page to output
    echo "The title is '" . $driver->getTitle() . "'\n";

    // print URL of current page to output
    echo "The current URL is '" . $driver->getCurrentURL() . "'\n";

    // find element of 'History' item in menu
    $historyButton = $driver->findElement(
        WebDriverBy::cssSelector('#ca-history a')
    );

    // read text of the element and print it to output
    echo "About to click to button with text: '" . $historyButton->getText() . "'\n";

    // click the element to navigate to revision history page
    $historyButton->click();

    // wait until the target page is loaded
    $driver->wait()->until(
        WebDriverExpectedCondition::titleContains('Revision history')
    );

    // print the title of the current page
    echo "The title is '" . $driver->getTitle() . "'\n";

    // print the URI of the current page

    echo "The current URI is '" . $driver->getCurrentURL() . "'\n";


    // dump current cookies to output
    $cookies = $driver->manage()->getCookies();
    print_r($cookies);

    // terminate the session and close the browser
    $driver->quit();

  }
}

这是我的 docker-compose.yml 中关于 webdriver 的部分:

(...)
chrome:
image: selenium/node-chrome:4.0.0-rc-2-prerelease-20210908
shm_size: 2gb
depends_on:
  - selenium-hub
environment:
  - SE_EVENT_BUS_HOST=selenium-hub
  - SE_EVENT_BUS_PUBLISH_PORT=4442
  - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
ports:
  - "6900:5900"

edge:
    image: selenium/node-edge:4.0.0-rc-2-prerelease-20210908
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    ports:
      - "6901:5900"

  firefox:
    image: selenium/node-firefox:4.0.0-rc-2-prerelease-20210908
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
    ports:
      - "6902:5900"

  selenium-hub:
    image: selenium/hub:4.0.0-rc-2-prerelease-20210908
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

这是我过去在 VSCode Studio 终端上运行测试的方式:

docker-compose exec candidato php artisan test --filter HomePageTest

这是输出控制台:

The title is 'PHP - Wikipedia'
The current URL is 'https://en.wikipedia.org/wiki/PHP'
About to click to button with text: 'View history'
The title is 'PHP: Revision history - Wikipedia'
The current URI is 'https://en.wikipedia.org/w/index.php?title=PHP&action=history'
Array
(
    [0] => Facebook\WebDriver\Cookie Object
        (
            [cookie:protected] => Array
                (
                    [name] => WMF-Last-Access
                    [value] => 10-Sep-2021
                    [path] => /
                    [domain] => en.wikipedia.org
                    [expiry] => 1634040000
                    [secure] => 1
                    [httpOnly] => 1
                    [sameSite] => None
                )

        )

    [1] => Facebook\WebDriver\Cookie Object
        (
            [cookie:protected] => Array
                (
                    [name] => WMF-Last-Access-Global
                    [value] => 10-Sep-2021
                    [path] => /
                    [domain] => .wikipedia.org
                    [expiry] => 1634040000
                    [secure] => 1
                    [httpOnly] => 1
                    [sameSite] => None
                )

        )

    [2] => Facebook\WebDriver\Cookie Object
        (
            [cookie:protected] => Array
                (
                    [name] => GeoIP
                    [value] => BR:SC:Blumenau:-26.88:-49.10:v4
                    [path] => /
                    [domain] => .wikipedia.org
                    [secure] => 1
                    [httpOnly] => 
                    [sameSite] => None
                )

        )

    [3] => Facebook\WebDriver\Cookie Object
        (
            [cookie:protected] => Array
                (
                    [name] => enwikiwmE-sessionTickLastTickTime
                    [value] => 1631278158906
                    [path] => /
                    [domain] => en.wikipedia.org
                    [expiry] => 1633870158
                    [secure] => 
                    [httpOnly] => 
                    [sameSite] => None
                )

        )

    [4] => Facebook\WebDriver\Cookie Object
        (
            [cookie:protected] => Array
                (
                    [name] => enwikiwmE-sessionTickTickCount
                    [value] => 1
                    [path] => /
                    [domain] => en.wikipedia.org
                    [expiry] => 1633870158
                    [secure] => 
                    [httpOnly] => 
                    [sameSite] => None
                )

        )

    [5] => Facebook\WebDriver\Cookie Object
        (
            [cookie:protected] => Array
                (
                    [name] => enwikiel-sessionId
                    [value] => ad8362a761647ed4c36a
                    [path] => /
                    [domain] => en.wikipedia.org
                    [expiry] => 1633870158
                    [secure] => 
                    [httpOnly] => 
                    [sameSite] => None
                )

        )

    [6] => Facebook\WebDriver\Cookie Object
        (
            [cookie:protected] => Array
                (
                    [name] => enwikimwuser-sessionId
                    [value] => 8773dc6651c9a01ff6c7
                    [path] => /
                    [domain] => en.wikipedia.org
                    [secure] => 
                    [httpOnly] => 
                    [sameSite] => None
                )

        )

)

   WARN  Tests\Feature\HomePageTest
  ! home page not logged → This test did not perform any assertions  /app/tests/Feature/HomePageTest.php:24

  Tests:  1 risked
  Time:   15.03s

所以我假设测试运行正确,但我希望在屏幕上看到浏览器打开。 没有在屏幕上看到“实时”测试,我做错了什么?是因为容器的原因还是缺少可以打开浏览器的命令?

感谢您的帮助。

【问题讨论】:

    标签: selenium-webdriver docker-compose automated-tests phpunit


    【解决方案1】:

    几天后我发现我需要映射一个 VNC 端口来查看容器内运行的浏览器。所以我的 docker-compose.yml 看起来像这样:

      chrome:
        image: selenium/node-chrome:4.0.0-rc-2-prerelease-20210908
        shm_size: 2gb
        depends_on:
          - selenium-hub
        environment:
          - NOVNC=true
          - SE_EVENT_BUS_HOST=selenium-hub
          - SE_EVENT_BUS_PUBLISH_PORT=4442
          - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
          - NO_VNC_PORT=7900
        ports:
          - "6900:5900"
          - "7900:7900"
    
      edge:
        image: selenium/node-edge:4.0.0-rc-2-prerelease-20210908
        shm_size: 2gb
        depends_on:
          - selenium-hub
        environment:
          - NOVNC=true
          - SE_EVENT_BUS_HOST=selenium-hub
          - SE_EVENT_BUS_PUBLISH_PORT=4442
          - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
          - NO_VNC_PORT=7901
        ports:
          - "6901:5900"
          - "7901:7900"
    
      firefox:
        image: selenium/node-firefox:4.0.0-rc-2-prerelease-20210908
        shm_size: 2gb
        depends_on:
          - selenium-hub
        environment:
          - NOVNC=true
          - SE_EVENT_BUS_HOST=selenium-hub
          - SE_EVENT_BUS_PUBLISH_PORT=4442
          - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
          - NO_VNC_PORT=7902
        ports:
          - "6902:5900"
          - "7902:7900"
    
      selenium-hub:
        image: selenium/hub:4.0.0-rc-2-prerelease-20210908
        container_name: selenium-hub
        ports:
          - "4442:4442"
          - "4443:4443"
          - "4444:4444"
    

    所以我在任何浏览器中打开它:http://localhost:7900/

    并在屏幕上实时运行测试。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 2012-04-24
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多