Unit Testing in JavaScript: Selenium

I’ve described tools like QUnit or JSSpec. They allow us to test JavaScript functions, classes, generally things that works without refreshing the page. But what if we need to check existance of “Log in” page, whether navigation works properly or user is able to modify settings of his account. These actions require loading several pages on different browsers and systems. Here comes Selenium. Suite of tools to automate web app testing across many platforms.
Selenium Components
Selenium consists of three components. Each one has a specific role in aiding the development of web application test automation.
- Selenium IDE – Firefox extension to record test cases and suites.
- Selenium RC – used to run tests on different browsers and systems.
- Selenium Grid – runs at once multiple instances of Selenium RC
In short:
- Selenium IDE makes easier to create Selenium RC test cases
- Selenium RC test cases could be written in multiple programming and scripting languages
- Selenium RC provides Client Libraries for each of mentioned languages
- Selenium RC starts web server that provides API for Client Libraries and Selenium Grid. Selenium API commands are called Selenese
- Selenium Grid manages several Selenium RCs through Selenese
- Selenium Grid server is called Selenium Hub
Selenium IDE
Thanks to this tool, non-programmers are able to generate Selenium RC test cases (we can choose in which language code is generated). Selenium IDE is Firefox extension (available to install from download page).
What you can do in Selenium IDE?
- record test cases or write them manually
- group test cases into test suites
- export and save test suites in every supported language
- find reference of every API command
- debug tests by toggling breakpoints
To fully understand what we’re doing, we must learn few things:
- Selenium IDE locates elements on page (eg search button) using XPath. When recording XPathes are found automatically, but from time to time you’ll have to write them manually. Firebug is especially helpful here.
- Every test consists of commands called Selenese. Every command may be called with maximum two arguments. The first is usually target element pointed by it’s ID, name, link, DOM, CSS or XPath.
- Some commands begin with ‘assert’ or ‘verify’. If assert command fails, test case is aborted. If verify command fails, it fails test and continue to run the test case.
- If command ends with ‘AndWait’, it tells Selenium to wait for the page to load after the action has been done. However, it fails when using AJAX calls. n this case you have to use ‘WaitFor’ commands (read this).
Most commonly used commands are listed below:
open opens a page using a URL. click/clickAndWait performs a click operation, and optionally waits for a new page to load. verifyTitle/assertTitle verifies an expected page title. verifyTextPresent verifies expected text is somewhere on the page. verifyElementPresent verifies an expected UI element, as defined by its HTML tag, is present on the page. verifyText verifies expected text and it’s corresponding HTML tag are present on the page. waitForPageToLoad pauses execution until an expected new page loads. Called automatically when clickAndWait is used. waitForElementPresent pauses execution until an expected UI element, as defined by its HTML tag, is present on the page. Use with AJAX calls.
Here is short tutorial how to record test cases in selenium:
Watch this if you wish to know how to create tests manually:
To learn more about Selenium IDE, go to documentation. Locating techniques and common functions are described here. If you need full reference of Selenium commands, read this.
Selenium RC
is the solution for tests that need more than simple browser actions and linear execution. Selenium-RC uses the full power of programming languages to create more complex tests like reading and writing files, querying a database, and emailing test results.
You’ll want to use Selenium-RC whenever your test requires logic not supported by Selenium-IDE. What logic could this be? For example, Selenium-IDE does not directly support:
- condition statements
- iteration
- logging and reporting of test results
- error handling, particularly unexpected errors
- database testing
- test case grouping
- re-execution of failed tests
- test case dependency
- screenshot capture of test failures
Although these tasks are not supported by Selenium directly, all of them can be achieved by using programming techniques with a language-specific Selenium-RC client library.
Selenium RC comes in two parts.
- A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them.
- Client libraries for your favorite computer language.
If you want to run Selenium RC server on your machine:
- Install Java on your machine
- Download and unpack Selenium RC archive from download page
- Run console and go to selenium-server folder
- Type: java -jar selenium-server.jar
You may configure selenium server. To list available options type:
java -jar selenium-server.jar -help
Now, you’re ready to run tests on particular Client Driver. Each driver requires different steps, so you’d better follow steps on this page.
Selenium Grid
is a tool that dramatically speeds up functional testing of web-apps by leveraging your existing computing infrastructure. It allows you to easily run multiple tests in parallel, on multiple machines, in an heterogeneous enviroment.
If you wish to test Selenium Grid, please follow this instructions.
Setting up Selenium RC on different systems and connecting them using Selenium Grid is pretty complex task. You can make this job easier by buying services that run in cloud. An example is http://saucelabs.com
Disadvantages
Let me quote Google Blog:
Like every large project, it’s not perfect. Selenium is written in JavaScript which causes a significant weakness: browsers impose a pretty strict security model on any JavaScript that they execute in order to protect a user from malicious scripts. Examples of where this security model makes testing harder are when trying to upload a file (IE prevents JavaScript from changing the value of an INPUT file element) and when trying to navigate between domains (because of the single host origin policy problem).
Additionally, being a mature product, the API for Selenium RC has grown over time, and as it has done so it has become harder to understand how best to use it. For example, it’s not immediately obvious whether you should be using “type” instead of “typeKeys” to enter text into a form control. Although it’s a question of aesthetics, some find the large API intimidating and difficult to navigate.
Because of that Google invented WebDriver
Rather than being a JavaScript application running within the browser, it uses whichever mechanism is most appropriate to control the browser. For Firefox, this means that WebDriver is implemented as an extension. For IE, WebDriver makes use of IE’s Automation controls
Now, WebDriver is merging with Selenium. As a result Selenium 2 arised. I’m going to describe this tool in future. Take care.
Useful links:
- Selenium Documentation
- Selenium Grid Home
- Selenium Wiki on Google Code
- Selenium Inspector
- Selenium IDE Tutorial: part1, part2
- Selenium on Rails in 5 minutes
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
May 30, 2010 in JavaScript, Programming, Unit Testing, Web Development | View Comments
-
Vas
-
Member Xoriant QA COE
-
garryd
-
Adam Stankiewicz









