Selenium Tutorial

Hi all of you who are interested in Selenium testing.
I'll jot down a little tutorial here that should help you start using Selenium RemoteControl

Things to use


It comes in two useful shapes: Selenium IDE and Selenium RC. Download both.
IDE is a Firefox extension, so you'll need this browser too. Selenium IDE is helpful with 'real-time' building of your tests. You'll use it to check how your test works, where it fails, what errors it throws. It also provides the insight to all the Selenium commands and their usage.
RC is a server that behaves as a proxy - your commands go through this server and end up as javascript commands in browser. Result of these commands (handled by browser) is then returned through server to your test. That's how you communicate with the browser.
Unlike IDE, Selenium RC is capable of running tests on any browser you provide, most important of which being Internet Explorer.
Selenium RC has two parts: a server (in Java) and some clients (in different languages - Java, Ruby, Perl, .Net, Php, Python). This means that you'll need Java installed to run server. This also means that you can create tests in different languages.

Apache Ant

Ant is not really needed, but it helps a lot. Download it, extract it somewhere and add its "/bin" directory to path (see here). With Ant you can automate your processes (in this case - starting your tests). If you, for instance, create tests in Java, you can compile your code, export it to some directory, start Selenium server, start tests and write test results to some file, all using a single Ant script.


Whatever you do with web applications, Firebug will come handy. Among other things, it allows you to get details of any HTML page element, which is crucial for creating Selenium tests. Firebug is a Firefox extension and can be easily installed.

How to use it

1. Install Ant (as described above; google a bit for details)
2. Unpack Selenium RC (I'll refer to this directory as SEL_HOME)
3. Create a directory SEL_HOME/tests with directories 'src' and 'build'. Create a file called 'build.xml' in 'tests' directory. File system should now look like this:

4. Under 'src' directory create another called, for instance, 'mytests' and there create "" file

Now you have your environment set for testing. We need to create a test in "" file and an Ant script in "build.xml" to get everything going

Create a test

Edit "" file and enter something like this:

package mytests;

import com.thoughtworks.selenium.*;
import junit.framework.*;

/* This test is an extension of jUnit's TestCase
public class Login extends TestCase {
// Create a default Selenium object that will be used
// to perform Selenium commands
private Selenium browser;

// The "setUp" method will be performed before actual tests start
public void setUp() {
// Create a string (br) that tells Selenium what browser to start (firefox)
// This is the most painful part with setting Selenium environment
// For more info check
String br = "*custom \"c:\\Program Files\\Mozilla Firefox\\firefox.exe\" ";
br += "-no-remote ";
// Firefox profiles on Windows XP can be found on a path similar to this one:
// C:\Documents and Settings\mare\Application Data\Mozilla\Firefox\Profiles\cefj5tw7.Selenium
// On Windows Vista, that would be something like this (don't forget to escape characters):
br += "-profile \"C:\\Users\\Mare\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\badaabd3.testing\"";

// Connect to server, start browser and set the speed of test (milliseconds between each action)
browser = new DefaultSelenium("localhost", 4444, br, "");

// Test 1. ---------------------------------------
public void testLogin() {
// Open the start page"");

// Wait for login link to appear, click it and wait for login field
browser.waitForCondition("selenium.isElementPresent(\"link=Login\")", "30000");"link=Login");
browser.waitForCondition("selenium.isElementPresent(\"login-username\")", "30000");

// Enter wrong username and password and click "Login"
browser.type("login-username", "asdf");
browser.type("login-password", "asdf");"//input[@name='login']");

// Check that application denies your username and password
browser.waitForCondition("selenium.isElementPresent(\"//div[contains(.,'Invalid username or password. Please try again.')]\")", "30000");
// End of Test 1. ---------------------------------------

// The "tearDown" method will be performed after all tests finish
public void tearDown() {
// Disconnect from server and quit browser
Be sure to change Firefox options to suit your needs!! Change "profile" path and path to firefox.exe

Create an Ant script

Use the build.xml file attached at the bottom of this page. This file says the following:
1) default target of this script is "run_tests"
2) this target depends on the target "compile", so run "compile" first.
3) "compile" deletes 'build' directory and creates the fresh, empty one
4) "compile" uses "javac" compiler to compile your ".java" files (like") from 'src' directory. Compiled classes are put to 'build' directory. In order to compile your classes, classpath is provided containing all the jars (classes). That's the jars/classes contained in "selenium server" and "selenium java client" directories. Google a bit if you don't understand the classpath notion.
5) When compilation finishes, jUnit runs a test "mytests.Login" and writes the results to "result" file.

That's all that is needed to start tests.

Configure Firefox

In order for this script to work well, you need to set up Firefox correctly. Set its connection to use selenium server as a proxy. Find more about this on this link.

Run everything

First start the server:
java -jar SEL_HOME\selenium-server-1.0-beta-1\selenium-server.jar

Then start your tests:
in console change the directory to SEL_HOME\tests and then enter "ant". This should start your script.


Several things might happen with this configuration.
1. Firefox instance might be blocking the new, Selenium's instance of the browser. Check the processes (ctrl-shift-esc) and kill the firefoxes you don't need.
2. Firefox might ask for https certificate. Add the certificate as an exception. Some hints can be found here.
3. Firefox might ask weather to open previous sessions or not. Choose not to open previous sessions.
4. Firefox might prevent Selenium opening a popup. Select always to allow the site opening popups.
Subpages (1): Tips and Tricks
Marko Marjanović,
Aug 29, 2008, 5:42 AM