PHPUnit Selenium configuration with a twist
This blog post was published more than one year ago and might be outdated!
· 2 min read
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 ;)