Options for running test automation cases

There are multiple options concerning the test setup and the location of the actual browser when running test cases and using webdriver. The tests can be run either locally on the machine running the test script or remotely, on a separate computer from the machine running the test script.

Webdriver refers here to both watir- and selenium-webdriver. As described in one of the earlier blog posts, watir-webdriver is an API on top of selenium-webdriver, it is using the same underlying code to control the browsers, which means that the same methods for running tests apply to both.

Next I'll give a quick overview of what is meant by a local vs. remote test in this context. The whole post is about Selenium 2, which supports test automation with webdriver.

Executing a test locally

Running tests locally is the "default" method of executing webdriver tests. It works in a similar way to running a test with Watir or Selenium IDE. You issue the command to start the tests, a browser starts on your local machine and your test cases control the browser in your local machine.

The following code can be used to start a local test:

require 'watir-webdriver'

browser = Watir::Browser.new :firefox

All subsequent requests initiated by the tests are then issued to the browser running on your current machine. Instead of firefox, one can also use chrome or internet_explorer if the test so requires.
The option to run tests locally is useful in many circumstances. It can be used, for example, when:

  • the tests are being implemented and it is important to see how each step works
  • debugging tests during maintenance
  • you want to see the browser to look for any layout problems
Executing tests through Selenium 2 / Grid 2

While the local option is useful in many cases, there is also the option for remotely executing the tests. Some of it's use cases are:

  • the test are hooked into a continuous integration system and are executed on a schedule (the tests could still run locally in this case, but typically not)
  • tests need to be executed on multiple platforms
  • tests need to be run in parallel to speed up the total time taken

Selenium hub

Selenium server can also be used in multiple ways. Each node can be started as a hub or as a node. A hub is a central point in a Selenium Grid 2 system, which has the following tasks:

  • maintain a list of nodes connected to it
  • maintain a list of browsers that the connected nodes provide
  • listen to client requests for test automation tasks
  • allocate client requests to available nodes and thus browsers

Webdriver node

Nodes on the other hand:

  • listen to requests coming from the hub, which originate from a test automation client
  • start a local browser instance serving those client requests

Nodes can be started with command line options to specify how many of each browser type can be executed in parallel on the machine. For more options, see Grid2 Wiki page

The last missing piece is then how to start a browser in the test script so that it connects to the Selenium server. Here is a code sample showing one way to do it:

require 'watir-webdriver'

capabilities = Selenium::WebDriver::Remote::Capabilities.new
capabilities.browser_name = :firefox

browser = Watir::Browser.new( 
    :url => "http://<server IP>:4444/wd/hub", 
    :desired_capabilities => capabilities

In this case, again a Firefox instance is started. However, this time it's not started on the local machine, but on the Selenium server specified by the IP address / host name in the :url parameter, or when the Grid2 feature is used, on the next node that has a firefox instance available.