30-feet view of test automation framework with selenium

Is selenium the future of test automation? That leads us to yet another important question, what was the past of test automation? Well…,I was just kidding. There are numerous amazing test automation tools out there but none of them could takeover the field of test automation. Why? It’s because, when you take a closer look at any testing project, the complexity increases so much that it takes a real tester with highly sophisticated testing practices and coding skills more than a mere tool to handle those situations. Yes, any tool no matter how good it is, cannot simply provide the foundation for a sound test automation framework. It is we, the “testers”, who can do it! But how?

This article shows step-by-step how selenium with a hybrid of other tools and practices could be used by the tester to develop a highly maintainable, robust, sophisticated and sound test automation framework.

 

This article gives a 30-feet overview of how a test automation framework with selenium looks like.

 

The little secrets of test automation:

Would you be interested to know how I can automate tests in just six weeks while others usually take over six months? I automate far faster and much cheaper than anyone else can do! Come on, my little secrets are so little.

Often in a testing project we tend to handle things in our own way that we see fit. The delicate test automation framework gets upset due to these gimmicks. Let me introduce you to the few main things that really counts during test automation:

  1. Customer focus
  2. Easy-to-understand test case listing the user actions
  3. Decoupling of test data from test execution
  4. Intelligent exception handling
  5. Data-driven testing framework
  6. Modular testing framework
  7. Separation of test verification from test script

Even if you are new to automation you can easily get a hang of it as you follow the steps outlined. Let me illustrate these principles with a simple demo.

Pre-requisites:

To execute the practical demonstration in this article we require the installation of JDK, Selenium Server, Selenium client driver for java, ant, TestNG, eclipse and TestNG plugin for eclipse.

For installation/setting up of Selenium, please refer the article “Selenium RC configuration for java – all in a nutshell”.

In this demo I am using Ubuntu as platform. You could use your favorite platform for coding. So, let us walkthrough the steps for developing a robust and highly maintainable test automation framework with selenium.

 

Step 1: Create java project and add references to external jars:

Suppose we want to dress perfectly for the weather with a rain hat, a rain coat and an umbrella. Now let’s develop a selenium test automation framework to test a very simple application that I have created called Weather Monster - “http://weathermonster.heroku.com/”.

  1. Open eclipse → Create a new java project called weatherMonster
  2. Click on Next button → click on the Libraries tab
  3. Click on Add External JARs button and add the respective jar files for selenium server, selenium java client driver and TestNG as shown in the diagram below → Click Finish

 

 

Step 2: Automatic configuration of test environment

A configuration script contains the key value pair needed to setup the test environment. These values could be changed dynamically using a shell script.

Let’s create a shell script to dynamically start the selenium server.

Create a new folder called “config” in the weatherMonster project and place the below shell script named config.sh in it.

#!/bin/bash
cd ~/Documents/working/installable/selenium/jar\ files/
gnome-terminal --title="config" -x bash -c "java -jar selenium-server-standalone-2.0rc2.jar"

This shell script navigates to the folder path containing the selenium server jar file (Replace this path with respective path on your machine). It then opens a new GNOME-TERMINAL named config and runs the selenium server by execution of the command “java -jar selenium-server-standalone-2.0rc2.jar”

To grant execution privileges to the script use chmod command as below:

chmod 777 config.sh

Then run the script using the below command:

./config.sh

We need to run this script each time before test suite execution to start the selenium server.

Right click the src folder and create a package named config. Right click the config package and create a java class called seleniumTest.java

Let’s assign values to the environment variables such as host, port, browser and so on.
Since we do not want to hard code the test data and we would like to decouple test data from test execution let’s assign the test data to the variables separately in the config folder and organize them as shown below:

package config;
 
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.HttpCommandProcessor;
 
public class seleniumTest
{
	/*Environment variables*/
	public final String HOST = "localhost";
	public final int PORT = 4444;
	public final String BROWSER = "firefox";
	public final String BASE_URL = "http://weathermonster.heroku.com";
	//To set the baseURL and server port
	public final HttpCommandProcessor PROC = new HttpCommandProcessor(HOST,PORT, BROWSER, BASE_URL);
	//To create a selenium object for passing as variable
	public final DefaultSelenium Selenium = new DefaultSelenium(PROC);
 
	/*Timeout*/
	public final String TIMEOUT="60000";
 
