PHPUnit与Selenium2 - 共享会话无法正常工作

时间:2013-06-11 14:38:36

标签: unit-testing selenium phpunit

我正在使用PHPUnit和Selenium进行一些测试,我希望所有这些测试都在同一个浏览器窗口中运行。

我尝试用

启动Selenium Server
java -jar c:\php\selenium-server-standalone-2.33.0.jar -browserSessionReuse

但没有明显的变化。

我也在设置

中尝试使用shareSession()
public function setUp()
{
    $this->setHost('localhost');
    $this->setPort(4444);
    $this->setBrowser('firefox');
    $this->shareSession(true);
    $this->setBrowserUrl('http://localhost/project');
}

但唯一的变化是它为每个测试打开一个窗口,而不是真正共享会话。我现在没有想法。

我的测试看起来像这样:

public function testHasLoginForm()
{
    $this->url('');

    $email = $this->byName('email');
    $password = $this->byName('password');

    $this->assertEquals('', $email->value());
    $this->assertEquals('', $password->value());
}

3 个答案:

答案 0 :(得分:3)

这是优雅的解决方案。要在Selenium2TestCase中共享浏览器会话,您必须在初始浏览器设置中设置sessionStrategy => 'shared'

public static $browsers = array(
    array(
        '...
        'browserName' => 'iexplorer',
        'sessionStrategy' => 'shared',
        ...
    )
);

备选方案(默认)为'isolated'

答案 1 :(得分:2)

您不需要使用标志-browserSessionReuse 在您的情况下,设置功能在每次测试之前运行并启动新实例。 这就是我为防止这种情况所做的事情(它有点难看,但在Windows和Ubuntu中都适用于我):

  1. 我用静态ver:$ first创建了helper类并初始化它。 helper.php:

    <?php
    class helper
    {
        public static $first;
    }
    helper::$first = 0;
    ?>
    
  2. 编辑主测试文件setUp()函数(并将require_once添加到helper.php):

    require_once "helper.php";
    
    class mySeleniumTest extends PHPUnit_Extensions_SeleniumTestCase
    {
    
            public function setUp()
            {
                    $this->setHost('localhost');
                    $this->setPort(4444);
                    if (helper::$first == 0 )
                    {
                            $this->shareSession(TRUE);
                            $this->setBrowser('firefox');
                            $this->setBrowserUrl('http://localhost/project');
                            helper::$first = 1 ;
                    }
            }
    ....
    
  3. setHost和setPort在if之外,因为每次测试后重新启动的值(对我而言......)并且每次都需要设置(如果selenium服务器不是localhost:4444)

答案 2 :(得分:0)

刚刚找到一种(更快)的方法:如果在一个函数中执行多个测试,则所有测试都在同一个窗口中执行。挫折是测试和报告不会很好地通过测试呈现,但速度快了!

在每个测试的相同功能中,只需使用:

$this->url('...');

$this->back();
相关问题