Thursday, November 13, 2014

On 11:39 AM by Unknown in ,    1 comment
Introduction to WebDriver & Comparison with Selenium RC

Hope you have learned to create simple tests in Selenium IDE, we shall now create more powerful scripts using an advanced tool called WebDriver.
What is WebDriver?

WebDriver is a web automation framework that allows you to execute your tests against different browsers, not just Firefox (unlike Selenium IDE).

1


WebDriver also enables you to use a programming language in creating your test scripts(not possible in Selenium IDE).

You can now use conditional operations like if-then-else or switch-case

You can also perform looping like do-while.

Following programming languages are supported by WebDriver

  • Java

  • .Net

  • PHP

  • Python

  • Perl

  • Ruby


You do not have to know all of them. You just need to be knowledgeable in one.However, in this tutorial, we will be using Java with Eclipse as our IDE.
WebDriver Vs Selenium RC

Before advent of WebDriver in  2006, there was another, automation tool called Selenium Remote Control. Both WebDriver and Selenium RC have following features:

  • They both allow you to use a programming language in designing your test scripts.

  • They both allow you to run your tests against different browsers.


So how do they differ? Let us discuss the answers.

Architecture

WebDriver’s architecture is simpler than Selenium RC’s.

  • It controls the browser from the OS level

  • All you need are your programming language’s IDE (which contains your Selenium commands) and a browser.


Selenium RC’s architecture is way more complicated.

  • You first need to launch a separate application called Selenium Remote Control (RC) Server before you can start testing

  • The Selenium RC Server acts as a “middleman” between your Selenium commands and your browser

  • When you begin testing, Selenium RC Server “injects” a Javascript program called Selenium Core into the browser.

  • Once injected, Selenium Core will start receiving instructions relayed by the RC Server from your test program.

  • When the instructions are received, Selenium Core will execute them as Javascript commands.

  • The browser will obey the instructions of Selenium Core, and will relay its response to the RC Server.

  • The RC Server will receive the response of the browser and then display the results to you.

  • RC Server will fetch the next instruction from your test script to repeat the whole cycle.


2



Speed

3WebDriver is faster than Selenium RC since it  speaks directly to the browser uses the browser’s own engine to control it.

4Selenium RC is slower  since it uses a JavaScript program called Selenium Core. This Selenium Core is the one that directly controls the browser, not you.
Real-life Interaction

5WebDriver interacts with page elements in a more realistic way. For example, if you have a disabled text box on a page you were testing, WebDriver really cannot enter any value in it just as how a real person cannot.

6Selenium Core, just like other Javascript codes, can access disabled elements .In the past, Selenium testers complain that Selenium Core was able to enter values to a disabled text box in their tests.Differences in API
API

7Selenium RC’s API is more matured but contains redundancies and often confusing commands. For example, most of the time, testers are confused whether to use type or typeKeys; or whether to use click, mouseDown, or mouseDownAt. Worse, different browsers interpret each of these commands in different ways too!

WebDriver’s API is simpler than Selenium RC’s. It does not contain redundant and confusing commands.
Browser Support

8WebDriver can support the headless HtmlUnit browser.

HtmlUnit is termed as “headless” because it is an invisible browser – it is GUI-less.

It is a very fast browser because no time is spent in waiting for page elements to load. This accelerates your test execution cycles. Since it is invisible to the user, it can only be controlled through automated means.

Selenium RC cannot support the headless HtmlUnit browser. It needs a real, visible browser to operate on.

Limitations of WebDriver

WebDriver cannot Readily Support New Browsers
Remember that WebDriver operates on the OS level. Also remember that different browsers communicate with the OS in different ways. If a new browser comes out, it may have a different process of communicating with the OS as compared to other browsers. So, you have to give the WebDriver team quite some time to figure that new process out before they can implement it on the next WebDriver release.

However, it is up to the WebDriver’s team of developers to decide if they should support the new browser or not.

