Selenium 2 (WebDriver) and Phpunit?

Paul R Rogers picture Paul R Rogers · Nov 17, 2010 · Viewed 26.2k times · Source

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

Answer

cmc picture cmc · Oct 4, 2011

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.