【问题标题】:Selenium with PHPUnit not loading pageSelenium 与 PHPUnit 不加载页面
【发布时间】:2017-05-07 00:13:25
【问题描述】:

我正在尝试使用 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\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\tests\acceptance\UserSubscriptionTest.php:66 C:\xampp\htdocs\seleniumtest\tests\acceptance\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。

【问题讨论】:

    标签: selenium selenium-webdriver phpunit


    【解决方案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);
    }
    

    【讨论】:

    • 非常感谢。你的回答真的很有帮助。不知道为什么 $this->setBrowserUrl('localhost/cwckids/web/user/login');没有设置网址,但您的解决方案工作正常。我也喜欢截图的想法。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多