Selenium 2 (WebDriver) and Phpunit?
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.
Paul R Rogers
Passionate about delivering usable solutions, on time and with TLC
Updated on March 19, 2020Comments
-
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 over 13 yearsFor the time being I've settled on using Java for tests. Java's Dbunit is much faster than the PHP port anyway.
-
Stéphane almost 13 yearsThere is also this other Php binding library. Both libraries seem to be rather active at the moment.
-
Adil over 12 yearsThanks for pointing to this facebook wrapper. It seems like a lot of commands aren't working such as sendKeys
-
cmc over 12 yearsHey 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 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 over 12 years@Tgr: Please kindly refer to the part "Some unavoidable exceptions to direct protocol translation." in the readme.
-
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
fromWebDriverBase
, which callsgetHTTPMethod
with the function name (possibly removingget
/post
/delete
from it).getHTTPMethod
throws an exception if the command name is not found in the array returned by themethods()
function of the class, which is just a static list of known commands. -
cmc over 12 years
-
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 about 12 yearsSelenium support comes with PHPUnit: phpunit.de/manual/3.6/en/selenium.html
-
bcoughlan about 12 yearsPHPUnit 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 over 11 yearsQuick update - phpunit does now support Selenium 2 phpunit.de/manual/3.6/en/selenium.html
-
Eric Cope over 11 yearswhy does an IDE need to be installed?
-
falsch about 11 yearsLast updated for two years. Use the instaclick/webdriver package instead!
-
Paul R Rogers almost 8 yearsTimely 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.