# First Steps with Selenium RC

November 2010

## Selenium 1.0 Testing Tools: Beginner’s Guide

 Test your web applications with multiple browsers using the Selenium Framework to ensure the quality of web applications Save your valuable time by using Selenium to record, tweak and replay your test scripts Get rid of any bugs deteriorating the quality of your web applications Take your web applications one step closer to perfection using Selenium tests Packed with detailed working examples that illustrate the techniques and tools for debugging

(For more resources on this subject, see here.)

# Important preliminary points

To complete the examples of this article you will need to make sure that you have at least Java JRE installed. You can download it from http://java.sun.com. Selenium Remote Control has been written in Java to allow it to be cross platform, so we can test on Mac, Linux, and Windows.

# What is Selenium Remote Control

Selenium IDE only works with Firefox so we have only been checking a small subsection of the browsers that our users prefer. We, as web developers and testers, know that unfortunately our users do not just use one browser. Some may use Internet Explorer, others may use Mozilla Firefox. This is not to mention the growth of browsers such as Google Chrome and Opera.

Selenium Remote Control was initially developed by Patrick Lightbody as a way to test all of these different web browsers without having to install Selenium Core on the web server. It was developed to act as a proxy between the application under test and the test scripts. Selenium Core is bundled with Selenium Remote Control instead of being installed on the server.

This change to the way that Selenium tests are run allowed developers to interact with the proxy directly giving developers and testers a chance to use one of the most prominent programming languages to send commands to the browser.

Java and C# have been the main languages used by developers to create Selenium Tests. This is due to most web applications being created in one of those languages. We have seen language bindings for dynamic languages being created and supported as more developers move their web applications to those languages. Ruby and Python are the most popular languages that people are moving to.

Using programming languages to write your tests instead of using the HTML-style tests with Selenium IDE allows you, as a developer or tester, to make your tests more robust and take advantage of all setups and tear down those that are common in most testing frameworks.

Now that we understand how Selenium Remote Control works, let us have a look at setting it up.

## Setting up Selenium Remote Control

Selenium Remote Control is required on all machines that will be used to run tests. It is good practice to limit the number of Selenium Remote Control instances to one per CPU core. This is due to the fact that web applications are becoming more "chatty" since we use more AJAX in them. Limiting the Selenium instances to one per core makes sure that the browsers load cleanly and Selenium will run as quickly as possible.

## Time for action – setting up Selenium Remote Control

2. Extract the ZIP file.
3. Start a Command Prompt or a console window and navigate to where the ZIP file was extracted.
4. Run the command java –jar selenium-server-standalone.jar and the output should appear similar to the following screenshot:

### What just happened?

We have successfully set up Selenium Remote Control. This is the proxy that our tests will communicate with. It works by language bindings, sending commands through to Selenium Remote Control which it then passes through to the relevant browser. It does this by keeping track of browsers by having a unique ID attached to the browser, and each command needs to have that ID in the request.

Now that we have finished setting up Selenium Remote Control we can have a look at running our first set of tests in a number of different browsers.

### Pop quiz – setting up Selenium Remote Control

• Once you have placed Selenium Remote Control somewhere accessible, how do you start Selenium Remote Control?

# Running Selenium IDE tests with Selenium Remote Control

The Selenium IDE to create all the tests have only been tested on applications in Firefox. This means the testing coverage that you are offering is very limited. Users will use a number of different browsers to interact with your application. Browser and operating system combinations can mean that a developer or tester will have to run your tests more than nine times. This is to make sure that you cover all the popular browser and operating system combinations.

Now let's have a look at running the IDE tests with Selenium Remote Control.

(For more resources on this subject, see here.)

## Time for action – running Selenium IDE tests with Selenium Remote Control

Imagine that you have to run your Selenium IDE tests on a computer that doesn't have Selenium IDE installed or you do not want to use Firefox with any add-ons installed. To do this you will need to use Selenium Remote Control.

To run our tests in Selenium Remote Control we will have to use the –htmlsuite argument.

This tells Selenium to open the Test Suite that we created. We then need to tell it where to find the Test Suite and then where to write the results to. Our command in Command Prompt or a console window will be similar to the following snippet:

Java -jar selenium-server-standalone.jar -htmlsuite *firefox http://book.theautomatedtester.co.uk c:\path\to\testsuite.html c:\path\to\result.htm

Let's try this with our test suite now.

1. Open a Command Prompt or console window.
2. Use the following command:
Java -jar selenium-server-standalone.jar -htmlsuite *firefoxhttp://book.theautomatedtester.co.uk "c:\book\chapter 6\testsuite.html" "c:\book\chapter 6\result.html"

I have placed my testsuite in c:\book\chapter 6\. Run this command and the output should look similar to the following screenshot:

When the test starts running it will launch two browser windows. The first window will hold the Selenium Core Framework with the Test Suite on the left-hand side, the test steps in the center, and the results on the right-hand side. We can see what this looks like in the following screenshot:

### What just happened?

Using the –htmlsuite argument, we have managed to run our Selenium IDE tests through Selenium Remote Control. This time our tests used Firefox to run the Selenium IDE tests in Firefox. The command started Firefox and loaded the URL under test. It then loaded our test suite, and knows where to put the results when the tests are finished. When the tests have finished it will write the results to a file in a HTML format showing which tests have passed or failed and the command that it may have failed on. We can see what this looks like in the following screenshot:

# Running your Selenium IDE tests in Internet Explorer

