I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 635 posts at DZone. You can read more from them at their website. View Full User Profile

Selenium 2 from PHP code

11.15.2011
| 17941 views |
  • submit to reddit

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...

PHPUnit_Selenium

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());
    $session->open('http://www.google.com');
    $textbox = $session->element('name', 'q');
    $textbox->value('h');

php-webdriver-bindings

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");
    $webdriver->connect("firefox");
    $webdriver->get("http://google.com");
    $element = $webdriver->findElementBy(LocatorStrategy::name, "q");
    if ($element) {
        $element->sendKeys(array("php webdriver" ) );
        $element->submit();
    }

WebDriver-PHP

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->set_implicit_wait(5000);
    $this->load("http://seleniumhq.org/");
    $this->assert_title("Selenium - Web Browser Automation");
    $this->get_element("css=h2")->assert_text("What is Selenium?");

Conclusions

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.
Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Gonzalo Ayuso replied on Tue, 2011/11/15 - 2:43pm

Interesting. Now: Selenium2 or Zombie.js. I'm playing with Zombie.js and it's really good. I need to have a look to new the new version of Selenium too.

Jakub Zalas replied on Tue, 2011/11/15 - 4:59pm

There's another PHP library, called PHP-Selenium.

 It's integrated into Mink and therefore can be used with Behat scenarios.

Giorgio Sironi replied on Wed, 2011/11/16 - 4:57am

I'll check it out, but it seems for Selenium 1... :)

Giorgio Sironi replied on Wed, 2011/11/16 - 5:00am in response to: Gonzalo Ayuso

We tried Zombie.js, but it was too difficult to synchronize the steps of the test with (for example) the end of an Ajax call.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.