Selenium 2 from PHP code

DZone 's Guide to

Selenium 2 from PHP code

· Web Dev Zone ·
Free Resource

Selenium 2, the new iteration of the end-to-end web application testing tool, contains some fancy innovations over its 1.x version: it controls browser directly via the OS, instead of generating JS commands. It's also more reliable than the 1.x as it features a different waiting model.

After trying out Selenium 2, it was natural for me to look for its support in PHP code; however, there isn't an official PHP Api distributed on Selenium's website. I tested all the PHP bindings I could find to see which is the best choice today.

What we need

My goal is to use PHP to write tests for Selenium 2: its browser won't be started directly by a PHP process, but from a local server that exposes its functionality on an HTTP port.

So all these libraries require that a standalone Selenium server is running at the time they are started (unlike for Java-based tests):

[10:36:27][giorgio@Desmond:~]$ java -jar selenium-server-standalone-2.9.0.jar
Nov 15, 2011 10:36:37 AM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server

They also require the curl extension to be enabled in PHP, to communicate with the server. In Ubuntu, make sure to have installed the package php5-curl.

Furthermore, we need a PHP library if we don't want to write tests by sending JSON messages to the server directly...


This extension is shipped with PHPUnit as a PEAR package, and so it's probably already present in your machines. However, it was built for Selenium 1; while Selenium 2 (WebDriver for friends) should be mostly compatible with the old Api, I wouldn't consider it together with PHPUnit_Selenium (just continue to use Selenium 1 jars.)

Facebook's php-webdriver

The Api of these bindings is built with magic methods: it replicates the JSON wire protocol of Selenium 2 with __call(). There are some small exceptions in methods like element() which are explicitly defined to simplify the call:

$session->element('id', 'element_id');
// instead of passing array('using' => ..., 'value' => ...) which won't work

However, some crucial commands like sendKeys are not supported, and value() doesn't really work. It's "a very thin wrapper" but seems incomplete to me.

Moreover, it seems really strangely written, since it says that it gives the option of configuring curl by patching a static class.

Looks like this:

    $webdriver = new WebDriver();
    $session = $webdriver->session('firefox', array());
    $textbox = $session->element('name', 'q');


This project hosted on Google code has a real Api: PHP methods with a signature that can be documented and you can discover via your IDE. It's also a really small download.

It's probably the best Api I encountered: there are even docblocks for all public methods.

Looks like this:

    $webdriver = new WebDriver("localhost", "4444");
    $element = $webdriver->findElementBy(LocatorStrategy::name, "q");
    if ($element) {
        $element->sendKeys(array("php webdriver" ) );


This project integrates with PHPUnit easily: although a class to extend isn't shipped there is an example where setUp() and tearDown() can be borrowed from your test suite.

In fact, the project includes sample test for Selenium, and it's unit tested! The other tests you see in the source are targeted to its own production code.

The library includes some stuff for running tests on the Sauce Labs cloud, so there is a company behind this code. The only pitfall is that the code features strage conventions like method_name(); it's also not namespaced, like all the other projects.

Looks like this:

    $this->driver = WebDriver_Driver::InitAtLocal("4444", "firefox");
    $this->assert_title("Selenium - Web Browser Automation");
    $this->get_element("css=h2")->assert_text("What is Selenium?");


You have lots of (maybe too many) choices:

  • plain old PHPUnit: you'll stick to the Selenium 1 Api.
  • Facebook's php-webdriver: you'll get a library which mirrors the original protocol and magically adds methods.
  • php-webdriver-bindings: a small library, just a few kilobytes, which implements explicit methods instead of relying on __call(). Probably the best Api.
  • WebDriver-PHP: when you may want to run tests in the cloud with Sauce Labs, and to use a library written by people with lots of experience in dealing with Selenium.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}