	/*Weather Monster home page*/
	public final String HOME_MSG = "This is weathermonster, bringing you the weather";
	public final String HOME_PAGETITLE = "Weather Monster";
}

 

Step 3: Modularization of commonly used methods

A module is a small independent script that represents AUT(Application Under Test). The most commonly used methods in the test case should be created as modules in separate folders that can be called and reused by many test cases. This increases the maintainability of the test suite. If there is a change, it needs to be applied only in one single place as opposed to all the places thus saving lots of time and cost.

Modularization also makes the test case easy-to-read and understand.

Consider the below test automation code. Can you understand it? It is a technical stack containing all the configuration details which is very hard to understand.

 

The test case should list the user actions not the technical details. This makes the test case customer-focused and easy-to-read. Now, let’s move out all the technical stack from the test case to a separate module.

Right click the src folder and create a package named module. Right click the module package and create a java class called testInit.java.

Here we code all the technical details of starting selenium server for test initialization so that the actual test case is freed from these unnecessary details and can cover the main area to test.


package module;
 
import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import config.*;
 
public class testInit extends SeleneseTestBase
{
	//Declare a new selenium test
	seleniumTest test = new seleniumTest();
 
	@Test
	public void testSetup(DefaultSelenium selenium)
	{
		try
		{
			//Open, maximize and focus on the facebook page. Delete all cookies
			selenium.start();
			selenium.open("/");
			selenium.waitForPageToLoad(test.TIMEOUT); //Handles timeout exception
			selenium.windowMaximize();
			selenium.windowFocus();
			selenium.deleteAllVisibleCookies(); //Clear all session cookies before testing
		}
		catch (Exception e)
		{
			 e.printStackTrace();
		}
	}
}


Step 4: Writing a selenium test case

Now that we already have modules for test setup and initialization, the writing of a selenium test case doesn’t take much effort. All we need to do is just call these modules and reuse them in our test case. This allows us to focus more on the customer.

A good automation script should contain the actual code logic to perform and validate a business scenario.

Right click the src folder and create a package named testSuite. Right click the testSuite package and create a java class called weatherMonsterHome.java


package testSuite;
 
/* Test case for weather monster home page*/
 
import module.*;
import config.*;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
 
public class weatherMonsterHome extends SeleneseTestBase
{
	//Declare a new selenium test
	seleniumTest test = new seleniumTest();
	testInit setUp = new testInit();
 
	@Test
	public void testWeatherMonster()
	{
			/*Test the home page of Weather Monster
			 * Pass the expected message and page title of the home page as parameters
			 */
			setUp.testSetup(test.Selenium);
			weatherMonster(test.Selenium, test.HOME_MSG,test.HOME_PAGETITLE);
	}
 
