Configuring Browser

The browser needs to be configured in a test case before being able to access the Mink session. All possible ways of the 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 the \aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration class in the setUpTest method of test case class and setting it through the setBrowser method.

 1<?php
 2
 3use aik099\PHPUnit\BrowserTestCase;
 4
 5class PerTestBrowserConfigTest extends BrowserTestCase
 6{
 7
 8    /**
 9     * @before
10     */
11    protected function setUpTest()
12    {
13        // Creates the regular browser configuration using "BrowserConfigurationFactory".
14        $browser = $this->createBrowserConfiguration(array(
15            // options goes here (optional)
16        ));
17
18        // Creates the "Sauce Labs" browser configuration using "BrowserConfigurationFactory".
19        $browser = $this->createBrowserConfiguration(array(
20            // required
21            'type' => 'saucelabs',
22            'apiUsername' => 'sauce_username',
23            'apiKey' => 'sauce_api_key',
24            // optional options goes here
25        ));
26
27        // Creates the "BrowserStack" browser configuration using "BrowserConfigurationFactory".
28        $browser = $this->createBrowserConfiguration(array(
29            // required
30            'type' => 'browserstack',
31            'apiUsername' => 'bs_username',
32            'apiKey' => 'bs_api_key',
33            // optional options goes here
34        ));
35
36        // Options can be changed later (optional).
37        $browser->setHost('selenium_host')->setPort('selenium_port')->setTimeout(30);
38        $browser->setBrowserName('browser name')->setDesiredCapabilities(array(
39            'version' => '6.5'
40        ));
41        $browser->setBaseUrl('http://www.test-host.com');
42
43        // Set browser configuration to the test case.
44        $this->setBrowser($browser);
45
46        parent::setUpTest();
47    }
48
49}

Per Test Case Configuration

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

 1<?php
 2
 3use aik099\PHPUnit\BrowserTestCase;
 4
 5class PerTestCaseBrowserConfigTest extends BrowserTestCase
 6{
 7
 8    public static $browsers = array(
 9        array(
10            'driver' => 'selenium2',
11            'host' => 'localhost',
12            'port' => 4444,
13            'browserName' => 'firefox',
14            'baseUrl' => 'http://www.google.com',
15        ),
16        array(
17            'driver' => 'selenium2',
18            'host' => 'localhost',
19            'port' => 4444,
20            'browserName' => 'chrome',
21            'baseUrl' => 'http://www.google.com',
22        ),
23    );
24
25}

Note

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

Browser Session Sharing

As a benefit of the shared (per test case) browser configuration, that was described above is an ability to not only share the 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 the sessionStrategy option (line 15) to the browser configuration.

 1<?php
 2
 3use aik099\PHPUnit\BrowserTestCase;
 4
 5class CommonBrowserConfigTest extends BrowserTestCase
 6{
 7
 8    public static $browsers = array(
 9        array(
10            'driver' => 'selenium2',
11            'host' => 'localhost',
12            'port' => 4444,
13            'browserName' => 'firefox',
14            'baseUrl' => 'http://www.google.com',
15            'sessionStrategy' => 'shared',
16        ),
17    );
18
19    /**
20     * @before
21     */
22    public function setUpTest()
23    {
24        parent::setUpTest();
25
26        if ( $this->getSessionStrategy()->isFreshSession() ) {
27            // login once before any of the tests was started
28        }
29    }
30
31    public function testOne()
32    {
33        // user will be already logged-in regardless
34        // of the test execution order/filtering
35    }
36
37    public function testTwo()
38    {
39        // user will be already logged-in regardless
40        // of the test execution order/filtering
41    }
42
43    /**
44     * @inheritDoc
45     */
46    public function onTestSuiteEnded()
47    {
48        $session = $this->getSession(false);
49
50        if ( $session !== null && $session->isStarted() ) {
51            // logout once after all the tests were finished
52        }
53
54        return parent::onTestSuiteEnded();
55    }
56
57}

Selecting the Mink Driver

With the help of the driver and the 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<?php
 2
 3use aik099\PHPUnit\BrowserTestCase;
 4
 5class DriverShowCaseTest extends BrowserTestCase
 6{
 7
 8    public static $browsers = array(
 9        array(
10            'driver' => 'goutte',
11
12            // Defaults for this driver.
13            'driverOptions' => array(
14                'server_parameters' => array(),
15                'guzzle_parameters' => array(),
16            ),
17
18        ),
19        array(
20            'driver' => 'sahi',
21
22            // Defaults for this driver.
23            'port' => 9999,
24            'driverOptions' => array(
25                'sid' => null,
26                'limit' => 600,
27                'browser' => null,
28            ),
29        ),
30
31        array(
32            'driver' => 'selenium2',
33
34            // Defaults for this driver.
35            'port' => 4444,
36            'driverOptions' => array(),
37        ),
38
39        array(
40            'driver' => 'zombie',
41
42            // Defaults for this driver.
43            'port' => 8124,
44            'driverOptions' => array(
45                'node_bin' => 'node',
46                'server_path' => null,
47                'threshold' => 2000000,
48                'node_modules_path' => '',
49            ),
50        ),
51    );
52
53}

Configuration Options

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

Name

Description

driver

Mink driver name (defaults to the selenium2, since v2.1.0)

driverOptions

Mink driver specific options (since v2.1.0)

host

host, where driver’s server is located (defaults to the localhost)

port

port, on which driver’s server is listening for the incoming connections (determined by the driver)

timeout

connection timeout of the server in seconds (‘selenium2’ driver only, defaults to 60)

browserName

name of the browser to use (e.g. firefox, chrome, etc., defaults to the firefox)

desiredCapabilities

parameters, that allow to fine-tune browser and other selenium2 driver options (e.g. tags, project, os, version)

baseUrl

base url of the website, that is tested

sessionStrategy

used session strategy (defaults to isolated)

type

type of the configuration (defaults to default, but also can be saucelabs or browserstack)

apiUsername

API username of the used service (applicable to the saucelabs and browserstack browser configurations)

apiKey

API key of the 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 the mentioned above settings, that allow to individually change them from the setUpTest method before test has started.