Selenium 2 (WebDriver) and Phpunit? Selenium 2 (WebDriver) and Phpunit? selenium selenium

Selenium 2 (WebDriver) and Phpunit?


Quick update: phpunit does now support Selenium 2


At the time of writing, PHPUnit does not support Selenium 2.

php-webdriver from facebook allows the complete WebDriver API to be called from PHP in an elegant way. To quote:

Most clients require you to first read the protocol to see what's possible, then study the client itself to see how to call it. This hopes to eliminate the latter step.

It is used by starting up the Selenium 2 server, which provides the interface at localhost:4444/wd/hub.

/usr/bin/java -jar /path/to/selenium-server-standalone-2.7.0.jar

then running the PHP test code, which calls that interface. For example:

<?phprequire '/path/to/php-webdriver/__init__.php';$webdriver = new WebDriver();$session = $webdriver->session('opera', array());$session->open("http://example.com");$button = $session->element('id', 'my_button_id');$button->click();$session->close();

The WebDriver API is mapped to PHP methods, compare calling click on element in the example with the element/click API call in the documentation.

The test code can then be wrapped in regular phpUnit tests.

This is not native phpUnit support, but it's a quite robust approach.


please look at the http://code.google.com/p/php-webdriver-bindings/ . This is PHP library that communicates with Selenium Webdriver server using JsonWireProtocol. This is early version but it works.


Currently (2017) I recommend using php-webdriver, what is AFAIK the most feature complete PHP language binding to interact with Selenium WebDriver.

This library was rewritten in 2014 to support Selenium 2, and its API is mostly based on the official Java WebDriver bindings. This means you can also take advantage of code examples written in Java, as they can be usually simply followed in PHP. Its also written in a modern OOP way and follows standard PSR-4 namespaces and also PSR-2 coding standards.

I would recommend this library over phpunit-selenium - as it was originally designed for Selenium 1 (though it nowadays support Selenium 2) and its API is strongly tight to PHPUnit. It also does not support some of the WebDriver features and is not up-to-date with upcomin W3C WebDriver specification.

Php-webdriver is on the other hand independent library, but its integration with PHPUnit is quite simple - or you can use existing tools like Steward, which includes all the PHPUnit integration and provide also nice convenience layer and eg. allow to simply run multiple tests in parallel (without a need of another tools like paratest).

There are also other testing framework integration options mentioned on the project homepage.