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.

  1. Selenium IDE – Firefox extension to record test cases and suites.
  2. Selenium RC – used to run tests on different browsers and systems.
  3. 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.

  1. A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them.
  2. Client libraries for your favorite computer language.

If you want to run Selenium RC server on your machine:

  1. Install Java on your machine
  2. Download and unpack Selenium RC archive from download page
  3. Run console and go to selenium-server folder
  4. 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:


Add to Del.cio.us

RSS Feed

Add to Technorati Favorites

Stumble It!


Digg It!

        www.sajithmr.com

  • Vas
    Great Blog...Great Articles...Great Info

    Thanks buddy...Cheers!

    http://whiteboxqa.com/#seleniu...
  • Member Xoriant QA COE
    As Testing Cost becomes crucial factor for any project, organizations prefer the Open Source Automation Tools instead of investing in costly licensed tools for Web Site Automation Testing. Along with the Zero purchase Cost the Open Source Automation Testing tools also provide competitive features for automating the web applications and portals. Selenium is the only better option for website test automation. Now a days It is becoming more popular and it is the First Choice of Automation Testers for Web GUI. Thanks to FrontendForce and Adam Stankiewicz for providing the detailed information about the set of Selenium tool at one place. I am really inspired by the Title “Unit Testing in JavaScript-Selenium”. This blog is giving an insight that Selenium can also be used for Unit testing of websites in JavaScript. Otherwise I was relating the term Unit Testing with Programming languages like Java, .Net, PHP only. We have our skilled experts to have Customized JUnit Test Suite for unit testing (http://www.xoriant.com/blog/so.... We at Xoriant have been working on Selenium for Automation Testing of Web sites for Pharma and Travel Domains (http://www.xoriant.com/images/... Software Test Automation.pdf).
  • garryd
    I have recently started working with Unit Testing It will be immature on my part to really comment on the post. What i can write about is the explanation that you have provided in the post is very simple to understand and the videos clears the rest of the part.
  • I really appreciate what you said. Thanks :)
blog comments powered by Disqus