How to change desired capabilities before the scenario is executed

489 views
Skip to first unread message

Martin Parsiegla

unread,
Jun 9, 2015, 10:46:43 AM6/9/15
to be...@googlegroups.com
Hey there,

I'm using the browser_stack driver in behat3 and I want to change some of the capabilities of the Selenium2Driver before a scenario is executed, to be more accurate, I want to change the name (default value is "Behat feature suite") to the scenario name.

The problem here is, as soon as I call $this->getSession()->getDriver(), the session/driver has already started. 
I could set the desired capabilities and restart the driver, but then there would be an empty test suite in browser stack which is not what I want.

Is there a better way to change some of the desired capabilities before the scenario is executed?

Thanks

Martin

Tominek

unread,
Jul 1, 2015, 8:08:00 AM7/1/15
to be...@googlegroups.com
Hi Martin,

you can specify some additional web driver capabilities in the behat.yml file.

extensions:
    Behat\MinkExtension:
        browser_name: firefox
        sessions:
            default:
                selenium2:
                    wd_host: http://localhost:4444/wd/hub
                    capabilities:
                        browser: firefox
                        version: latest
                        platform: WINDOWS
                        extra_capabilities:
# insert your additional web driver capabilities below
                            enablePersistentHover: true

You should be able to find supported capabilities here >> https://code.google.com/p/selenium/wiki/DesiredCapabilities

Dne úterý 9. června 2015 16:46:43 UTC+2 Martin Parsiegla napsal(a):

Martin Parsiegla

unread,
Jul 1, 2015, 10:55:52 AM7/1/15
to be...@googlegroups.com
Hey Tominek,

thank you for your answer. Sadly this it not what I'm looking for.

I wan to change the desired capabilities before a scenario is executed, so I can not use the behat.yml. However, I found a way to do this, even so it is kind of a hack:

First I created my own mink class 

class MyMink extends Mink
{
}


Then I overwrite the getSession-Method so I can adjust the desired capabilities for my needs:


public function getSession($name = null)
{
    $session = $this->locateSession($name);
    $driver = $session->getDriver();
    if ($driver instanceof Selenium2Driver && !$session->isStarted()) {
        $this->prepareDesiredCapabilities($driver);
    }

    // start session if needed
    if (!$session->isStarted()) {
        $session->start();
    }

    return $session;
}

The prepareDesiredCapabilities-Method adjust the capabilites for my needs, but first I had to find a way to get the current desired capabilites from the Selenium2Driver, since there is no getter and the instance variable is private

private function getDesiredCapabilities(DriverInterface $driver)
{
// see http://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/ for more information
$getDesiredCapabilities = function (Selenium2Driver $driver) {
return $driver->desiredCapabilities;
};
$getDesiredCapabilities = Closure::bind($getDesiredCapabilities, null, $driver);

return $getDesiredCapabilities($driver);
}

And finally I could append my own desired capabilites to the driver

/**
 * @param Selenium2Driver $driver
 */
private function prepareDesiredCapabilities(Selenium2Driver $driver)
{
    $desiredCapabilities = $this->getDesiredCapabilities($driver);
    $desiredCapabilities['build'] = $this->getBuildName();
   
    $driver->setDesiredCapabilities($desiredCapabilities);
}

To get behat to use the newly created mink class, I had to write my own extension which (again) extends the main mink extension class:

class MyMinkExtension extends MinkExtension
{
}

And in the end I use this Extension instead of the main MinkExtension in my behat.yml

default:
  extensions:
    MyMinkExtension:
      base_url: https://www.example.com
      sessions:
        default:
            # ...


I know this way is not perfekt, but it was the only way I found. I hope this might help someone else

Kind Regards
Reply all
Reply to author
Forward
0 new messages