Php webdriver - 如何强制新测试使用不同的配置文件


Php webdriver - how to force new test to use different profile?

我正在使用动态创建的 Firefox 配置文件在包含多个节点的 Selenium 网格上运行多个测试,如下所示:

$firefoxProfile = new FirefoxProfile();
$capabilities = DesiredCapabilities::firefox ();
$capabilities->setCapability(FirefoxDriver::PROFILE, $firefoxProfile);
$this->webdriver = RemoteWebDriver::create("http://my.tests.com", $capabilities, 5000);

但是,每次集线器选取具有先前运行的 Firefox 实例的节点时,它都会使用相同的配置文件并删除先前运行的会话。发生这种情况是因为应用程序使用相同的 cookie 进行身份验证。

有没有办法强制硒网格即时创建新的配置文件并获得一个全新的 Firefox 实例?

一些进一步的信息

为了启动集线器,我目前使用以下命令行

    java -jar /opt/selenium/selenium-server.jar -trustAllSSLCertificates -timeout  300 '
                                        -role hub -newSessionWaitTimeout 60 -maxSession 2 '
                                        -port 9444 -nodeTimeout 300 '
                                        -browserTimeout 300 &

为了启动节点,我使用

    xvfb-run -n 99 --server-args="-screen 0 800x600x16 -ac"  '
      -a java -jar /opt/selenium/selenium-server.jar -role node '
                 -browser browserName=firefox,maxInstances=2 '
                 -hub http://my.tests.com:9444/grid/register 

奇怪的是,当我设置一个独立的Selenium服务器时,它会创建多个Firefox实例,就像我想要的那样......

我正在硒网格上运行多个测试,其中包含 多个节点,使用动态创建的 Firefox 配置文件,如下所示

你保护你的变量吗?这就像你重用了某个类实例。 spl_object_hash()可以在这里帮助你。它

返回对象的唯一标识符

对于给定实例,这始终相同。

附注:

尝试将它们分开并使用单元测试/使用 PHPUnit 中可用的夹具/:

class BaseTestCase extends PHPUnit_Framework_TestCase
{
    static $driver;
    private $capabilities;
    public function setCapabilities($capabilities)
    {
        $this->capabilities = $capabilities;
    }
    public static function setUpBeforeClass()
    {
        $host = 'http://my.tests.com';
        self::$driver = RemoteWebDriver::create($host, $this->capabilities, 5000);
    }
    public static function tearDownAfterClass()
    {
        self::$driver->close();
    }
    public function getDriver()
    {
        return self::$driver;
    }
}
class FirefoxTest extends BaseTestCase
{
    public function setUp()
    {
        $firefoxProfile = new FirefoxProfile();
        $capabilities = DesiredCapabilities::firefox ();
        $capabilities->setCapability(FirefoxDriver::PROFILE, $firefoxProfile);
        self->setCapabilities($capabilities);
        $this->getDriver()->get("http://my.tests.com/x");
    }
    public function testTitle()
    {
        echo $this->getDriver()->getTitle();
    }
    public function testSomethingElse()
    {
        // do test
    }
}

这个例子不会在FirefoxTest和XXXTest之间共享相同的$driver,但建议这样做,因为您需要为每个测试提供一个干净的石板。

但是,FirefoxTest 中的所有测试都将共享相同的驱动程序。当你做"phpunit测试"时,测试的执行顺序将是: setUpBeforeClass()

setUp()
testTitle()
setUp()
testSomethingElse()
tearDownAfterClass() 

关于夹具的更多信息

你也可以尝试另一种轻量级的硒替代品,称为Selenoid。主要区别在于它在新的 Docker 容器中启动每个浏览器。这可以保证您的会话是完全隔离的。