带有 PHPUnit 的 Selenium 未加载页面
Selenium with PHPUnit not loading page
我正在尝试使用 phpunit 和 phpunit-selenium 为 php 项目制作测试服。在我的作曲家文件中,我有
"require-dev": {
"phpunit/phpunit": "^5.7",
"phpunit/phpunit-selenium": "^3.0"
}
安装的php单元版本为5.7.4
我正在使用 selenium-server-standalone-3.0.1.jar 作为 selenium 服务器。我启动服务器
java -Dwebdriver.gecko.driver="C:\Harlan\Selenium\geckodriver.exe" -jar selenium-server-standalone-3.0.1.jar
在我的测试中class我有
require_once dirname(__FILE__) . '/../../vendor/autoload.php';
class UserSubscriptionTest extends PHPUnit_Extensions_Selenium2TestCase {
public function setUp() {
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowserUrl('http://localhost/cwckids/web/user/login');
$this->setBrowser('firefox');
}
public function tearDown() {
$this->stop();
}
public function testFormSubmissionWithUsername()
{
$this->byName('login-form[login]')->value('admin');
$this->byName('login-form[password]')->value('mypassword');
$this->byId('login-form')->submit();
$content = $this->byTag('body')->text();
$this->assertEquals('Everything is Good!', $content, 'something wrong!!');
}
}
我的问题是 firefox 浏览器打开但无法加载页面 http://localhost/cwckids/web/user/login
测试立即失败,因为找不到元素。它给出一条消息说
无法定位元素:{"method":"name","selector":"login-form[login]"}
我找不到问题的解决方案。是不是版本不兼容?我尝试了几个版本的 Firefox 和 selenium 服务器。我的 Firefox 版本是 50.1.0。如果是版本不兼容,有人可以建议正确的版本吗?谢谢
完整的轨迹
C:\xampp\htdocs\seleniumtest>php单位tests/acceptance/UserSubscriptionTest.php
Sebastian Bergmann 和贡献者的 PHPUnit 5.7.4。
E 1 / 1 (100%)
时间:6.51 秒,内存:9.25MB
有 1 个错误:
1) UserSubscriptionTest::testFormSubmissionWithUsername
PHPUnit_Extensions_Selenium2TestCase_WebDriverException:无法定位元素:{"method":"name","selector":"login-form[login]"}
有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html
构建信息:版本:'2.53.1',修订:'a36b8b1',时间:'2016-06-30 17:37:03'
系统信息:host: 'DESKTOP-I0LAEAM', ip: '192.168.8.101', os.name: 'Windows 10', os.arch: 'amd64', os.version: ' 10.0', java.version: '1.8.0_77'
驱动程序信息:driver.version:未知
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Driver.php:165
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\CommandsHolder.php:108
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:134
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:175
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:108
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:394
C:\xampp\htdocs\seleniumtest\测试\验收\UserSubscriptionTest.php:66
C:\xampp\htdocs\seleniumtest\测试\验收\UserSubscriptionTest.php:66
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:348
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:314
错误!
测试:1,断言:0,错误:1。
有几点我建议更改:
你错过了对 $this->url();
的调用,我在你的测试方法中看到的是:
public function testFormSubmissionWithUsername()
{
$this->url('your actual URL here'); // Add this line
$this->byName('login-form[login]')->value('admin');
$this->byName('login-form[password]')->value('mypassword');
$this->byId('login-form')->submit();
$content = $this->byTag('body')->text();
$this->assertEquals('Everything is Good!', $content, 'something wrong!!');
}
您也没有从自己的 setUp 方法中调用 parent::setUp()
方法:
protected function setUp()
{
parent::setUp();
// Set your browser, port setup etc here
}
也没有必要在 tearDown 中显式调用 $this->stop();
,因此完全删除该函数。
最后,我会告诉 selenium 对失败进行截图,它们可以节省很多时间:
/**
* 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 . '\testfails\' . basename(get_class($this)) . '.png';
file_put_contents($file, $filedata);
parent::onNotSuccessfulTest($exception);
}
我正在尝试使用 phpunit 和 phpunit-selenium 为 php 项目制作测试服。在我的作曲家文件中,我有
"require-dev": {
"phpunit/phpunit": "^5.7",
"phpunit/phpunit-selenium": "^3.0"
}
安装的php单元版本为5.7.4 我正在使用 selenium-server-standalone-3.0.1.jar 作为 selenium 服务器。我启动服务器 java -Dwebdriver.gecko.driver="C:\Harlan\Selenium\geckodriver.exe" -jar selenium-server-standalone-3.0.1.jar
在我的测试中class我有
require_once dirname(__FILE__) . '/../../vendor/autoload.php';
class UserSubscriptionTest extends PHPUnit_Extensions_Selenium2TestCase {
public function setUp() {
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowserUrl('http://localhost/cwckids/web/user/login');
$this->setBrowser('firefox');
}
public function tearDown() {
$this->stop();
}
public function testFormSubmissionWithUsername()
{
$this->byName('login-form[login]')->value('admin');
$this->byName('login-form[password]')->value('mypassword');
$this->byId('login-form')->submit();
$content = $this->byTag('body')->text();
$this->assertEquals('Everything is Good!', $content, 'something wrong!!');
}
}
我的问题是 firefox 浏览器打开但无法加载页面 http://localhost/cwckids/web/user/login
测试立即失败,因为找不到元素。它给出一条消息说 无法定位元素:{"method":"name","selector":"login-form[login]"}
我找不到问题的解决方案。是不是版本不兼容?我尝试了几个版本的 Firefox 和 selenium 服务器。我的 Firefox 版本是 50.1.0。如果是版本不兼容,有人可以建议正确的版本吗?谢谢
完整的轨迹
C:\xampp\htdocs\seleniumtest>php单位tests/acceptance/UserSubscriptionTest.php Sebastian Bergmann 和贡献者的 PHPUnit 5.7.4。
E 1 / 1 (100%)
时间:6.51 秒,内存:9.25MB
有 1 个错误:
1) UserSubscriptionTest::testFormSubmissionWithUsername PHPUnit_Extensions_Selenium2TestCase_WebDriverException:无法定位元素:{"method":"name","selector":"login-form[login]"} 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html 构建信息:版本:'2.53.1',修订:'a36b8b1',时间:'2016-06-30 17:37:03' 系统信息:host: 'DESKTOP-I0LAEAM', ip: '192.168.8.101', os.name: 'Windows 10', os.arch: 'amd64', os.version: ' 10.0', java.version: '1.8.0_77' 驱动程序信息:driver.version:未知
C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Driver.php:165 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\CommandsHolder.php:108 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:134 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:175 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:108 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:394 C:\xampp\htdocs\seleniumtest\测试\验收\UserSubscriptionTest.php:66 C:\xampp\htdocs\seleniumtest\测试\验收\UserSubscriptionTest.php:66 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:348 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:314
错误! 测试:1,断言:0,错误:1。
有几点我建议更改:
你错过了对 $this->url();
的调用,我在你的测试方法中看到的是:
public function testFormSubmissionWithUsername()
{
$this->url('your actual URL here'); // Add this line
$this->byName('login-form[login]')->value('admin');
$this->byName('login-form[password]')->value('mypassword');
$this->byId('login-form')->submit();
$content = $this->byTag('body')->text();
$this->assertEquals('Everything is Good!', $content, 'something wrong!!');
}
您也没有从自己的 setUp 方法中调用 parent::setUp()
方法:
protected function setUp()
{
parent::setUp();
// Set your browser, port setup etc here
}
也没有必要在 tearDown 中显式调用 $this->stop();
,因此完全删除该函数。
最后,我会告诉 selenium 对失败进行截图,它们可以节省很多时间:
/**
* 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 . '\testfails\' . basename(get_class($this)) . '.png';
file_put_contents($file, $filedata);
parent::onNotSuccessfulTest($exception);
}