Selenium 2.0 webdriver
Selenium 2.0 - is a tool for automated functional testing, which includes two previously independent projects - Selenium Remote Control (Selenium RC) and webDriver (Selenium WD). Selenium 2.0 allows you to use all the tools like Selenium WebDriver, and Selenium RC. Selenium developers recommend to use Selenium webDriver, as Selenium RC was officially deprecated. While working at Selenium 2.0 they mainly developed Selenium WebDriver and its compatibility with Selenium RC. Selenium RC itself was not changed much.
So, what are the differences between Selenium WebDriver and Selenium RC and where innovation is?
The thing is in the way of interaction with the browser:
- Selenium RC sends commands to the browser using JavaScript Selenium Core.
- WebDriver works with the browser through native browser interface.
On the development stage Selenium RC developers have chosen the right decision to make their tool more cross-browser through the use of JavaScript Selenium Core.
Using JavaScript in Selenium Core also caused certain limitations, such as:
- impossiblity to create a flexible universal tool applicable to such a variety of competing browsers
- limitations on performance caused by universality
It was clear that the next step in Selenium development was to fit the tool for each of the major browsers.
It provided:
- Productivity increasing through the use of native interface compared to the universal JavaScript Selenium Core
- More accurate emulation of users actions while working with a browser
To summarize, Selenium WebDriver works with every browser. It improves the stability of the tests (since they are "sharpened" to a specific browser), tests become easier to write and maintain, increases the speed of their work. Selenium WebDriver uses native commands (the most complete simulation of users actions), which is an important advantage over the Selenium RC.
There is a number of WebDrivers for the major browsers in the moment, which are in different readiness stages:
- FirefoxDriver (3.0, 3.5, 3.6, 4.0, 5.0, 6, 7, 8, 9, 10 and beginning with ver. 2.15 partially 11)
- InternetExplorerDriver (6, 7, 8, 9 - 32 and 64-bit where applicable)
- ChromeDriver (12.0.7, 12.0 +); OperaDriver (11.5 +)
- OperaDriver (11.5 +)
- SafariDriver - currently is not available
- HtmlUnitDriver (2.9), cross-platform driver on java, no need to install any browser
- AndriodDriver (2.3 + for phones and tablets (devices & emulators) IphoneDriver)
- iOS 3 + for phones (devices & emulators) and 3.2 + for tablets (devices & emulators)
Languages (bindings) officially supported in Selenium WebDriver are:
- C #
- Java
- Ruby
- Python
It should be noted that JsonWireProtocol is freely available now, currently in draft status. In the covering document states that all applications that use WebDriver to work with browser should use the same wire protocol (transport). This protocol defines common logic of Web Services using JSON through HTTP. The protocol is linear and is implemented as a pair of request/response "commands" and "answers". It assumes that using this protocol is possible to implement work with WebDriver using any object oriented language.
At the moment of writing this article there are several implementations using PHP and Perl. In this article I would like to review the option of working with WebDriver using:
PHP -> JsonWireProtocol -> Selenium-Server (WebDriver) -> Browser
Selenium - is a tool(java-application) for test automation of web applications. Complexity is various, ranging from simple HTML forms to heavy RIA and Flex applications. Speaking more technical language Selenium Server is HTTP-server written in Java(based on Jetty). When in running condition it listens the broadcast on port 4444 and tries to do the received commands. Including Selenium Remote Contol, Selenium Remote webDriver and theirs API for different languages.
To run the automated test Selenium webDriver + PHP we need take several steps:
Step 1 - install JDK
or the functioning of Selenium Server we need JDK (Java Development Kit) version 5 and above.
Packages for installation can be found at:
http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u1-download-513651.html
or installed from the repository:
$ sudo add-apt-repository "deb http://archive.canonical.com/ubuntu lucid partner" $ sudo apt-get update $ sudo apt-get install sun-java6-jre sun-java6-plugin sun-java6-fonts
Check if it was installed properly and the version of Java is right:
$ Java -version
You should see a similar result as in the following screenshot:
Step 2 - install PHP
Since we will use PHP as a language to work with Selenium API, we need to launch PHP interpreter version at least PHP5.3 on your platform. It will be explained later why we should do it. To do this, we should download the binary archive PHP5 (*. zip) of the latest stable version, on download page of the official PHP website http://www.phpunit.de.
Installing PHP from the repository:
$ Apt-get install php5
Check the version of php:
$ Php - v
the result is something like:
Php version should not be below 5.2
Step 3 - PHP5-CURL
Since we will use JsonWireProtocol and work with the Selenium Server 2.0 as http server, we need a package PHP5-CURL.
For setup it:
$ Sudo apt-get install php5-curl
To check:
$ Php-i | grep curl
The result should be similar to the:
Step 4 - install PEAR
Selenium API is compatible with many test frameworks such as JUnit, Test:: Unit (Ruby), TestNG (Java), etc. Similar compatibility list can be found on the official website, and basically their presence is not required for normal system operation, but it is very convenient and necessary if we want to create something functional, worthwhile and time saving. Due to the fact that PHP language was chosen to communicate with the Selenium API, we will use the PHPUnit which can be launched using manual from the official resource(http://www.phpunit.de). We use phpUnit version 3.6 which require the presence of PHP5.3
To install it you will need to do following:
First, install PEAR.
$sudo apt-get install php-pear
Next, tell PEAR to update its own channel.
$sudo pear channel-update pear.php.net
Then, tell PEAR to upgrade itself to the newest version.
$sudo pear upgrade-all
You should now have the newest version of PEAR installed.
To install PHPUnit, let PEAR know where to find PHPUnit.
$sudo pear channel-discover pear.phpunit.de
Then install PHPUnit. (the -a makes sure all dependency packages are also installed)
$sudo pear install -a phpunit/PHPUnit
Update:
According to the latest PHPUnit installation documentation, you can install PHPUnit with the following commands (make sure you have updated PEAR first):
$sudo pear config-set auto_discover 1 $sudo pear install pear.phpunit.de/PHPUnit
If everything went well, check for correct installation of phpUnit:
$ PhpUnit - version
the result is something like:
Step 5 - Selenium Server
Next we need Selenium Server itself. It can be found on the official website (http://seleniumhq.org/download/), we use the latest version 2.14.0 on the moment of writing the tutorial . This version supports both scripts style Selenium RC (Remote Control) and Selenium WD (webDriver).
JAR archive (note that the jar file can be unpacked, but it’s absolutely useless) contains an API for different programming languages and Selenium Server, as well as Selenium RC (Remote Control) and Selenium WD (webDriver) hereinafter referred to as webDriver.
Step 6 - PHP wrapper
Our next step is a wrapper that provides the connection between the PHP script and Selenium Server. There is no PHP wrapper, because PHP is not officially supported. However there are several third-party developments (Third Party Language Bindings NOT SUPPORTED / DEVELOPED by seleniumhq) which are available for free.
- https://github.com/chibimagic/WebDriver-PHP/
- http://code.google.com/p/php-webdriver-bindings/
- https://github.com/facebook/php-webdriver
With the appropriate corrections you can develop your own wrapper. We'll discuss it in a later part of our tutorial. In this tutorial we will use the PHP wrapper which is available from the following repositories:
$ Git clone https://github.com/chibimagic/WebDriver-PHP.git
Then run the selenium server, go to the directory where Selenium Server was executed and run following console command:
$ Java-jar selenium-server-standalone-2.14.0.jar
The following output should be seen:
By default launched Selenium Server expects to receive commands on localHost port 4444. After receiving command on start of a browser the session is created. All further activity is based on the request and response form, based on browser responses within the session frame. PHP wrapper JsonWireProtocol provides an object-oriented approach for work with webDriver./p>
Lets write and start our first automated test
There is an example of such test in assembly we have received from the repository: https://github.com/chibimagic/WebDriver-PHP.git
A detailed description of how to construct a test using the webDriver will be described in next tutorial. In current tutorial our main task is to make sure, that tests are started and successfully performed.
test code listing the SampleTest.php (this file is present in repository):
require_once 'WebDriver.php';
require_once 'WebDriver/Driver.php';
require_once 'WebDriver/MockDriver.php';
require_once 'WebDriver/WebElement.php';
require_once 'WebDriver/MockElement.php';
class SampleTest extends PHPUnit_Framework_TestCase {
protected $driver;
public function setUp() {
$this->driver = WebDriver_Driver::InitAtLocal("4444", "firefox");
}
// Forward calls to main driver
public function __call($name, $arguments) {
if (method_exists($this->driver, $name)) {
return call_user_func_array(array($this->driver, $name), $arguments);
} else {
throw new Exception("Tried to call nonexistent method $name with arguments:\n" . print_r($arguments, true));
}
}
// test body
public function test() {
$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?");
$this->get_element("id=q")->send_keys("webdriver");
$this->get_element("id=submit")->click();
$first_result = $this->get_element("css=a.gs-title")->get_text();
}
// end test body
public function tearDown() {
if ($this->driver) {
if ($this->hasFailed()) {
$this->driver->set_sauce_context("passed", false);
} else {
$this->driver->set_sauce_context("passed", true);
}
$this->driver->quit();
}
parent::tearDown();
}
}
Next make sure that Selenium Server is running and is in standby mode Then go to the directory where our webDriver-PHP is deployed from the repository.
We than run our test using phpunit:
$ Phpunit SampleTest.php
The following output should be seen:
After this we must wait when Selenium Server launch browser, and start user simulation actions in real time. If users simulation actions are correct, we must see next console screen:
The dot indicates the correct execution of the script. In any other case you will see F - as erroneous script execution and print error exception php or phpunit.
So, in this tutorial we have understood basic principles of work with Selenium webDriver using JSON through HTTP (JsonWireProtocol) that is using Selenium webDriver with PHP. Selenium webDriver with PHP was installed as well. Selenium is one of the few tools that make possible to develop functional test as close as possible to users actions. In next tutorial, I would like to write about the nuances of working with JsonWireProtocol using PHP framework and problem-solving interactions of synchronous and asynchronous methods.