Thursday, November 13, 2014
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).
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.
Speed


Real-life Interaction


API

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

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 BrowsersRemember 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

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.


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.

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.

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).


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.

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.

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.

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.

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.
Browser | Name of Driver Server | Remarks |
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 Explorer | Internet Explorer Driver Server | Available in 32 and 64-bit versions. Use the version that corresponds to the architecture of your IE |
Chrome | ChromeDriver | Though 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 |
Opera | OperaDriver | Though its name is just “OperaDriver”, it is in fact a Driver Server, not just a driver. |
PhantomJS | GhostDriver | PhantomJS is another headless browser just like HTMLUnit. |
Safari | SafariDriver | Though its name is just “SafariDriver”, it is in fact a Driver Server, not just a driver. |
You can download these driver servers here
Summary
- Aside from a browser, you will need the following to start using WebDriver
- Java Development Kit (JDK). http://www.oracle.com/technetwork/java/javase/downloads/index.html
- Eclipse IDE - http://www.eclipse.org/downloads/
- Java Client Driver - http://seleniumhq.org/download/
- When starting a WebDriver project in Eclipse, do not forget to import the Java Client Driver files onto your project. These files will constitute your Selenium Library.
- HTMLUnit and Firefox are the only browsers that you can automate without the use of a Driver Server.
- Each other browser has its own driver server.
Subscribe to:
Post Comments (Atom)
Search
Popular Posts
-
Sample Selenium IDE Test Script by Recording: We will use the Mercury Tours website as our web application under test. It is an online flig...
-
Locating GUI Elements Locating elements in WebDriver is done by using the “findElement(By. locator ())” method. The “locator” part of the co...
-
Answer 75 : If you want to see all options available while starting Selenium server then you should use option “-h” while starting Seleni...
-
Answer 60 : While using java you need to create instance of DefaultSelenium class and pass it four parameters – selenium = ne...
-
Answer 51 : Selenium RC is an offering from SeleniumHQ which tries to overcome following draw backs of Selenium IDE – Able to execute...
-
Answer 54 : Selenium core is the core JS engine of Selenium which executes tests on browser, but because of same origin policy it needs t...
-
Answer 18 : You need to use context menu to add check points to your Selenium IDE tests Answer 19 : There are two ways to edit tests...
-
Answer 15 : Yes, you can first record individual test cases and then group all of them in a test suite. Following this entire test suite ...
-
Answer 38 : You could XPath checker - https://addons.mozilla.org/en-US/firefox/addon/xpath-checker/ to test you XPath locators and Fi...
-
Answer 44 : You can change default behavior of Selenium IDE > element locator preference by crating JS file with following– Locato...
Shaik. Powered by Blogger.
This comment has been removed by a blog administrator.
ReplyDelete