Selenium RC Has Built-In Test Result Generator
9
Selenium RC automatically generates an HTML file of test results. The format of the report was pre-set by RC itself. Take a look at an example of this report below.

WebDriver has no built-in command that automatically generates a Test Results File. You would have to rely on your IDE’s output window, or design the report yourself using the capabilities of your programming language and store it as text, html, etc.

10Screenshot_1

 Installing Selenium WebDriver


In this tutorial we will install WebDriver (Java only) and Configure Eclipse

Step 1 – Install Java on your computer, Download and install the Java Software Development Kit (JDK) here.

11

This JDK version comes bundled with Java Runtime Environment (JRE) so you do not need to download and install the JRE separately.

Step 2 – Install Eclipse IDE

Download “Eclipse IDE for Java Developers” here. Be sure to choose correctly between Windows 32 Bit and 64 Bit versions.

12

You should be able to download a ZIP file named “eclipse-java-juno-SR1-win32-x86_64.zip” (the version number “SR1” may change over time).

13Inside that ZIP file, there is an “eclipse” folder which contains all the application files. You can extract the “eclipse” folder anywhere you want in your PC; but for this tutorial, extract it to your C drive.

14Unlike  other popular software , no installation is required to use eclipse.

Step 3 – Download the Selenium Java Client Driver

You can download the Selenium Java Client Driver here. You will find client drivers for other languages there, but only choose the one for Java.

15

This download comes as a ZIP file named “selenium-2.25.0.zip”. For simplicity, extract the contents of this ZIP file on your C drive so that you would have the directory “C:\selenium-2.25.0\”. This directory contains all the JAR files that we would later import on Eclipse.

Step 4 – Configure Eclipse IDE with WebDriver

Launch the “eclipse.exe” file inside the “eclipse” folder that we extracted in step 2. If you followed step 2 correctly, the executable should be located on C:\eclipse\eclipse.exe.

When asked to select for a workspace, just accept the default location.

16

Create a new project through File > New > Java Project. Name the project as “myproject”.

Right-click on the newly created project and select New > Package, and name that package as “mypackage”.

Create a new Java class under mypackage by right-clicking on it and then selecting New > Class, and then name it as “myclass”. Your Eclipse IDE should look like the image below.

17

Right-click on myproject and select Properties.

On the Properties dialog, click on “Java Build Path”.

Click on the Libraries tab, and then click “Add External JARs..”

Navigate to C:\selenium-2.25.0\ (or any other location where you saved the extracted contents of “selenium-2.25.0.zip” in step 3).

Add all the JAR files inside and outside the “libs” folder. Your Properties dialog should now look similar to the image below.

18

Finally, click OK and we are done importing Selenium libraries into our project.
Different Drivers

HTMLUnit and Firefox are two browsers that WebDriver can directly automate – meaning that no other separate component is needed to install or run while the test is being executed. For other browsers, a separate program is needed. That program is called as the Driver Server.

A driver server is different for each browser. For example, Internet Explorer has its own driver server which you cannot use on other browsers. Below is the list of driver servers and the corresponding browsers that use them.











































BrowserName of Driver ServerRemarks
HTMLUnit(none)WebDriver can drive HTMLUnit without the need of a driver server
Firefox(none)WebDriver can drive Firefox without the need of a driver server
Internet ExplorerInternet Explorer Driver ServerAvailable in 32 and 64-bit versions. Use the version that corresponds to the architecture of your IE
ChromeChromeDriverThough its name is just “ChromeDriver”, it is in fact a Driver Server, not just a driver. The current version can support versions higher than Chrome v.21
OperaOperaDriverThough its name is just “OperaDriver”, it is in fact a Driver Server, not just a driver.
PhantomJSGhostDriverPhantomJS is another headless browser just like HTMLUnit.
SafariSafariDriverThough its name is just “SafariDriver”, it is in fact a Driver Server, not just a driver.

You can download these driver servers here
Summary

1 comment: