Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

phpunit webdriver bindings are hosted on google code. There is something we need to understand beyond this.

  1. PHPUnit needs to be installed. (Either through PEAR package or download and install manually)
  2. PHP IDE such as Eclipse PDT has to be downloaded and installed.
  3. Selenium-Stand-Alone server has to be running while executing the WebDriver Selenium test

I wrote a tutorial about how to use Selenium 2, Facebook wrapper, find it here:

http://testigniter.blogspot.co.uk/2012/01/running-selenium-2-webdriver-using.html

I recommened the usage of Menta, a Selenium 2 Framework which requires WebDriver. Both packages are PSR-0 compatible, so you can use them with Composer. You can configure selenium with the phpunit.xml. Here an example

<phpunit bootstrap="tests/bootstrap.php"
         backupGlobals="false" backupStaticAttributes="false"
         strict="true" verbose="true">
    <php>
        <var name="testing.selenium.seleniumServerUrl" value="http://localhost:4444/wd/hub" />
        <var name="testing.selenium.browser" value="firefox" />
        <var name="testing.selenium.windowPosition" value="0,0" />
        <var name="testing.selenium.windowSize" value="1280x1024" />
        <var name="testing.selenium.windowFocus" value="1" />
        <var name="testing.selenium.timeoutImplicitWait" value="10000" />
    </php>
    <testsuites>
        <testsuite name="Integrationstests">
            <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">tests/integration</directory>
        </testsuite>
    </testsuites>
    <logging>
        <log type="junit" target="build/logs/junit.xml"/>
    </logging>
</phpunit>

The bootstrap file reads the configuration variables from testing.selenium.*, so you can easily set new variables.

<?php

\Menta_ConfigurationPhpUnitVars::addConfigurationFile(__DIR__ . '/../phpunit.xml');

$configuration = \Menta_ConfigurationPhpUnitVars::getInstance();
\Menta_SessionManager::init(
    $configuration->getValue('testing.selenium.seleniumServerUrl'),
    $configuration->getValue('testing.selenium.browser')
);

Now you can easily implement you testcases. Here an example

<?php

namespace tests\integration;

use WebDriver\LocatorStrategy;

class TestSearch extends \PHPUnit_Framework_TestCase
{
    public function testGoogle()
    {
        $session = \Menta_SessionManager::getSession();
        $session->open('http://www.google.de');
        $element = $session->element(LocatorStrategy::NAME, 'q');
        $this->assertTrue($element->displayed());
    }
}

The Menta Package also have two demo files, located here

Today a took a deep jump into Selenium and phpunit. It is possible and you may find some examples and instructions over here: http://phpunit.de/manual/current/en/selenium.html

Creator of phpunit got some nice examples of the api. With a little experimentation and reading the error message, you'll get along. Havn't found a great library myself as well.

https://github.com/sebastianbergmann/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php

As last a tutorial with from nettuts which helps you along the basics: http://net.tutsplus.com/tutorials/php/how-to-use-selenium-2-with-phpunit/

Yes, Selenium 2 (WebDriver) and PHPUnit tests is simple. But May I want to gave you advice that the first you should be try Selenium IDE because you need to expect in selenium command. If you are expect in Selenium command if so selenium 2 (Webdriver) and PHPUnit test will be more simple for you.

You can try selenium IDE tutorials on here and you can learn selenium 2 (Webdriver) and PHPUnit at here.

I work on selenium2php. I have too many tests for Selenium1 recorded with Selenium IDE. Now it converts html tests into Selenium2. Actually, for PHPUnit_Extensions_Selenium2TestCase. I am going to implement more commands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top