Automation Tools - Selenium 101
Author: Roman Tverdokhlebov
Reviewer: Brian Rock
Introduction: This course describes the basics of Selenium, its components and how to start work with it. A few simple examples are provided for better understanding.
Background: Web applications (applications that are accessed via a web browser over a network such as the Internet or an intranet) are used throughout now and it's completely impossible to overestimate their role in our life. Many applications (like CRM systems, office apps and many other) became online.
Essential advantages of web-applications are: you can access them from everywhere, your data in most cases is stored on the server and there is no reason to worry about its safety and accessibility, you need not download, install and setup anything on your computer in order to make use of them.
Main disadvantages of web-applications are: they are rendered by a browser, so it's difficult to ensure the same layout for all the users, a lot of people interact with a server at the same time, data transferred between a client and a server can be trapped or substituted, user can affect application functionality and interface changing browser settings, small hole in application security may result in great loss for many people.
So it is very important to put great efforts into web apps testing! There are a lot of different tools for web apps testing automation, but Selenium can be recommended because of:
- It's free
- It evolves and grows
- There are different ways to make and run test cases: record and run using Selenium IDE, record and run using Cubic Test, record, refactor and run using Java IDE, use Selenium Core, WebDriver or Selenium Grid to run your tests etc;
- Recorded test cases from Selenium IDE can be translated to different programming languages: Java, Ruby, PHP, .NET, Perl, Python.
- It supports a lot of platforms and browsers - http://seleniumhq.org/about/platforms.html.
Main Selenium components are:
- Selenium IDE is a Firefox add-on that makes it easy to record and playback tests in Firefox 2+. You can even use it generate code to run the tests with Selenium Remote Control.
- Selenium Remote Control is a client/server system that allows you to control web browsers locally or on other computers, using almost any programming language and testing framework.
- Selenium Grid takes Selenium Remote Control to another level by running tests on many servers at the same time, cutting down on the time it takes to test multiple browsers or operating systems.
- CubicTest is a graphical Eclipse plug-in for writing Selenium and Watir tests. It makes web tests faster and easier to write, and provides abstractions to make tests more robust and reusable.
- Bromine is a web-based QA tool for selenium that enables you to easily run selenium RC tests and view the results. It scales beautifully from the single tester that just wants to run some tests without all the hassle to the corporate solution with multiple user-groups and hundreds of test cases.
- Selenium on Rails provides an easy way to test Rails application with Selenium Core. Suite files are created on the fly for each directory and the Selenium Core files don't have to pollute /public.
- Selenium on Ruby is the hub for newer Ruby related Selenium projects .
The following test cases will be used to demonstrate the basic capabilities of Selenium:
- Verification that the Selenium site can be found via Google;
- Verification that the uTest site can be found via Google.
The simplest way to automate web application test case is to record it using Selenium IDE for FireFox. So only two tools are required:
- After Selenium IDE installation navigate to the Tools menu and select Selenium IDE option. Recording will be started automatically.
- Type in address bar - http://www.google.com and press Enter. You'll see that in your recorder in Base URL input field address http://www.google.com/ appeared.
- Then type Selenium into search input field and press Google Search button. Do not press Enter, Selenium IDE does not recognize it. Select address seleniumhq.org appeared after the first search result, right mouse click and select verifyTextPresent seleniumhq.org. Press red button marked “Step 1”. Now we almost completed automation of the first test case!
First column contains Selenium commands. Select any command to see its short description. Second column contains targets. For command “open” target is URL to be browsed, for commands “type” and “click” argument “target” contains element id. Third column contains optional values, e.g., string to be entered into an input field.
Test suite forming
It looks like there is only one single test case, but actually there is a test suite with one test case. To see test suite press vertical separator or create new test case using menu File:
Having created an empty test case and recorded two test scenarios in the same fashion, we will get the test suite with two test cases. Next, let’s execute it.
Execution & debug
There are two ways to execute test cases: with IDE and with Selenium TestRunner.
Select first test case “Selenium” and click “Play current test case” button to execute it. In most cases test failed. Why? Because page has not been loaded completely and verification was performed when body text wasn't received and parsed. So we should modify test case and insert one more command. Select the last command “verifyTextPresent”, click option “Insert new command” in context menu and fill in appropriate data to improve the test case:
Target = “30000” means that we should wait until page load, but not more than 30 seconds. Run test again, it should succeed.
In Selenium IDE you can toggle breakpoint, set speed of the execution, run particular commands, or walk step by step through test script:
Using “Play with Selenium Test Runner” button it is possible to execute test cases in much more informative way. You can review all your test scripts, see test execution, time spent and test results in one single window:
Having saved recorded test scripts, you’ll get an HTML file like the following:
It isn’t very useful since you can’t use it anywhere except Selenium IDE. But you can transform test scripts to different programming languages. Just navigate to Options menu, select Format option and choose suitable format:
Exported test scripts can be executed outside Selenium IDE using Selenium RC, e.g., in case of Java in Eclipse or as standalone set of JUnit tests, but this theme is out of scope for the current course. Hope you enjoyed this introduction to Selenium course!
1) Selenium HQ - http://seleniumhq.org/
2) Selenium Projects - http://seleniumhq.org/projects/
3) Selenium IDE - http://seleniumhq.org/projects/ide/
4) Selenium intro - http://seleniumhq.org/movies/intro.mov
5) Selenium in Wikipedia - http://en.wikipedia.org/wiki/Selenium_(software)
6) Selenium Tutorial - http://www.jroller.com/selenium/