Internet Explorer is one of the most popular browsers among users. Internet Explorer comes bundled with Microsoft operating systems, which is found on almost 90% of computers in the world. We, as developers and testers, should always be making sure that our code works on at least the latest variation of Internet Explorer but there are still people that use Internet Explorer 6, so be wary.

## Time for action – running our tests in Internet Explorer

Now that we are aware that we need to make sure that our web applications work in Google Chrome, let us have a look at how we can make our Selenium IDE tests work on the same:

1. Open up a Command Prompt or console window.
2. Use the command java –jar selenium-server.jar –htmlsuite*iexplore http://book.theautomatedtester.co.uk c:\path\to\testsuite.html c:\path\to\result.html and press Return. When the browser has loaded it should look similar to the following screenshot:

### What just happened?

We have just seen how our tests run in Internet Explorer. This is the first time that we have run our tests against a browser that is not Mozilla Firefox. We have accomplished this without any changes to our tests. This one of the most powerful features of Selenium and why it is one of the favorite testing tools available. It allows us to extend test coverage to the two most used browsers on the planet.

Now that we have run our tests in both Internet Explorer and Mozilla Firefox, it is good to use a WebKit-based browser. The most popular browser of this type is Google Chrome. In this section we will have a look at running our Selenium IDE tests within Google Chrome. This will help us make sure that we have sufficient coverage of our application.

## Time for action – running Selenium IDE tests within Google Chrome

Now that we have seen our tests work with two of the most popular browsers available, let's have a look at how it works with Google Chrome.

1. Open up a Command Prompt or console window.
2. Run the following command: java –jar selenium-server.jar –htmlsuite*googlechrome http://book.theautomatedtester.co.uk c:\path\to\testsuite.html c:\path\to\result.html and press Return. When the browser has loaded, it should look similar to the following screenshot:

### What just happened?

We have just seen how we can run our tests within Google Chrome. This means that we have, within this article, written one test suite and run it against three different browsers. This saves us, as developers or testers, a lot of effort in not having to create tests for each of the different browsers.

There are different types of browsers out there such as Opera and Konquerer to name two. We can see the browsers that are supported in the following list, and if your browser is not on the list, you can tell your test to use your custom browser with the path to your browser.

• *firefox
• *mock
• *firefoxproxy
• *pifirefox
• *chrome
• *iexploreproxy
• *iexplore
• *firefox3
• *safariproxy
• *konqueror
• *firefox2
• *safari
• *piiexplore
• *firefoxchrome
• *opera
• *iehta
• *custom

(For more resources on this subject, see here.)

# Running your Selenium IDE tests with the User Extensions

User Extensions are little commands that allow our tests to access different parts of the application.

## Time for action – running Selenium IDE tests with User Extensions

Imagine that you have created a number of User Extensions and you would now like to use them in Selenium Remote Control tests that you have created by exporting your tests. The following steps will allow you to do this

1. Open a Command Prompt or console window.
2. Run the following command:
java -jar selenium-server-standalone.jar -userExtensions \\path\to\extensions.js -htmlsuite *firefox http://book.theautomatedtester.co.uk c:\path\to\testsuite.html c:\path\to\results.html

### What just happened?

We have just seen how we can use User Extensions with Selenium Remote Control. This means that our applications can not only be used on different browsers and platforms, but also use the new commands that we create in order to help us with our testing. This is one of the most popular arguments that people use.

### Pop quiz

• Are you allowed to use relative paths to the Test Suite and results file?
• What is the argument needed to make your tests run within Firefox?
• What is the argument needed to make your tests run within Internet Explorer?
• What is the argument that allows our tests to use User Extensions?

### Have a go hero

Now that you have seen your tests running in Mozilla Firefox, Internet Explorer, and Google Chrome, try running your tests in another browser. This can be from the list of supported browsers, or using the *custom attribute.

# Selenium Remote Control arguments

The following is a list of the most common Selenium Remote Control arguments and how we can use them:

### -port

Since Selenium Remote Control acts as a proxy between your tests and the application being tested, it has to use a port to listen for commands. There will be instances where you will not be able to use the standard 4444 port. When this happens, adding –port &gtport number< allows you to use a different port number without conflicts.

### -userExtensions

We have already seen this in action. If you have created a user extension command in Selenium IDE, it can be used with Selenium Remote Control. By using –userExtensions c:\path\to\file.js we can access all the extra commands in our tests.

### -firefoxProfileTemplate

If you require a special profile, or if you need to make sure that a specific Firefox Add-on is installed, use –firefoxProfileTemplate /path/to/firefox/profile. This command will take the profile that you want and then clone it to be used in your test.

# Summary

In this article we had a look at Selenium Remote Control. This is the one tool in the Selenium suite of tools that truly allows us to write tests that are cross-platform and cross-browser. This will give us the confidence that our application works in all these different environments.

Specifically, we covered the following topics:

• Selenium Remote Control: We had a look at how Selenium Remote Control works. It is a proxy web server that sits between our tests which are running and the web application. This allows our tests to inject extra JavaScript to make the tests do the same as if we were using Selenium IDE.
• Setting up Selenium Remote Control: In this section of the article, we had a look at how we can set up Selenium Remote Control. We saw how Selenium Server and Selenium are just Java .jar files that can run from a Command Prompt.
• Running Selenium IDE tests within Selenium Remote Control: In this section of the article we had a look at how we can use all the test cases, to test our web applications within Selenium Remote Control.
• Selenium Remote Control arguments: In this section we had a look at the rest of the Selenium Remote Control arguments that we may need in our tests.

Further resources on this subject:

You've been reading and excerpt of: