Selenium与PHPUnit没有加载页面

时间:2016-12-21 14:23:15

标签: selenium selenium-webdriver phpunit

我正在尝试使用phpunit和phpunit-selenium为php项目进行测试。在我的作曲家文件中我有

"require-dev": {
    "phpunit/phpunit": "^5.7",
    "phpunit/phpunit-selenium": "^3.0"
}

已安装的phpunit版本为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

在我的测试课中我有

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> phpunit 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' 系统信息:主机:'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 \厂商\ PHPUnit的\ PHPUnit的硒\ PHPUnit的\扩展\ Selenium2TestCase \ Driver.php:165 C:\ XAMPP \ htdocs中\ seleniumtest \厂商\ PHPUnit的\ PHPUnit的硒\ PHPUnit的\扩展\ Selenium2TestCase \ CommandsHolder.php:108 C:\ XAMPP \ htdocs中\ seleniumtest \供应商\ PHPUnit的\ PHPUnit的硒\ PHPUnit的\扩展\ Selenium2TestCase \元素\ Accessor.php:134 C:\ XAMPP \ htdocs中\ seleniumtest \供应商\ PHPUnit的\ PHPUnit的硒\ PHPUnit的\扩展\ Selenium2TestCase \元素\ Accessor.php:175 C:\ XAMPP \ htdocs中\ seleniumtest \厂商\ PHPUnit的\ PHPUnit的硒\ PHPUnit的\扩展\ Selenium2TestCase \元素\ Accessor.php:108 C:\ XAMPP \ htdocs中\ seleniumtest \供应商\ PHPUnit的\ PHPUnit的硒\ PHPUnit的\扩展\ Selenium2TestCase.php:394 C:\ XAMPP \ htdocs中\ seleniumtest \测试\验收\ UserSubscriptionTest.php:66 C:\ XAMPP \ htdocs中\ seleniumtest \测试\验收\ UserSubscriptionTest.php:66 C:\ XAMPP \ htdocs中\ seleniumtest \供应商\ PHPUnit的\ PHPUnit的硒\ PHPUnit的\扩展\ Selenium2TestCase.php:348 C:\ XAMPP \ htdocs中\ seleniumtest \厂商\ PHPUnit的\ PHPUnit的硒\ PHPUnit的\扩展\ Selenium2TestCase.php:314

错误! 测试:1,断言:0,错误:1。

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);
}
相关问题