How to test in different browsers with PHPUnit-Selenium

PHPUnit v 3.7 documetation says that we can run same test in different browsers. We have to create a descendant class from PHPUnit_Extensions_SeleniumTestCase with a special variable called $browsers.

There is the following structure in the documentation for $browsers:

public static $browsers = array(
      array(
        'name'    => 'Firefox on Linux',
        'browser' => '*firefox',
        'host'    => 'my.linux.box',
        'port'    => 4444,
        'timeout' => 30000,
      ),
      array(
        'name'    => 'Safari on MacOS X',
        'browser' => '*safari',
        'host'    => 'my.macosx.box',
        'port'    => 4444,
        'timeout' => 30000,
      )
    );

But this structure is NOT correct. Test does not work with this structure!

In spite of PHPUnit documentation the correct structure for $browsers and an addition array $parameters is the following:

class WebTest extends \PHPUnit_Extensions_Selenium2TestCase
{
    // default params
    public $parameters = array(
        'seleniumServerRequestsTimeout' => 30000,
        'timeout'                       => 30000,
    );

    // list of browsers with per-browser config
    public static $browsers = array(
        array('browserName' => 'firefox', 'host'=>'10.216.6.84', 'port'=>4445),
        array('browserName' => 'chrome'),
        array('browserName' => 'internet explorer')
    );

    protected function setUp()
    {
        $this->setBrowserUrl('https://localhost/php/openinghours/');
    }

    function testTitle()
    {
        $this->url('https://localhost/php/openinghours/');
        $this->assertEquals('Opening Hours', $this->title());        
    }
}

Default values for host and ports are localhost and 4444.

How to test

We need Selenium Server. It can be downloaded from here http://docs.seleniumhq.org/download/ in the section Selenium Server (formerly the Selenium RC Server). Server runs from the command line
java -jar c:/lib/selenium/selenium-server-standalone.jar

Chrome

We need a driver for Chrome browser. It can be downloaded from here http://code.google.com/p/chromedriver/downloads/list

We should add the following line to the Selenium Server start command:
-Dwebdriver.chrome.driver=c:/lib/selenium/chromedriver.exe
Where c:/lib/selenium/chromedriver.exe is the path to the Chrome-driver program.
I prefer to have the whole Selenium installation in one place c:\lib\selenium. And it should be in system PATH environment variable.

FireFox

Selenium does not need a special driver to run FireFox. But we have to point Selenium where FireFox is installed. We should add the following line to Selenium start command:
-Dwebdriver.firefox.bin="C:\Program Files (x86)\Mozilla Firefox\firefox.exe"

Internet Explorer

Selenium needs a driver to run tests in IE (or how it is called in docs “Internet Explorer Driver Server”). It can be downloaded from here http://code.google.com/p/selenium/downloads/list. This executable file should be available in system PATH environment variable to work properly.

As I wrote already I prefer to have the whole Selenium in one place c:\lib\selenium and this place is in PATH.

If we get the following error while running IE-driver: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. Then we need
to set equal (either on or off) Protected Mode value value for all time zones.

I we get the following error: Unexpected error launching Internet Explorer. Browser zoom level was set to 107%. It should be set to 100%. Then we have to reset zoom to 100%

After all my bat-fail to start Selenium server looks like this:

javaw -jar c:/lib/selenium/selenium-server-standalone.jar
-Dwebdriver.chrome.driver=c:/lib/selenium/chromedriver.exe
-Dwebdriver.firefox.bin="C:\Program Files (x86)\Mozilla Firefox\firefox.exe"

Note javaw, not java here. This is the same but without console output. Thus probably works faster. Now we are ready. Just start the Selenium server and run tests.

Continuous Integration

I use Windows server for continuous integration. To run Selenium tests on it we have to start Selenium server as Windows service. I did not found any conventional ways to do it unfortunately. In the version Windows Server 2003 and early the were an utility called srvany in the ResKit that could made a service from any executable file. But this utility is not longer available since Windows Server 2008.

However I found remarkable program Non-Sucking Service Manager that does exactly what we need. Downloaded it and put executable nssm.exe somewhere in PATH (c:\windows for example). Then run it from command line nssm install Selenium, pick our Selenium Server bat file and install the service.

Just start the service now and Selenium as always ready for continues integration tests.