Configuring Browser

The browser needs to be configured in a test case before being able to access Mink session. All possible ways of browser configuration are described below.

Per Test Configuration

It is possible to configure browser individually for each test within a test case by creating an instance of \aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration class in setUp method in of test case class and setting it via setBrowser method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

use aik099\PHPUnit\BrowserTestCase;

class PerTestBrowserConfigTest extends BrowserTestCase
{

    protected function setUp()
    {
        // To create regular browser configuration via BrowserConfigurationFactory.
        $browser = $this->createBrowserConfiguration(array(
            // options goes here (optional)
        ));

        // To create "Sauce Labs" browser configuration via BrowserConfigurationFactory.
        $browser = $this->createBrowserConfiguration(array(
            // required
            'type' => 'saucelabs',
            'apiUsername' => 'sauce_username',
            'apiKey' => 'sauce_api_key',
            // optional options goes here
        ));

        // To create "BrowserStack" browser configuration via BrowserConfigurationFactory.
        $browser = $this->createBrowserConfiguration(array(
            // required
            'type' => 'browserstack',
            'api_username' => 'bs_username',
            'api_key' => 'bs_api_key',
            // optional options goes here
        ));

        // Options can be changed later (optional).
        $browser->setHost('selenium_host')->setPort('selenium_port')->setTimeout(30);
        $browser->setBrowserName('browser name')->setDesiredCapabilities(array(
            'version' => '6.5'
        ));
        $browser->setBaseUrl('http://www.test-host.com');

        // Set browser configuration to test case.
        $this->setBrowser($browser);

        parent::setUp();
    }

}

Per Test Case Configuration

In case, when all tests in a test case share same browser configuration it’s easier to specify it via static $browsers property (array, where each item represents a single browser configuration) in that test case class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php

use aik099\PHPUnit\BrowserTestCase;

class PerTestCaseBrowserConfigTest extends BrowserTestCase
{

    public static $browsers = array(
        array(
            'driver' => 'selenium2',
            'host' => 'localhost',
            'port' => 4444,
            'browserName' => 'firefox',
            'baseUrl' => 'http://www.google.com',
        ),
        array(
            'driver' => 'selenium2',
            'host' => 'localhost',
            'port' => 4444,
            'browserName' => 'chrome',
            'baseUrl' => 'http://www.google.com',
        ),
    );

}

Note

When several browser configurations are specified in $browsers array, then each test in a test case will be executed against each of browser configurations.

Browser Session Sharing

As a benefit of shared (per test case) browser configuration, that was described above is an ability to not only share browser configuration, that is used to create Mink session, but to actually share created sessions between all tests in a single test case. This can be done by adding sessionStrategy option (line 14) to the browser configuration.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php

use aik099\PHPUnit\BrowserTestCase;

class CommonBrowserConfigTest extends BrowserTestCase
{

    public static $browsers = array(
        array(
            'driver' => 'selenium2',
            'host' => 'localhost',
            'port' => 4444,
            'browserName' => 'firefox',
            'baseUrl' => 'http://www.google.com',
            'sessionStrategy' => 'shared',
        ),
    );

}

Selecting the Mink Driver

With the help of driver and driverOptions browser configuration settings (since v2.1.0) it’s possible to specify which Mink driver to use. This file demonstrates how to use each driver:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

use aik099\PHPUnit\BrowserTestCase;

class DriverShowCaseTest extends BrowserTestCase
{

    public static $browsers = array(
        array(
            'driver' => 'goutte',

            // Defaults for this driver.
            'driverOptions' => array(
                'server_parameters' => array(),
                'guzzle_parameters' => array(),
            ),

        ),
        array(
            'driver' => 'sahi',

            // Defaults for this driver.
            'port' => 9999,
            'driverOptions' => array(
                'sid' => null,
                'limit' => 600,
                'browser' => null,
            ),
        ),

        array(
            'driver' => 'selenium2',

            // Defaults for this driver.
            'port' => 4444,
            'driverOptions' => array(),
        ),

        array(
            'driver' => 'zombie',

            // Defaults for this driver.
            'port' => 8124,
            'driverOptions' => array(
                'node_bin' => 'node',
                'server_path' => null,
                'threshold' => 2000000,
                'node_modules_path' => '',
            ),
        ),
    );

}

Configuration Options

Each browser configuration consists of the following settings (all optional):

Name Description
driver Mink driver name (defaults to selenium2, since v2.1.0)
driverOptions Mink driver specific options (since v2.1.0)
host host, where driver’s server is located (defaults to localhost)
port port, on which driver’s server is listening for incoming connections (determined by driver)
timeout connection timeout of the server in seconds (‘selenium2’ driver only, defaults to 60)
browserName name of browser to use (e.g. firefox, chrome, etc., defaults to firefox)
desiredCapabilities parameters, that allow to fine-tune browser and other ‘selenium2’ driver options (e.g. ‘tags’, ‘project’, ‘os’, ‘version’)
baseUrl base url of website, that is tested
sessionStrategy used session strategy (defaults to isolated)
type type of configuration (defaults to default, but can also be saucelabs or browserstack)
apiUsername API username of used service (applicable to ‘saucelabs’ and ‘browserstack’ browser configurations)
apiKey API key of used service (applicable to ‘saucelabs’ and ‘browserstack’ browser configurations)

There are also corresponding setters (e.g. setHost) and getters (e.g. getHost) for each of mentioned above settings, that allow to individually change them from setUp method before test has started.