1

我使用 phpunit 作为硒的包装器。我有一个模拟同一网站上的两个用户的测试。所以我需要打开两个不能共享cookies的浏览器——它不能只是两个窗口。它们是相同的测试,例如,一个用户会在第一个浏览器实例中单击某些东西,而另一个用户会在另一个浏览器实例中寻找变化。退出并以其他用户身份重新登录不是一种选择。

有没有办法做到这一点?

4

1 回答 1

1

Disclaimer: I haven't tried this at all, but the pattern might work.

Unfortunately the PHPUnit WebDriver implementation is tightly coupled to the unit test framework code. However, you could try something like this in order to have 2 different web driver instances running in parallel:

<?php
class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
    private $driver1;
    private $driver2;

    protected function setUp()
    {
        $this->driver1 = $this->createDriver();

        $this->driver2 = $this->createDriver();
    }

    protected function createDriver()
    {
        $driver = new PHPUnit_Extensions_Selenium2TestCase();
        $driver->setBrowser('firefox');
        $driver->setBrowserUrl('http://www.example.com/');
        $driver->start();

        return $driver;
    }

    public function testTitle()
    {
        $this->driver1->url('http://www.example.com/');
        $this->driver1->assertEquals('Example WWW Page', $this->title());

        $this->driver2->url('http://www.example.com/');
        $this->driver2->assertEquals('Example WWW Page', $this->title());
    }

    protected function tearDown() {
        $this->driver1->stop();
        $this->driver2->stop();
    }
}
?>

Theres quite a lot that could potentially go wrong with this but you could try it.

Alternatively you could ditch the PHPUnit integration for this particular test/tests and use a dedicate PHP WebDriver API like PHP-SeleniumClient, which would give you better control over the WebDriver instances.

于 2013-11-08T19:10:23.590 回答