简体   繁体   English

PHPUnit与Selenium2-共享会话不起作用

[英]PHPUnit with Selenium2 - share session not working

I'm doing some tests with PHPUnit and Selenium and i would like all of them to run in the same browser window. 我正在使用PHPUnit和Selenium进行一些测试,我希望它们都可以在同一浏览器窗口中运行。

I've tried starting the Selenium Server with 我试过用以下命令启动Selenium Server

java -jar c:\php\selenium-server-standalone-2.33.0.jar -browserSessionReuse

but with no visible change. 但没有明显的变化。

I've also tried with shareSession() in the setup 我也尝试在设置中使用shareSession()

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

but the only change is that it opens a window for every test, and not really sharing the session. 但是唯一的变化是,它为每个测试打开了一个窗口,并且没有真正共享会话。 I'm out of ideas at this point. 我现在还没主意。

My tests look like this: 我的测试如下所示:

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

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

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

Here's the elegant solution. 这是一个优雅的解决方案。 To share browser sessions in Selenium2TestCase , you must set sessionStrategy => 'shared' in your initial browser setup: 要在Selenium2TestCase共享浏览器会话,必须在初始浏览器设置中将sessionStrategy => 'shared'设置为:

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

The alternative (default) is 'isolated' . 替代方法(默认)是'isolated'

You do not need to use the flag -browserSessionReuse In your case The set up function running before every test and starting new instance. 您无需使用-browserSessionReuse标志。在这种情况下,设置函数在每次测试之前运行并启动新实例。 This is what i did to prevent this to happen (Its little bit ugly but work for me both in Windows and Ubuntu): 这是我为防止这种情况所做的工作(虽然有点丑陋,但在Windows和Ubuntu中都对我有用):

  1. I created helper class with static ver: $first and initialized it. 我创建了带有静态ver:$ first的帮助器类,并将其初始化。 helper.php: helper.php:

     <?php class helper { public static $first; } helper::$first = 0; ?> 
  2. Edit main test file setUp() function(and add require_once to helper.php): 编辑主测试文件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 ; } } .... 

setHost and setPort outside the if because the values restarted after each test(For me...) and need to set up every time (if the selenium server is not localhost:4444) setHost和setPort在if外,因为值在每次测试后重新启动(对于我来说...),并且每次都需要设置(如果selenium服务器不是localhost:4444)

Just found an (much) faster way to proceed : If you perform several test in one function, all test are performed in the same window. 刚刚找到了一种(快得多的)更快的方法:如果您在一个功能中执行多个测试,则所有测试都在同一窗口中执行。 The setback is that the tests and reporting won't be nicely presented by tests, but the speed is way up! 挫折是测试无法很好地呈现测试和报告,但是速度提高了!

In the same function for each test just use: 在每个测试的相同功能中,只需使用:

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

Or 要么

$this->back();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM