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

时间:2016-03-01 02:09:48

标签: php selenium webdriver

我正在使用动态创建的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进行身份验证。

有没有办法强制selenium网格动态创建一个新的配置文件并获得一个全新的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实例,因为我希望...

2 个答案:

答案 0 :(得分:1)

您还可以尝试使用名为Selenoid的替代轻量级Selenium替代品。主要区别在于它启动了新Docker容器中的每个浏览器。这可以保证您的会话完全隔离。

答案 1 :(得分:0)

  

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

你保护你的变量吗?这就像你重用了一些类实例。 spl_object_hash()可以帮到你。它

  

返回对象的唯一标识符

对于给定的实例始终是相同的。

PS:

尝试将它们分开&使用unittests /使用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之间共享相同的$驱动程序,但建议这样做,因为您需要为每个测试提供一个干净的平板。

然而,FirefoxTest 中的所有测试将共享相同的驱动程序。 当你进行'phpunit tests'时,测试的执行顺序将是:     setUpBeforeClass()

setUp()
testTitle()

setUp()
testSomethingElse()

tearDownAfterClass() 

更多关于fixtures