Selenium RC for Automated Testing

Nov 09, 2011
Belen Padilla

In my previous post, Selenium IDE for Automated Testing, I showed you how to use Selenium IDE, run a simple test case “GoogleSignUpErrors” and exported it as a Java file. This post is a guide to install Selenium Remote Control and run “GoogleSignUpErrors” as a Java program.

Selenium Remote Control

Selenium RC is a test tool that allows writing tests in any programming language. This tool is useful in test cases that need logic not supported by Selenium IDE. For example you might need to capture screenshots depending on the results of a certain action; in this case you can export your test case to your preferred language and modify the code to implement the desired behavior.

RC Components

Selenium RC works with these 2 components: Remote Control Server and Client Libraries.

(Selenium RC)

Remote Control Server receives the Selenium commands from the test program using HTTP GET/POST requests. Each command is passed to the browser using Selenium-Core JavaScript commands.  “Selenium-Core is a JavaScript program, actually a set of JavaScript functions which interprets and executes Selenese commands using the browser’s built-in JavaScript interpreter.” (Selenium RC)

Client Libraries are the Interface between each programming language and Selenium-RC server; they provide a programming function for each Selenese command. Client libraries pass the Selenese commands to the Selenium Server, then receive the result of the command and pass it to the test program. You can store the result of the command as a variable and do a specific action in your program, for example take a screenshot if the result is interpreted as “failed”.

Selenium-RC Installation

I am assuming Java is installed and PATH environmental variable is configured.

1.    Download Selenium RC Zip file from: http://seleniumhq.org/download/

2.    Unzip the file. There will be a folder per supported language and selenium-server folder which contains the selenium-server.jar which is the Selenium-RC server

As I am going to use a Java file, I installed JUnit to be used as test engine. To install JUnit, simply:

1.    Download latest version of JUnit from: http://sourceforge.net/projects/junit/files/

2.    Unzip Junit.zip file

After installing JUnit, you need to modify the CLASSPATH environmental variable:

Add JUnit to your %CLASSPATH% environmental variable. For example, in Windows:

      set CLASSPATH=%CLASSPATH%; C:\junit4.8.1\junit-4.8.1.jar;C:\junit4.8.1

To test JUnit installation, run this in the command line:

java org.junit.runner.JUnitCore org.junit.tests.AllTests
All tests should pass with OK message.(JUnit)

Starting Selenium Sever

1.    Go to the command-line console

2.    Run the command: java -jar [selenium server path ]\selenium-server.jar

Selenium-server will start running

Run Java Test case: GoogleSignUpError.java

First you will need to fix the automatically generated java file:

  • Remove : package com.example.tests;
  • Add: import junit.framework.*;
  • Add these code:

public static Test suite() {

                return new TestSuite(GoogleSignUpErrors.class);

}

public static void main(String args[]) {

                junit.textui.TestRunner.run(suite());

}

(Simple Thoughts - Java and Web Blog)

The fixed Java code:

//package com.example.tests;

import com.thoughtworks.selenium.*;

// This is the driver's import. You'll use this for instantiating

// browser and making it do what you need.

import junit.framework.*;//junit framework

import java.util.regex.Pattern;

// Selenium IDE add the Pattern module because it's sometimes used for

// regex validations. You can remove the module if it's not used in your

// script.

public class GoogleSignUpErrors extends SeleneseTestCase {

                public void setUp() throws Exception {

                                setUp("http://www.google.com/", "*chrome");

                }

                public void testGoogleSignUpErrors() throws Exception {

                                selenium.open("http://www.google.com/ncr");//instantiate and start the browser

                                verifyTrue(selenium.isElementPresent("logo"));

                                selenium.mouseOver("logo");

                                selenium.click("link=Sign in");

                                selenium.waitForPageToLoad("30000");

                                selenium.click("//div[@id='rhs_login_signup_box']/table/tbody/tr/td/a/b");

                                selenium.waitForPageToLoad("30000");

                                verifyTrue(selenium.isTextPresent("Create an Account"));

                                selenium.click("//img[@alt='Google']");

                                selenium.waitForPageToLoad("30000");

                                selenium.click("//div[@id='rhs_login_signup_box']/table/tbody/tr/td/a/b");

                                selenium.waitForPageToLoad("30000");

                                selenium.click("submitbutton");

                                selenium.waitForPageToLoad("30000");

                                verifyTrue(selenium.isTextPresent("Required field cannot be left blank"));

                                verifyTrue(selenium.isTextPresent("Required field cannot be left blank"));

                                verifyTrue(selenium.isTextPresent("Required field cannot be left blank"));

                                verifyTrue(selenium.isTextPresent("Required field cannot be left blank"));

                                verifyTrue(selenium.isTextPresent("The characters you entered didn't match the word verification. Please try again."));

                }

               

                public static Test suite() {

                                return new TestSuite(GoogleSignUpErrors.class);

                }

                public static void main(String args[]) {

                                junit.textui.TestRunner.run(suite());

                }

}

To run the java program:

1.    Open a new command prompt

2.    cd [GoogleSignUpErrors path location]

3.    Run these commands to compile and run the test case:

javac  GoogleSignUpErrors.java

java  GoogleSignUp

Selenium-RC Server will receive the commands from the java program and start executing them in Firefox.

References:

Selenium Project: http://seleniumhq.org/

JUnit: http://junit.sourceforge.net/doc/faq/faq.htm

Simple Thoughts - Java and Web Blog: