无法在 Windows 上设置 PHPUnit 和 Selenium 测试环境
Trouble setup PHPUnit and Selenium testing environment on Windows
我在 Windows 7 x64 机器上设置 PHPUnit 和 Selenium 测试环境时遇到问题。 Apache/2.4.20 (Win64) OpenSSL/1.0.2h PHP/7.0.11.
我的 composer.json 文件如下所示(片段):
"require": {
"php": ">=5.6.0",
"psr/http-message": "^1.0",
"psr/cache": "^1.0",
"zendframework/zend-diactoros": "^1.3",
"phpunit/phpunit": "^5.6",
"phpunit/phpunit-selenium": "^3.0"
},
我已经下载了以下组件:
- selenium-server-standalone-3.0.1 来自 http://www.seleniumhq.org/download/
- chromedriver_win32.zip(ofc 提取)来自 https://chromedriver.storage.googleapis.com/index.html?path=2.27/
所有文件都存储在文件夹D:\dev\selenium\
。
Selenium 服务器通过以下命令启动 C:\ProgramData\Oracle\Java\javapath\java.exe -Dwebdriver.chrome.driver="D:\dev\selenium\chromedriver.exe" -jar "D:\dev\selenium\selenium-server-standalone-3.0.1.jar"
在命令提示符下。
我整天都在 Google 上搜索,但一无所获;这就是我问你们的原因。当我试图通过在项目文件夹中执行命令 phpunit
来 运行 测试时,我没有得到任何结果。 Chrome 打开 URL data:;
并立即关闭。 Firefox 最差,它甚至打不开。标准 PHPUnit 测试已正确执行,我可以看到这些测试的结果。
-- 编辑--
忘了说我使用的是 Chrome 版本 55.0.2883.87 m x64。
如果有人可以建议所有这些实际有效的版本组合,我会非常高兴。
-- 编辑 2 --
Java版本:8更新111
Selenium 服务器的控制台输出:
09:50:06.651 INFO - Selenium build info: version: '3.0.1', revision: '1969d75'
09:50:06.651 INFO - Launching a standalone Selenium Server
2017-01-09 09:50:06.682:INFO::main: Logging initialized @414ms
09:50:06.745 INFO - Driver class not found: com.opera.core.systems.OperaDriver
09:50:06.745 INFO - Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
09:50:06.745 INFO - Driver class not found: com.opera.core.systems.OperaDriver
09:50:06.745 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
09:50:06.760 INFO - Driver provider org.openqa.selenium.safari.SafariDriver regi
stration is skipped: registration capabilities Capabilities [{browserName=safari, version=, platform
=MAC}] does not match the current platform VISTA
2017-01-09 09:50:06.807:INFO:osjs.Server:main: jetty-9.2.15.v20160210
2017-01-09 09:50:06.823:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@f5e5e3{/,null,AVAILABLE}
2017-01-09 09:50:06.994:INFO:osjs.ServerConnector:main: Started ServerConnector@c4039c{HTTP/1.1}{0.0.0.0:4444}
2017-01-09 09:50:06.994:INFO:osjs.Server:main: Started @739ms
09:50:06.994 INFO - Selenium Server is up and running
-- 编辑 3 --
PHPUnit_Extensions_Selenium2TestCase_WebDriverException: The best matching driver provider org.openqa.selenium.edge.EdgeDriver can't create a new driver instance for Capabilities [{browserName=*firefox}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: '****', ip: '10.10.146.251', os.name: 'Windows 7', os.arch: 'x86 (????)', os.version: '6.1', java.version: '1.8.0_111'
Driver info: driver.version: unknown
不敢相信还没有人回应。将近两天,我无法接近解决方案。无论如何,我目前正在考虑将 Codeception 作为替代方案,遗憾的是 Selenium 没有更好的支持!
这是一个已知问题:https://github.com/giorgiosironi/phpunit-selenium/issues/295#issuecomment-259398666
根据您当前的代码,您的网络测试文件应该是这样的。
这适用于 Chromedriver 2.25、phpunit 5.6.3、phpunit-selenium 3.0.2 和独立的 selenium 服务器 3.0.1,并假设您在本地 运行 测试(例如 NOT通过 jenkins 等远程机器上的硒集线器)
我添加了一些来自 Selenium2TestCase
的方法覆盖,其中一个将修复 运行 代码覆盖率(prepareSession
) 和一个有用的帮助函数,可以捕获测试失败的屏幕截图 (onNotSuccessfulTest
)
尝试使用以下内容作为测试 class:
namespace Acceptance\Tests;
use PHPUnit_Extensions_Selenium2TestCase;
class BaseWebTest extends PHPUnit_Extensions_Selenium2TestCase
{
// Remove: not needed
/*public static $browsers = [
[
'browserName' => 'chrome',
'host' => '127.0.0.1',
'port' => 4444,
'sessionStrategy' => 'shared'
]
];*/
/**
* Make sure parent setUp is called
*/
public function setUp()
{
parent::setUp();
$this->setHost($host); // localhost
$this->setPort((int)$port); // 4444
$this->setBrowser($browser); // chrome
$this->setBrowserUrl($application); // localhost/your_app (NOT just localhost)
$this->prepareSession()->currentWindow()->size(array('width' => 1920, 'height' => 1080)); // maximise window area
}
/**
* Ensure the session begins with a url that cookies can be set against.
* Without this calling the tester with code coverage breaks.
*
* @see https://github.com/giorgiosironi/phpunit-selenium/issues/295#issuecomment-259398666
*
* @return object
*/
public function prepareSession()
{
$session = parent::prepareSession();
$this->url('/');
return $session;
}
/**
* Override this method from \PHPUnit_Framework_TestCase so we can capture a screenshot.
*
* @return void
*/
public function onNotSuccessfulTest($exception)
{
$filedata = $this->currentScreenshot();
$file = 'YOUR\SCREENSHOT\DIR\HERE\' . basename(get_class($this)) . '.png';
file_put_contents($file, $filedata);
parent::onNotSuccessfulTest($exception);
}
public function testTitle()
{
$this->url('http://localhost/');
$this->assertEquals('Virtual host localhost configured', $this->title());
}
}
通过在单独的 window:
中调用它来确保 selenium 实际上是 运行
START java -Dwebdriver.chrome.driver=E:\path\to\chromedriver.exe -jar E:\path\to\selenium-server-standalone-3.0.1.jar
我在 Windows 7 x64 机器上设置 PHPUnit 和 Selenium 测试环境时遇到问题。 Apache/2.4.20 (Win64) OpenSSL/1.0.2h PHP/7.0.11.
我的 composer.json 文件如下所示(片段):
"require": {
"php": ">=5.6.0",
"psr/http-message": "^1.0",
"psr/cache": "^1.0",
"zendframework/zend-diactoros": "^1.3",
"phpunit/phpunit": "^5.6",
"phpunit/phpunit-selenium": "^3.0"
},
我已经下载了以下组件:
- selenium-server-standalone-3.0.1 来自 http://www.seleniumhq.org/download/
- chromedriver_win32.zip(ofc 提取)来自 https://chromedriver.storage.googleapis.com/index.html?path=2.27/
所有文件都存储在文件夹D:\dev\selenium\
。
Selenium 服务器通过以下命令启动 C:\ProgramData\Oracle\Java\javapath\java.exe -Dwebdriver.chrome.driver="D:\dev\selenium\chromedriver.exe" -jar "D:\dev\selenium\selenium-server-standalone-3.0.1.jar"
在命令提示符下。
我整天都在 Google 上搜索,但一无所获;这就是我问你们的原因。当我试图通过在项目文件夹中执行命令 phpunit
来 运行 测试时,我没有得到任何结果。 Chrome 打开 URL data:;
并立即关闭。 Firefox 最差,它甚至打不开。标准 PHPUnit 测试已正确执行,我可以看到这些测试的结果。
-- 编辑--
忘了说我使用的是 Chrome 版本 55.0.2883.87 m x64。 如果有人可以建议所有这些实际有效的版本组合,我会非常高兴。
-- 编辑 2 --
Java版本:8更新111
Selenium 服务器的控制台输出:
09:50:06.651 INFO - Selenium build info: version: '3.0.1', revision: '1969d75'
09:50:06.651 INFO - Launching a standalone Selenium Server
2017-01-09 09:50:06.682:INFO::main: Logging initialized @414ms
09:50:06.745 INFO - Driver class not found: com.opera.core.systems.OperaDriver
09:50:06.745 INFO - Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
09:50:06.745 INFO - Driver class not found: com.opera.core.systems.OperaDriver
09:50:06.745 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
09:50:06.760 INFO - Driver provider org.openqa.selenium.safari.SafariDriver regi
stration is skipped: registration capabilities Capabilities [{browserName=safari, version=, platform
=MAC}] does not match the current platform VISTA
2017-01-09 09:50:06.807:INFO:osjs.Server:main: jetty-9.2.15.v20160210
2017-01-09 09:50:06.823:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@f5e5e3{/,null,AVAILABLE}
2017-01-09 09:50:06.994:INFO:osjs.ServerConnector:main: Started ServerConnector@c4039c{HTTP/1.1}{0.0.0.0:4444}
2017-01-09 09:50:06.994:INFO:osjs.Server:main: Started @739ms
09:50:06.994 INFO - Selenium Server is up and running
-- 编辑 3 --
PHPUnit_Extensions_Selenium2TestCase_WebDriverException: The best matching driver provider org.openqa.selenium.edge.EdgeDriver can't create a new driver instance for Capabilities [{browserName=*firefox}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: '****', ip: '10.10.146.251', os.name: 'Windows 7', os.arch: 'x86 (????)', os.version: '6.1', java.version: '1.8.0_111'
Driver info: driver.version: unknown
不敢相信还没有人回应。将近两天,我无法接近解决方案。无论如何,我目前正在考虑将 Codeception 作为替代方案,遗憾的是 Selenium 没有更好的支持!
这是一个已知问题:https://github.com/giorgiosironi/phpunit-selenium/issues/295#issuecomment-259398666
根据您当前的代码,您的网络测试文件应该是这样的。 这适用于 Chromedriver 2.25、phpunit 5.6.3、phpunit-selenium 3.0.2 和独立的 selenium 服务器 3.0.1,并假设您在本地 运行 测试(例如 NOT通过 jenkins 等远程机器上的硒集线器)
我添加了一些来自 Selenium2TestCase
的方法覆盖,其中一个将修复 运行 代码覆盖率(prepareSession
) 和一个有用的帮助函数,可以捕获测试失败的屏幕截图 (onNotSuccessfulTest
)
尝试使用以下内容作为测试 class:
namespace Acceptance\Tests;
use PHPUnit_Extensions_Selenium2TestCase;
class BaseWebTest extends PHPUnit_Extensions_Selenium2TestCase
{
// Remove: not needed
/*public static $browsers = [
[
'browserName' => 'chrome',
'host' => '127.0.0.1',
'port' => 4444,
'sessionStrategy' => 'shared'
]
];*/
/**
* Make sure parent setUp is called
*/
public function setUp()
{
parent::setUp();
$this->setHost($host); // localhost
$this->setPort((int)$port); // 4444
$this->setBrowser($browser); // chrome
$this->setBrowserUrl($application); // localhost/your_app (NOT just localhost)
$this->prepareSession()->currentWindow()->size(array('width' => 1920, 'height' => 1080)); // maximise window area
}
/**
* Ensure the session begins with a url that cookies can be set against.
* Without this calling the tester with code coverage breaks.
*
* @see https://github.com/giorgiosironi/phpunit-selenium/issues/295#issuecomment-259398666
*
* @return object
*/
public function prepareSession()
{
$session = parent::prepareSession();
$this->url('/');
return $session;
}
/**
* Override this method from \PHPUnit_Framework_TestCase so we can capture a screenshot.
*
* @return void
*/
public function onNotSuccessfulTest($exception)
{
$filedata = $this->currentScreenshot();
$file = 'YOUR\SCREENSHOT\DIR\HERE\' . basename(get_class($this)) . '.png';
file_put_contents($file, $filedata);
parent::onNotSuccessfulTest($exception);
}
public function testTitle()
{
$this->url('http://localhost/');
$this->assertEquals('Virtual host localhost configured', $this->title());
}
}
通过在单独的 window:
中调用它来确保 selenium 实际上是 运行START java -Dwebdriver.chrome.driver=E:\path\to\chromedriver.exe -jar E:\path\to\selenium-server-standalone-3.0.1.jar