phpunit & php-webdriver 共享一个 webdriver 实例

phpunit & php-webdriver sharing a webdriver instance

我正在尝试弄清楚如何在多个 php 单元测试中共享单个 php-webdriver 实例。该实例在第一次测试中运行良好,但在第二次测试中,驱动程序的执行程序的 curl 资源已丢失。

设置-webdriver.php:

<?php

namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('php-webdriver-community/vendor/autoload.php');

$host = 'http://localhost:4444/wd/hub';
$capabilities = DesiredCapabilities::firefox();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);

tests/GoogleTest.php:

<?php

class GoogleTest extends PHPUnit_Framework_TestCase {

    public function testGoogleTitle() {

        global $driver;
        print_r($driver);
        $driver->get('http://www.google.com/');
        echo $driver->getTitle();
    }
}

tests/YahooTest.php:

<?php

class YahooTest extends PHPUnit_Framework_TestCase {

    public function testYahooTitle()
    {
        global $driver;
        print_r($driver);
        $driver->get('http://www.yahoo.com/');
        echo $driver->getTitle();
    }
}

我是 运行 它来自 /opt/local/bin/php56 /usr/local/bin/phpunit-5.1 --bootstrap setup-webdriver.php tests

Google 测试首先运行,并且工作正常,但是当 Yahoo 测试运行时,它退出

1) YahooTest::testYahooTitle
curl_setopt() expects parameter 1 to be resource, integer given

完整输出为:

PHPUnit 5.1.2 by Sebastian Bergmann and contributors.

.Facebook\WebDriver\Remote\RemoteWebDriver Object
(
    [executor:protected] => Facebook\WebDriver\Remote\HttpCommandExecutor Object
        (
            [url:protected] => http://localhost:4444/wd/hub
            [curl:protected] => Resource id #1007
        )

    [sessionID:protected] => 7b2577fa-98f1-4f75-9b18-a22a0b7474eb
    [mouse:protected] =>
    [keyboard:protected] =>
    [touch:protected] =>
    [executeMethod:protected] =>
)
GoogleE                                                                  2 / 2 (100%)Facebook\WebDriver\Remote\RemoteWebDriver Object
(
    [executor:protected] => Facebook\WebDriver\Remote\HttpCommandExecutor Object
        (
            [url:protected] => http://localhost:4444/wd/hub
            [curl:protected] => 0
        )

    [sessionID:protected] => 7b2577fa-98f1-4f75-9b18-a22a0b7474eb
    [mouse:protected] =>
    [keyboard:protected] =>
    [touch:protected] =>
    [executeMethod:protected] =>
)


Time: 2.67 seconds, Memory: 11.00Mb

There was 1 error:

1) YahooTest::testYahooTitle
curl_setopt() expects parameter 1 to be resource, integer given


/Users/robgudgeon/Downloads/code/phpunit-a-test/php-webdriver-community/lib/Remote/HttpCommandExecutor.php:227
/Users/robgudgeon/Downloads/code/phpunit-a-test/php-webdriver-community/lib/Remote/RemoteWebDriver.php:507
/Users/robgudgeon/Downloads/code/phpunit-a-test/php-webdriver-community/lib/Remote/RemoteWebDriver.php:187
/Users/robgudgeon/Downloads/code/phpunit-a-test/tests/YahooTest.php:9

FAILURES!
Tests: 2, Assertions: 0, Errors: 1.

我只是添加了对 print_r($driver) 的调用以试图找出错误的原因,这显然是因为 curl 资源已从 Google 测试的有效资源更改为在雅虎测试时为 0。

设置这个的原因是我有一个完整的工作测试套件(用于一个工作项目),但目前它在一个脚本中运行大约 50 个测试,我正在尝试找出如何拆分测试逻辑 groups/files,同时共享相同的 webdriver 实例 - 在我看来,最好的方法是使用 bootstrap 文件来设置 webdriver,然后重新使用它。我展示的代码只是一个概念验证,我很清楚在 php 单元测试中调用 print_r()、echo 等并不是测试事物的正确方法,我只是想学习和了解如何实现这一目标。

首先,不要使用全局变量,使用 PHPUnit 中可用的固定装置

class BaseTestCase extends PHPUnit_Framework_TestCase
{
    static $driver;

    public static function setUpBeforeClass()
    {

        $host = 'http://localhost:4444/wd/hub';
        $capabilities = DesiredCapabilities::firefox();
        self::$driver = RemoteWebDriver::create($host, $capabilities, 5000);
    }

    public static function tearDownAfterClass()
    {

        self::$driver->close();
    }

    public function getDriver()
    {
        return self::$driver;
    }
}

class GoogleTest extends BaseTestCase
{

    public function setUp()
    {
        $this->getDriver()->get('http://www.google.com/');
    }

    public function testTitle()
    {

        echo $this->getDriver()->getTitle();
    }

    public function testSomethingElse()
    {
        // do test
    }
}


class YahooTest extends BaseTestCase
{

    public function testYahooTitle()
    {
        $this->getDriver()->get('http://www.yahoo.com/');
        echo $this->getDriver()->getTitle();
    }
}

此示例不会在 GoogleTest 和 YahooTest 之间共享相同的 $driver,但建议这样做,因为您希望每个测试都有一个干净的平板。

然而,GoogleTest 中的所有测试将共享相同的驱动程序。 当您执行 'phpunit tests' 时,测试的执行顺序将是:

setUpBeforeClass() 

setUp()
testTitle()

setUp()
testSomethingElse()

tearDownAfterClass() 

我建议多阅读一些有关 fixtures

的内容