Selenium 2 (WebDriver) and Phpunit?

26,438

Solution 1

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:

<?php

require '/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.

Solution 2

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.

Solution 3

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.

Solution 4

PHPUnit Selenium integration code lives as a separate project in github, as far as I can see it does not support Selenium 2, so the answer to your question would be - No, you can not use Selenium 2 with PHPUnit.

But you can clone the source tree and make it work with Selenium 2.

Solution 5

We created a library for that, I hope it helps. It also uses the JSON Wire protocol but we aimed to make it compatible with the examples from other languages, so the syntax would be very similar. Here's the link: https://github.com/Nearsoft/PHP-SeleniumClient

If you like it, share it, improve it or fork it :)

Regards, Mark.

Share:
26,438
Paul R Rogers
Author by

Paul R Rogers

Passionate about delivering usable solutions, on time and with TLC

Updated on March 19, 2020

Comments

  • Paul R Rogers
    Paul R Rogers about 4 years

    Any one know how to use Selenium 2 with Phpunit? Are there any Selenium 2 samples in PHP?

  • Paul R Rogers
    Paul R Rogers over 13 years
    For the time being I've settled on using Java for tests. Java's Dbunit is much faster than the PHP port anyway.
  • Stéphane
    Stéphane almost 13 years
    There is also this other Php binding library. Both libraries seem to be rather active at the moment.
  • Adil
    Adil over 12 years
    Thanks for pointing to this facebook wrapper. It seems like a lot of commands aren't working such as sendKeys
  • cmc
    cmc over 12 years
    Hey Adil! You mean lots of commands aren't working in the other PHP selenium implementations, right? That's just another reason to use this one, the architecture inherently makes it complete.
  • Tgr
    Tgr over 12 years
    @cmc: There is nothing in @php-webdriver@ which would make it "inherently complete" (thought it seems fairly easy to add new commands). There is a static list of accepted commands (see the @getMethod()@ functions in the various classes).
  • cmc
    cmc over 12 years
    @Tgr: Please kindly refer to the part "Some unavoidable exceptions to direct protocol translation." in the readme.
  • Tgr
    Tgr over 12 years
    @cmc: that is besides the point. It may be a direct translation, but it is not an automatic one. WebDriverElement, WebDriverSession etc. inherit __call from WebDriverBase, which calls getHTTPMethod with the function name (possibly removing get/post/delete from it). getHTTPMethod throws an exception if the command name is not found in the array returned by the methods() function of the class, which is just a static list of known commands.
  • cmc
    cmc over 12 years
  • cmc
    cmc over 12 years
    ... half-kidding with the last comment of course. What I'm saying is: It's real easy to add new commands because the author made everything else automatic and consistent. So it's simply false to say there is "nothing inherently complete" about the framework, because everything is, except the command list and the occasional syntactic sugar. If that really really itches you, just go and clone the thing and give the author a pull request... for me almost automatic is good enough.
  • bcoughlan
    bcoughlan about 12 years
    Selenium support comes with PHPUnit: phpunit.de/manual/3.6/en/selenium.html
  • bcoughlan
    bcoughlan about 12 years
    PHPUnit has its own Selenium library which better mimics the JsonWireProtocol webdriver bindings (phpunit.de/manual/3.6/en/selenium.html). If you want to use the WebDriver one, Facebook's one is more actively supported.
  • Polsonby
    Polsonby over 11 years
    Quick update - phpunit does now support Selenium 2 phpunit.de/manual/3.6/en/selenium.html
  • Eric Cope
    Eric Cope over 11 years
    why does an IDE need to be installed?
  • falsch
    falsch about 11 years
    Last updated for two years. Use the instaclick/webdriver package instead!
  • Paul R Rogers
    Paul R Rogers almost 8 years
    Timely as I just recently retried the semi-official phpunit-selenium to find the docs are still lacking. Though, getting this one working reliably is also a challenge.