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

时间:2015-12-07 22:21:15

标签: php curl selenium-webdriver webdriver phpunit

我正在试图弄清楚如何在多个phpunit测试中共享一个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);

测试/ 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();
    }
}

测试/ 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

运行的

谷歌测试首先运行,并且工作正常,但是当雅虎测试运行时,它会退出

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)以试图找出错误的原因&amp;这显然是因为curl资源已经从Google测试的有效资源变为雅虎测试时的0。

设置它的原因是我有一个完整的工作测试套件(用于工作项目),但目前它在一个脚本中运行大约50个测试,我正在试图弄清楚如何将测试分成逻辑组/文件,虽然共享相同的webdriver实例 - 在我看来,最好的方法是使用bootstrap文件来设置webdriver&amp;然后重复使用它。我所展示的代码只是一个概念验证,我很清楚在phpunit测试中调用print_r(),echo等不是测试事物的正确方法,我只是想学习和放大器;了解如何实现这一目标。

1 个答案:

答案 0 :(得分:2)

首先,不要使用全局变量,使用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之间共享相同的$驱动程序,但建议这样做,因为您需要为每个测试提供一个干净的平板。

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

setUpBeforeClass() 

setUp()
testTitle()

setUp()
testSomethingElse()

tearDownAfterClass() 

我建议您多阅读fixtures

相关问题