Getting Started

Below you’ll find all needed information to find your way across the library.

Installation

Library can be installed using Composer like so:

  1. define the dependencies in your composer.json:
{
    "require": {
        "aik099/phpunit-mink": "~2.0"
    }
}
  1. install/update your vendors:
$ curl http://getcomposer.org/installer | php
$ php composer.phar install

Basic Usage

  1. sub-class test case class from \aik099\PHPUnit\BrowserTestCase class (line 5)
  2. define used browser configurations in static $browsers property of that class (line 8-21)
  3. access Mink session by calling $this->getSession() method in your test (line 26)
  4. access browser configuration by calling $this->getBrowser() method in your test (line 40)
 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
<?php

use aik099\PHPUnit\BrowserTestCase;

class GeneralTest extends BrowserTestCase
{

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

    public function testUsingSession()
    {
        // This is Mink's Session.
        $session = $this->getSession();

        // Go to a page.
        $session->visit('http://www.google.com');

        // Validate text presence on a page.
        $this->assertTrue($session->getPage()->hasContent('Google'));
    }

    public function testUsingBrowser()
    {
        // Prints the name of used browser.
        echo sprintf(
            "I'm executed using '%s' browser",
            $this->getBrowser()->getBrowserName()
        );
    }

}

Selenium in Cloud

When using Selenium-based solution for automated testing in the cloud (e.g. Sauce Labs or BrowserStack) you need to specify following settings:

  • 'type' => 'saucelabs' or 'type' => 'browserstack'
  • 'apiUsername' => '...'
  • 'apiKey' => '...'

instead of host and port settings. In all other aspects everything will work the same as if all tests were running locally.

 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
<?php

use aik099\PHPUnit\BrowserTestCase;

class BrowserConfigExampleTest extends BrowserTestCase
{

    public static $browsers = array(
        // Sauce Labs browser configuration.
        array(
            'type' => 'saucelabs',
            'apiUsername' => '...',
            'apiKey' => '...',
            'browserName' => 'firefox',
            'baseUrl' => 'http://www.google.com',
        ),
        // BrowserStack browser configuration.
        array(
            'type' => 'browserstack',
            'api_username' => '...',
            'api_key' => '...',
            'browserName' => 'firefox',
            'baseUrl' => 'http://www.google.com',
        ),
        // Regular browser configuration.
        array(
            'driver' => 'selenium2',
            'host' => 'localhost',
            'port' => 4444,
            'browserName' => 'chrome',
            'baseUrl' => 'http://www.google.com',
        ),
    );

}

Continuous Integration

When website under test isn’t publicly accessible, then:

  1. secure tunnel needs to be created from website under test to server, that runs the tests
  2. created tunnel identifier needs to specified in the PHPUNIT_MINK_TUNNEL_ID environment variable

Note

Before v2.1.0 the environment variable was called TRAVIS_JOB_NUMBER.