我正试图弄清楚如何在多个php单元测试中共享一个php web驱动程序实例。该实例在第一次测试中运行良好,但到了第二次测试时,驱动程序执行器的curl资源已经丢失。
setup-webdriver.hp:
<?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
运行它
谷歌测试首先运行,运行良好,但当雅虎测试运行时,它会退出
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.
我只是添加了对CCD_ 2的调用,试图找出错误的原因&这显然是因为curl资源已经从Google测试的有效资源变成了Yahoo测试时的0。
设置这个的原因是,我有一个完整的工作测试套件(用于一个工作项目),但目前它在一个脚本中运行大约50个测试,我正试图找出如何将测试划分为逻辑组/文件,同时共享同一个webdriver实例-在我看来,最好的方法是使用引导文件来设置webdriver&然后重用它。我展示的代码只是概念的证明,我很清楚在phpunit测试中调用print_r()、echo等并不是正确的测试方法,我只是想学习&了解如何实现这一点。
首先,不要使用全局变量,使用PHPUnit中可用的fixture
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测试"时,测试的执行顺序将是:
setUpBeforeClass()
setUp()
testTitle()
setUp()
testSomethingElse()
tearDownAfterClass()
我建议阅读更多关于固定装置的信息