PHPUnit Selenium configuration with a twist

PHPUnit Selenium configuration with a twist

The PHPUnit manual shows how to define a set of browsers for a specific unit test like this:

class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
    public static $browsers = array(
        array(
            'name'    => 'Firefox on Linux',
            'browser' => '*firefox',
            'host'    => 'my.linux.box',
            'port'    => 4444,
            'timeout' => 30000,
        ),
        array(
            'name'    => 'Internet Explorer on Windows XP',
            'browser' => '*iexplore',
            'host'    => 'my.windowsxp.box',
            'port'    => 4444,
            'timeout' => 30000,
        ) 
    );
}


This makes sense when using a single selenium server per browser. When using Selenium Grid as a central orchestration engine it is rather tedious to repeatedly define host and port for each browser configuration:

class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
    public static $browsers = array(
        array(
            'name'    => 'Firefox on Linux',
            'browser' => '*firefox',
            'host'    => 'selenium-hub.box',
            'port'    => 4444,
            'timeout' => 30000,
        ),
        array(
            'name'    => 'Internet Explorer on Windows XP',
            'browser' => '*iexplore',
            'host'    => 'selenium-hub.box',
            'port'    => 4444,
            'timeout' => 30000,
        ) 
    );
}


By digging deeper into the PHPUnit source code I could see that SeleniumTestSuite does not only accept a static property $browser but also a callable which in the end lead to this piece of code:

class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
    public static function browsers()
    {
        $config = $this->getBrowserConfigForTestCase(__CLASS__);
        foreach($config as $key => $value) {
            $config[$key]['host'] = 'selenium-hub.box';
            $config[$key]['port'] = 4444;
            $config[$key]['timeout'] = 30000;
        }

        return $config;
    }
}


In a "perfect world" you want make sure that host, port and timeout are configurable ;)


Eintrag von Stephan Hochdörfer am 05.05.2015

Tags: PHPUnit, Selenium

Diese Webseite verwendet Cookies, um die Bedienfreundlichkeit zu erhöhen. Mit der Nutzung unserer Webseite wird das Einverständnis erklärt, dass wir Cookies verwenden. Weitere Informationen.