	public void weatherMonster(DefaultSelenium Selenium, String strHomeMsg, String strExpectedHomePageTitle)
	{
		try
		{
			/*Verification of Weather Monster home page*/
			assertTrue(Selenium.isTextPresent(strHomeMsg));
			System.out.println("Message on the Weather Monster home page is displayed successfully");
 
			assertEquals(Selenium.getTitle(),strExpectedHomePageTitle);
			System.out.println("Title of Weather Monster home page is correct");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}


This decoupling of test script from all the test execution details has made the test case a very easy-to-read list of user actions.

Ultimately the test structure is as below:

Step 5: Run the test case

  1. Navigate to the config folder and give the below command to start selenium server:
./config.sh
  • Right click on weatherMonsterHome.java and select “Run as TestNG test”.

 

OUTPUT:

RemoteTestNG starting

Message on the Weather Monster home page is displayed successfully

Title of Weather Monster home page is correct

PASSED: testWeatherMonster

 

We have missed out on a few little secrets of test automation like intelligent exception handling and data-driven testing. We shall get introduced to them in our next article, so stay tuned!

Feel free to ask any question or post comments.

Selenium RC configuration for java – all in a nutshell

What do we need to get started with selenium RC?

Configuration of Selenium RC test automation framework with Java client driver is not that simple as it requires the installation of so many packages and it takes a die-hard effort to get all the required information on getting started at one place. It’s sometimes boring and you might ask yourself “Is this the only way to get started?“. This article is not a shortcut but still presents all in a nutshell on how to get started with selenium RC. The below nutshell picture portrays all the software packages needed to set up Selenium RC for java. So now lets get started on the long road of setting up our test automation framework using selenium RC and java.

Below are the step-by-step instructions for the installation and configuration of Selenium RC for java client driver on ubuntu platform. To start selenium server we need JAVA. So, let’s start with the installation of java.

Installation of java and setting class path:

Download Java JDK from the below path: http://www.oracle.com/technetwork/java/javase/downloads/index.html

To find the exact path where java is installed give the below command:

which java

OUTPUT: /usr/bin/java Then set the path for JDK using the below commands:

export PATH=$PATH:/usr/bin/java
export JAVA_HOME=/usr/bin/java

To check whether java path is set properly give the below command

java -version

It should display the version of the Java installed.

Installation of selenium RC with java client driver:

Selenium Server: This is required to run the Selenium RC style scripts. Download and extract the selenium server jar file from: http://code.google.com/p/selenium/downloads/detail?name=selenium-server-standalone-2.0rc2.jar&can=2&q=

Selenium Client Driver for java: To create scripts that interact with the Selenium Server we need the selenium client driver. There are language-specific client drivers for ruby,c# and so on. Let us install the selenium client driver for java. Download the client-driver for the programming language java: http://seleniumhq.org/download/

Installation of ant:

Install ant on ubuntu using the below command

sudo apt-get install ant ant-optional

To find where ant has been installed give the below command:

which ant

Set the path of ant using the PATH variable as below:

export PATH=$PATH:/usr/bin/ant

Installation of Eclipse:

Install eclipse in ubuntu using the below command:

sudo apt-get install eclipse

TestNG plugin for eclipse:

1. Open eclipse. 2. Click Help → Install New Software.. 3. Then click on Available Software Sites link 4. Click on the Add button. Enter the site url as http://beust.com/eclipse to add TestNG plugin to eclipse. Now the TestNG plugin for eclipse is added as below:

Installation and configuration of TestNG on ubuntu:

The below command installs TestNG on ubuntu:

git clone git://github.com/cbeust/testng.git

Building TestNG with ant: Download cobertura-1.9.4.1-bin.zip from the below site into the home directory http://cobertura.sourceforge.net/download.html unzip using the below command:

unzip cobertura-1.9.4.1-bin.zip

Now navigate to the testng directory. Copy ivy-2.1.0.jar into /ant/lib directory as shown below:

sudo cp ivy-2.1.0.jar /usr/share/ant/lib

Now give ant command in the testng directory:

ant

The OUTPUT should be BUILD SUCCESSFUL.


Now we have installed all that we need to get started with Selenium RC using the client driver for java. In the next article, we will create a test suite using this setup.

Six steps for complete test automation with selenium grid

 

The earlier article “Crucial aspects of browser compatibilty testing with Selenium Grid” gives insight into browser compatibility testing andthe architecture of one of the most popular browser compatibility testing tools, selenium grid. Nowlet‘sget our hands dirty with test automation using Selenium Grid.

 

Pre-requisites:

The practice exercise in this article requires Eclipse, TestNG, ant, Java, Selenium RC and Selenium Grid. You can execute the code on your favorite platform. For this demo I am using ubuntu as platform.


Installation of Selenium Grid:

Please refer the documentation in seleniumhq for selenium grid download and installation instructions

http://selenium-grid.seleniumhq.org/step_by_step_installation_instructions_for_windows.html
http://selenium-grid.seleniumhq.org/get_started.html

Unzip the Selenium Grid folder and execute the below command in the Selenium Grid root directory to check whether it is installed correctly or not:

ant sanity-check

The output should be BUILD SUCCESSFUL

Now let’s get started.


 

Step 1. Launch of selenium hub:

Open a terminal and launch the selenium hub using the below command:

ant launch-hub

The hub is where the selenium remote controls providing the different environments like chrome on ubuntu or IE on Windows register themselves. We can view the list of remote controls registered with the hub at http://localhost:4444/console

Now the available remote controls section in the hub console displays nothing  since we did not start any remote control yet.

 

Step 2. Launch of selenium remote controls:

To start a remote control open a new terminal and issue the below command:

ant launch-remote-control

Then, open the hub console http://localhost:4444/console . The selenium grid hub snapshot displays a remote control on port 5555 for firefox. This is the default environment if we do not specify our own.

We can configure more than one selenium remote control with the same port but more than one remote control cannot run tests on the same port. The reason behind this is simple. For example, more than one person can call the same phone number but cannot talk over the phone to the same person at the same time. Only one call can be answered on a single phone at a time. Similarly it is impossible to run tests in parallel on two selenium remote controls configured with the same port number. We need to configure each remote control with unique port numbers. How do we do this?

If we want to specify our own environment we need to use the Dport and Denvironment options as below:

ant -Dport=5556 -Denvironment=*chrome launch-remote-control

 

Step 3. Automatic environment setup:

Let’s create a bash script for automatically opening up new terminals and launching several remote controls with the desired environment. If you are working on windows you need to create a batch file instead of a bash script. The below script works only on ubuntu platform.

Navigate to the selenium grid directory and create the below bash script. Let’s call it run.sh

#!/bin/bash
gnome-terminal --title="Hub" -x bash -c "ant launch-hub"
sleep 150
echo "10 seconds wait over"
gnome-terminal --tab --title="Chrome" -x bash -c "ant -Dport=5555 -Denvironment=*chrome launch-remote-control" &
gnome-terminal --tab --title="Firefox" -x bash -c "ant -Dport=5556 -Denvironment=*firefox launch-remote-control" &
gnome-terminal --tab --title="Opera" -x bash -c "ant -Dport=5557 -Denvironment=*opera launch-remote-control"

The bash script opens a new gnome terminal called “Hub” and launches the selenium hub. It sleeps for 150 seconds waiting for the hub to get started. Depending on the time taken you can change the sleep period. The script then opens three other new terminals and registers three remote controls with the respective environments specified. On execution of the script the below output is got on http://localhost:4444/console page.

Now the environment is setup for selenium grid automation. Let’s write a simple selenium grid program.

 

 

Step 4. Creating a java project called giftShop:

 

Suppose you would like to cheer up your friend on her birthday and give her the best birthday party of all time. You are planning  to celebrate your friend’s birthday with balloons, cakes, ice creams and of course a birthday gift!. You are going to surprise your friend with “I have got something for you” from www.gifts.com in such a way that no one can ever put a price tag on the friendship. Let’s write a selenium program to test the display of birthday gifts page on www.gifts.com website on multiple browsers.

 

  1. In Eclipse create a new java project called “giftShop”. Click on Next button and click on the “Libraries” tab. Click on “Add External JARs” button. Select the testng and selenium server JAR files as below and click on finish button.

 

 

2. Now create a new TestNG class inside the giftShop project. In the source folder field, browse to /giftShop/src. Give the package name as giftShop and class name as giftBox. Click on finish button.

Step 5. Enable testing in parallel mode in multiple browsers:


Right click on the giftShop project. Click on “Convert into TestNG” option. “Generate testng.xml” window will get displayed. This option will be available only if the TestNG plugin for eclipse is installed. Select “tests” in parallel mode dropdown and click on Finish button.

 

To organize the project structure create a folder called config and place the generated testng.xml file in it.

In the testng.xml file we just created, specify the browser and port names with which we have configured our selenium remote control as in the below piece of code.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
	<test name="Chrome">
		<parameter name="selenium.host" value="localhost"/>
		<parameter name="selenium.port" value="5555"/>
		<parameter name="selenium.browser" value="*chrome"/>
		<parameter name="selenium.url" value="http://www.gifts.com/"/>
		<parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
		<classes>
			<class name="giftShop.giftBox" />
		</classes>
	</test>
	<test name="Firefox">
		<parameter name="selenium.host" value="localhost"/>
		<parameter name="selenium.port" value="5556"/>
		<parameter name="selenium.browser" value="*firefox"/>
		<parameter name="selenium.url" value="http://www.gifts.com/"/>
		<parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
		<classes>
			<class name="giftShop.giftBox" />
		</classes>
	</test>
	<test name="Opera">
		<parameter name="selenium.host" value="localhost"/>
		<parameter name="selenium.port" value="5557"/>
		<parameter name="selenium.browser" value="*opera"/>
		<parameter name="selenium.url" value="http://www.gifts.com/"/>
		<parameter name="bdayLink" value="//li[@id='nav-oc']/div/ul[3]/li[3]/a/b"/>
		<classes>
			<class name="giftShop.giftBox" />
		</classes>
	</test>
</suite>

Step 6: Writing selenium grid program:

This selenium grid program will start selenium on chrome, firefox and opera. It would open up the www.gifts.com website and click on birthday as occasion. It asserts whether the landing page displays the birthday gifts in all the three browsers as expected.

 

package giftShop;
 
import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
 
public class giftBox 
{
	Selenium selenium;
 
	//Declaration of variables used in this program
	String strTimeout="30000";
	String bdayTitle="Birthday Gifts - Birthday Gift Ideas from Gifts.com";
	int intsleepTime=15000;
 
    @BeforeClass
    //Reads the values of host, port, browser and url "www.gifts.com" from testng.xml
    @Parameters({ "selenium.host", "selenium.port", "selenium.browser", "selenium.url" })
    public void beforeClass(String host, String port, String browser, String url)
    {
        selenium = new DefaultSelenium(host, Integer.parseInt(port),browser, url);
        selenium.start();
        selenium.open("");
        selenium.windowMaximize();
        selenium.windowFocus();
        selenium.deleteAllVisibleCookies();
    } 
 
    @Test
    @Parameters({ "bdayLink" })
    public void testB(String bDayGift)
    {
        try
        {
        	//Click on birthday as occasion on www.gifts.com website
        	selenium.click(bDayGift);
        	selenium.waitForPageToLoad(strTimeout);
 
        	//Verify that the birthday gift items page is displayed
        	Assert.assertEquals(bdayTitle, selenium.getTitle());
            Thread.sleep(intsleepTime);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
 
    @AfterClass
    public void afterClass() 
    {
        selenium.close();
        selenium.stop();
    } 
}

Running the test case:

Please do the following to run the selenium grid test case (after the hub and RCs are launched):
1. Right click on the testng.xml file
2. Click on Run as “TestNG Suite”

Please feel free to post any comments or ask questions.

Crucial aspects of browser compatibilty testing with Selenium Grid

What is browser compatibility for software? In different browsers the same text, graphics and color can appear differently. Some of the browsers do not support Javascript or Active-X control which are required by the software to provide the desired functionality to the end-user. The critical areas of the software is expected to appear and function as desired on all the commonly used browsers like safari, IE, firefox etc. This ensures that the code developed supports browser compatibility, one of the crucial aspects of software development.

Examples of browser compatibility issues:
1. The date input field is validated for incorrect date-formats using javascript. If javascript is not supported by the browser, the software will allow the customer to enter invalid date which is a defect

2. A country drop-down is sorted alphabetically using a client-side javascript. The same rule applies as one. If it is not supported, the user finds it very difficult to select a country from the dropdown

3. Misalignment of controls on the web page which looks frustrating to the customer

4. Web page looking cluttered on mobile browsers

Having elaborated on browser compatibility, let us discuss a very popular tool called selenium grid that provides a very easy way to execute multiple tests in parallel on different browsers. By running the test suite concurrently on several browsers it reduces the cost for browser compatibility testing and also dramatically speeds up the feedback cycle. This article is a great head-start on how selenium grid makes browser compatibility testing fun and easy. It explores the boundless potential it offers when used in conjunction with Selenium RC. In the next article “Six steps to complete test automation with selenium grid” we will get our hands dirty with selenium grid programming and execution.

Architecture of Selenium Grid:

Here we can see three main components in the diagram, Test, Selenium Hub and Selenium Remote Control.

The Test is a series of Selenium commands

The Selenium hub routes the selenese requests from the Test to the appropriate Selenium Remote Control.

The Selenium Remote Control is an instance of Selenium Server that registers itself to the hub when it starts describing the environment provided…….for example chrome on ubuntu, IE on windows.

Selenium grid is a distributed grid of Selenium Remote Controls. The Remote Control is an instance of Selenium Server that registers itself to the hub when it starts describing the environment provided. When the Selenium Remote Control is started we define which environment it provides to the hub. In the selenium test, when instantiating the Selenium driver, the hub environment is passed in the place of the browser string.

The Remote Control instances that have been registered with the grid show up in http://localhost:4444/console. The Selenium hub routes the selenese requests to the appropriate Selenium Remote Control. The hub puts the request on hold if there is no available Remote Control on the grid. As soon as the suitable Remote Control becomes available the hub will serve the request. The hub parses the tests that target specific environments. We can specifically target a particular browser and a particular platform for testing the software.

This is how Selenium grid works and it makes browser compatibility testing a breeze.