Archived PushToTest site

Selenium Tutorial For Beginners - Tutorial 1

Write Your First Functional Selenium Test

Selenium Tutorial Table of Contents, Next Chapter, Previous Chapter

Decide if you are going to write your Selenium tests in a high-level language like Java, Ruby, Python, C#, and PHP. If  your coding skills need the help of a record/playback tool to write test scripts then skip to Tutorial 5.

Writing test scripts in a high level language is easy. I recommend you learn the concepts behind Selenium PageObjects first. PageObjects is an object oriented library for building easily maintainable tests. It separates test code into a Model, View, Controller pattern. Read a blog and watch a screencast on PageObjects. Other object oriented test scripting solutions include: GEB, Fitness.

A very simple Selenium test looks like this in Java:

package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class temp script extends SeleneseTestCase {
    public void setUp() throws Exception {
        setUp("http://localhost:8080/", "*iexplore");
    public void testTemp script() throws Exception {"/BrewBizWeb/");"link=Start The BrewBiz Example");
        selenium.type("name=id", "bert");
        selenium.type("name=Password", "biz");"name=dologin");

The super constructor setUp tells the Selenium client library to connect to the Selenium RC service to use Microsoft Internet Explorer (*iexplore) and the base URL of http://localhost:8080/. All subsequent Selenium commands will be relative to the base URL. For example,"/BrewBizWeb/") commands Selenium RC to tell IE to open http://localhost:8080/BrewBizWeb/.

Once you write this Java class, write an Ant script to compile the code and package it into a Java Archive Resource (JAR) file. Most Selenium users will run their Selenium tests from the Ant script itself. That makes it easy to integrate the test with a Continuous Integration environment as described in Tutorial 12.

There are great tutorials on the documentation site to explain Selenium test script authoring in more depth.

Selenium has many pitfalls. For example, many Selenium tutorials offer instruction to use XPath expressions to locate elements in a Web page. Do not use XPath if you can avoid it. The Microsoft Internet Explorer browsers do not feature a native XPath expression evaluator. One of my Selenium tests takes 3 minutes to run in Firefox and takes 30 minutes to run in IE depending on the XPath expressions in the Selenium script. The Selenium Load Testing screencast explains the problem and solution.

Learn the Selenium language basics, contructors, element locators, and event handling by watching the Selenium Basics screencast.

Watch The Selenium Basics Screenccast

Selenium Tutorial for Beginners (c) 2012 Frank Cohen. All rights reserved.