Skip to content

Set custom capabilities

Ondřej Machulda edited this page Apr 2, 2017 · 5 revisions

"Capabilities" describes desired aspects of the browser where tests should be run. For example platform name, browser windows size, whether insecure SSL certificates should be accepted etc. - see W3C WebDriver specification.

Capabilities could only be specified before the browser is started - so you cannot modify them from inside the test (or from setUp() method), because the browser is already instantiated.

Steward provides several options how to define custom capabilities - each option is suitable for different use-case.

Command line (CLI) options

ℹ️ Available since Steward 2.0.

This is the simplest solution. You pass the capability you want to use to the run command by --capability option in a format --capability=capabilityName:capabilityValue. Some examples:

# Simple capability
$ ./vendor/bin/steward run staging firefox --capability="screenResolution:1280x800"

# Multiple capabilities
$ ./vendor/bin/steward run staging firefox --capability="platform:Windows 10" --capability="screenResolution:1280x800"

# Browser version MUST be defined as a string - encapsulate it into quotes so it is not casted to a number
$  ./vendor/bin/steward run staging microsoftedge --capability="version:'14.14393'"

CLI options are most usable when you just need to pass a simple option or when the options differs each run (for example you could have separate test runs for Windows 10 and Windows 7 - so you just change the CLI option of each job on you continuous integration server).

Extend CapabilitiesResolver to change capabilities for specific browser

ℹ️ Available since Steward 2.2.

If you need more complex logic (or when defining many command line options makes the run command definition to complicated) or when you need some global capabilities which don't differ between runs, you can extend CapabilitiesResolver class.

The CapabilitiesResolver has some prepared points of extension. Say you want to pass additional capability to all you Edge browser builds - you can just override setupMicrosoftEdgeCapabilities() method of CapabilitiesResolver like this:

<?php
// MyCapabilitiesResolver.php

namespace My\Selenium;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Lmc\Steward\Selenium\CapabilitiesResolver;

class MyCapabilitiesResolver extends CapabilitiesResolver
{
    protected function setupMicrosoftEdgeCapabilities(DesiredCapabilities $capabilities)
    {
        $capabilities = parent::setupMicrosoftEdgeCapabilities($capabilities);

        $capabilities->setCapability('pageLoadStrategy', 'eager');

        return $capabilities;
    }
}

Note there are similar prepared method for all supported browsers.

Last step is to configure Steward to use your adjusted capabilities resolver class using steward.yml configuration file:

# steward.yml
capabilities_resolver: My\Selenium\MyCapabilitiesResolver

Extend CapabilitiesResolver to change capabilities for all runs

ℹ️ Available since Steward 2.2.

In case you want to define some capability not based on browser name, but simply for all cases, you can do this by overriding setupCustomCapabilities() method:

<?php
// MyCapabilitiesResolver.php

namespace My\Selenium;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\WebDriverCapabilityType;
use Lmc\Steward\Selenium\CapabilitiesResolver;

class MyCapabilitiesResolver extends CapabilitiesResolver
{
    protected function setupCustomCapabilities(DesiredCapabilities $capabilities, $browser)
    {
        $capabilities = parent::setupCustomCapabilities($capabilities, $browser);

        $capabilities->setCapability(WebDriverCapabilityType::ACCEPT_SSL_CERTS, false);

        return $capabilities;
    }
}

Also don't forget to configure Steward to use your adjusted capabilities resolver class using steward.yml configuration file:

# steward.yml
capabilities_resolver: My\Selenium\MyCapabilitiesResolver
Clone this wiki locally