Sunday 15 September 2013

First Functional Selenium Test

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 {
        selenium.open("/BrewBizWeb/");
        selenium.click("link=Start The BrewBiz Example");
        selenium.waitForPageToLoad("30000");
        selenium.type("name=id", "bert");
        selenium.type("name=Password", "biz");
        selenium.click("name=dologin");
        selenium.waitForPageToLoad("30000");
    }
}


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, selenium.open("/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. 


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.

No comments:

Post a Comment