EGIT – GIT in action within your Eclipse IDE

This article presents two simple steps to place your eclipse workspace under GIT. EGIT is a plugin that allows you to use GIT directly within the Eclipse IDE.

Step1: Installation of EGIT

Navigate to Help  –> Install New Software

Step1help

 

Type the URL from which the EGIT could be downloaded. Then, click select all and next.

http://download.eclipse.org/releases/indigo/

step3selectallnext

 

Then Deselect All and select only “Eclipse Egit” and click on next.

step3bselecteclipseegit

After installation of EGIT, we need to configure GIT on Eclipse

 

Step2:  Configure your Eclipse project to use GIT

Right click on your project in Eclipse. In the context menu, Choose Team –> Share Project

Step4LocalRepository

 

Then, select GIT in the Share Project menu

Step5SeletGIT

Then enter the name of your project and create a new GIT repository for the project

Step6CreateNewGITrepository

To view your project as a GIT repository within the Eclipse IDE, navigate to Windows –> Show View –> GIT –> GIT Repositories

Step7ShowViewGITReo

Now you can see your project placed under GIT version control and apply all the GIT commands from your Eclipse IDE!

GIT merge conflict – commit your changes or stash them before you can merge

Here are the frequent GIT merge conflict horror stories one might encounter when one pulls the latest code changes or when the project code is rebased. If you are using GIT bash, its quite easy to configure the GIT environment with a visual GUI tool called DiffMerge that helps you clearly see the lines of code causing the conflict and resolve the merge conflict with a “single click”.

Problem:

Merge conflicts while doing git pull
Output 1:

git pull
Enter passphrase for key '/c/Users/arvenkataraman/.ssh/id_rsa':
Auto-merging testSuite/sample.xml
CONFLICT (content): Merge conflict in testSuite/sample.xml
Automatic merge failed; fix conflicts and then commit the result.

Output 2:

git pull --rebase
U       testSuite/sample1.xml
U       testSuite/sample2.xml
U       testSuite/sample3.xml
M       testSuite/sample4.xml
U       testSuite/sample5.xml
U       testSuite/sample6.xml
U       testSuite/sample7.xml
M       testSuite/sample8.xml
M       testSuite/sample9.xml
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm '
as appropriate to mark resolution, or use 'git commit -a'.


Output 3:

git pull
Falling back to patching base and 3-way merge...
Auto-merging testSuite/sample.xml
CONFLICT (content): Merge conflict in testSuite/ sample.xml 
Auto-merging testSuite/sample2.xml
CONFLICT (content): Merge conflict in testSuite/ sample2.xml 
Auto-merging testSuite/sample3.xml
CONFLICT (content): Merge conflict in testSuite/ sample3.xml
Failed to merge in the changes.
Patch failed at 0001 test changes

Solution


Step 1: Download DiffMerge tool

The SourceGear DiffMerge tool could be downloaded from the below location:
http://www.sourcegear.com/diffmerge/downloads.php


Step 2: Configure GIT to use DiffMerge tool

Open GIT Bash and navigate to the GIT repository. Then give the below commands to configure GIT with the downloaded DiffMerge tool

git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd "C:/\"Program Files\"/\"SourceGear\"/Common/DiffMerge/sgdm.exe --merge --result=\$MERGED \$LOCAL \$BASE \$REMOTE"
git config --global mergetool.diffmerge.trustExitCode true


Step 3: Invoke DiffMerge from GIT bash

 


If the git pull command doesn’t succeed due to merge conflict, please give the below command:

git difftool

The DiffMerge opens up as below with automatically selected merge conflict files for comparison
Step1mid2selectfiles

Step 4: Shift the highlighted piece of code to resolve the merge conflict

On the top of the DiffMerge tool there is an arrow to apply changes from the right to left:
step2 DiffMerge for merge conflict
The files are displayed side-by-side for comparision and the differential piece of code to be merged is highlighted in violet color:
step3 Merge changes
Select the piece of code highlighted and click on the “apply changes from the right” arrow at the top. After merge the code will look like below:
step4 After merge

Similarly we could resolve all the merge conflicts with a single click. It is a lot more easier since this is a visual tool for merge.

Code debugging with watch variables/expression using Eclipse IDE

Here are the steps to do extensive debugging of java code using Eclipse IDE with watch variables, expression and Inspect.

Step1: Enable breakpoint

Set the breakpoint at the line of code or point of method entry from where you would like to start debugging the code. Right click on the left margin of the editor next to the line of code and a context menu pops up. Select toggle breakpoint in the context menu as shown below:

Figure1: Toggle Breakpoint

Step1-enableBreakpoint

 

Step2: Configure the breakpoint to stop execution

To start debugging, the execution should stop at the breakpoint specified. For this click on breakpoint properties (refer Figure1)  and do the following:

  1. Check Hit count
  2. Specify value as 1
  3. Select “Suspend thread” option

This will stop the execution when the program hits the breakpoint.

Step3setBreakpint

 

Step3: Switch to debug perspective

In Eclipse, select Window –>Open Perspective –> Debug as shown below:

Step4 switch to debug perspective

 

Step4: Run in debug mode

Now run the program in debug mode. Select Run –> Debug

Step5 Run in debug mode

Now the program starts running in debug mode and you would see the state of the thread as “running”

Step6 running thread

When the program hits the breakpoint the state of the thread changes from “running” to “suspended

Step7 suspend thread

 

Step5: Debugging the code with Expressions \Watch variables \ Inspect

Now the code stops at the breakpoint. You could use the watch variables / expression to monitor the current value of the debug variable.

Suppose you set the breakpoint at the variable named “counter” and the program stopped at counter variable as shown below:

step8 stops at code\

Add the expression with the variable name “counter” which allows you to monitor the value of the variable as you execute the program:

Step11 watch variableexpressions

You could also right click on the counter variable and select “Inspect” from the context menu

Step12 right click to inspectstepinto

If it is a method then, select the method name and click on “Step Into Selection“. This will allow you to monitor or debug the method execution line by line.

 

Step6: Use F6 key to step into the method

To “step into” the next executable line of code in the current method, press the “F6 Key”. This will pass the program control from the current line to the next executable line of code.

Step9 Press F6 key

 

Git – Permission denied (publickey)

This is a very common issue that most users face after installation of GIT.

Extensive troubleshooting of this GIT issue “Git – Permission denied (publickey)” can be done with the below command:

ssh -vT git@github.com

Output:

OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
debug1: Connecting to github.com [204.232.175.90] port 22.
debug1: Connection established.
debug1: identity file /c/Users/arvenkataraman/.ssh/identity type -1
debug1: identity file /c/Users/arvenkataraman/.ssh/id_rsa type -1
debug1: identity file /c/Users/arvenkataraman/.ssh/id_dsa type 2
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.5p1 Debia
n-6+squeeze1+github9
debug1: match: OpenSSH_5.5p1 Debian-6+squeeze1+github9 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.6
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
The authenticity of host 'github.com (204.232.175.90)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,204.232.175.90' (RSA) to the list of know
n hosts.
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /c/Users/arvenkataraman/.ssh/identity
debug1: Trying private key: /c/Users/arvenkataraman/.ssh/id_rsa
debug1: Offering public key: /c/Users/arvenkataraman/.ssh/id_dsa
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

 

Solution:

Step 1: Navigate to .ssh directory inside your home directory

         cd ~/.ssh

Step 2: Generate the public/private rsa key pair

         ssh-keygen

Step 3: Copy the SSH key to the clipboard using the below command

         cat id_rsa.pub | clip

Step 4: Navigate to your GIT repository in https://github.com/

Step 5: Go to Account Settings

accountsetting

Step 6: Under Account Setting select SSH keys

ssh

Step 7:  Click on Add SSH key, paste the key copied to the clipboard (in Step 3) and store the key

saveSSH

 

 

TestNG – running tests in parallel

If you have several suites to be run, you could run them in parallel at the same time in different threads.

Parallel suites

In command line the ant task suiteThreadPoolSize can be used to run several suites in parallel using org.testng.TestNG as shown below:

java org.testng.TestNG -suiteThreadPoolSize 3 testng1.xml testng2.xml testng3.xml

This command runs the three suites in 3 different threads in parallel

Parallel methods, tests, classes

In testng.xml file, using the attribute parallel inside the suite tag instructs TestNG to runs tests in parallel in different threads. The attribute parallel accepts certain values as outlined below:

<suite name="mysuite" parallel="methods" thread-count="5">
<suite name="mysuite" parallel="tests" thread-count="5">
<suite name="mysuite" parallel="classes" thread-count="5">
<suite name="mysuite" parallel="instances" thread-count="5">

1. parallel=”methods”: TestNG will run all the test methods in parallel in different threads. The dependent methods will also run in different threads but would respect the order that was specified.
2. parallel=”tests”: TestNG will run all the methods in the same test in the same thread but each test will be run in separate threads. This allows to group the thread-safe classes in the same test so that they will run in the same thread while taking advantage of TestNG’s capability to run tests in several threads.
3. parallel=”classes”: TestNG will run all the test methods in the same class in the same thread and each class will be run in separate thread.
4. parallel=”instances”: TestNG will run all the test methods in the same instance in the same thread and each instance will be run in separate threads.

Also, the attribute threadPoolSize of the @Test annotation could be used to run tests in parallel as shown below.

@Test(threadPoolSize=5, invocation-count=10, timeout=10000)
public void testMethod()
{
  System.out.println("Hello");
}

This will invoke the method testMethod 10 times in 5 different threads. The timeout of 10 seconds ensures that the thread is not blocked for more than 10 seconds. The timout applies for both parallel and non-parallel mode.

TestNG groups using include/exclude

TestNG allows to perform groupings of test methods. The methods can be declared to belong to groups and TestNG can be invoked to include/exclude certain set of groups. This provides maximum flexibility to structure the tests in a more intuitive way.

Step1: Declare groups in java class

Suppose we would like to group the test methods into two categories functional and sanity. We declare the methods to belong to either of the two groups using the “groups” attribute of @Test annotation.

public class Test1
{
  @Test(groups={"functional","sanity"})
  public void testMethod1(){
  }
 
  @Test(groups={"sanity"})
  public void testMethod2(){
  }
 
  @Test(groups={"functional","sanity"})
  public void testMethod3(){
  }
}

 

Step2: Specify groups in TestNG.xml file

Suppose we would like to run the sanity tests alone. We specify the groups in the TestNG.xml file using the <groups> tag inside the <suite> or <test> tag as shown below:

<suite name="mySuite" verbose="1">
  <test name="Regression">
    <groups>
      <run>
        <include name="sanity"/>
      </run>
    </groups>
    <classes>
      <class name="sample.Test1"/>
    </classes>
  </test>
</suite>

TestNG.xml – A Quick Overview

TestNG is a testing framework designed to simplify the testing needs.

 

Quick overview of TestNG.xml file

  • A suite is an xml file represented by <suite> tag. The TestNG suite can contain one or more TestNG tests
  • A test is represented by the <test> tag. A TestNG test can contain one or more TestNG classes
  • A class is a java class having TestNG annotations. It is represented by the <class> tag. A TestNG class can contain one or more TestNG methods
  • A method is method configured with @Test annotation

 

Sample TestNG.xml file

 

 

Also, it is possible to use packages instead of classes in TestNG.xml file. For this package tag is used as shown below:

 

Getting started with Ruby on Rails

Video demonstration of ruby on rails application creation:

One of the many advantages of rails is that it immediately gets you from zero to a functional application. To get started lets create a directory for the rails project called “railsproject”.

mkdir railsproject

Now open the ruby command prompt and navigate to the directory that we just created. Run the rails new command to make your first rails application. Let us call our first rails application as “first”. The rails new command is a program that creates a skeleton rails application. It creates a standard file and directory structure that makes it easier than ever to organize and deploy files. All the rails application have a common file and directory structure making it easy to understand someone else’s code.

rails new first

After the file creation is done rails automatically runs the bundle install command which we will discuss later in this article. Upon successful execution of the rails new command, it will display the message “Your bundle is complete!”

Output:

Bundle install command

 

Now lets have a quick look into the files and directories that have just been created. I use Aptana studio, an IDE for windows users of Rails.

Rails application directory structure

 

Let’s learn more about the files that have been created.

  • app – This contains the Core application code including the models, views, controllers and helpers. The model, view and controller are very important parts of a rails architecture which we would learn in-depth in the later articles
  • script/rails -A script to generate code and start the local server
  • vendor – This contains third party code such as plugins and gems
  • Gemfile – This file contains the gem requirements of the application

The bundle install or the bundler command which we had seen in the beginning of this article installs the gems from the Gemfile. The bundler looks for the gems that needs to be installed from this file. Thus the Gemfil should contain the gems needed by your application.

Gemfile:

source 'https://rubygems.org'
 
gem 'rails', '3.2.8'
 
# Bundle edge Rails instead:
# gem 'rails', :git =&gt; 'git://github.com/rails/rails.git'
 
gem 'sqlite3'
 
# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~&gt; 3.2.3'
  gem 'coffee-rails', '~&gt; 3.2.1'
 
  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms =&gt; :ruby
 
  gem 'uglifier', '&gt;= 1.0.3'
end
 
gem 'jquery-rails'
 
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~&gt; 3.0.0'
 
# To use Jbuilder templates for JSON
# gem 'jbuilder'
 
# Use unicorn as the app server
# gem 'unicorn'
 
# Deploy with Capistrano
# gem 'capistrano'
 
# To use debugger
# gem 'debugger'

Inside the gemfile, the rails gem is the gem for rails itself. “sqlite3″ is the gem for the ruby interface to the SQLite database. ”jquery-rails”  is for the jQuery javascript library. As you could notice there is a version 3.2.8 specified in the rails gem command. Unless the version is explicitly specified, rails automatically deploys the latest version of that gem.

Inheritance – the Ace of the Object Oriented Programming world

The code is just a junkyard if it is not designed for reuse. Re-usability is very simple if you understand inheritance, a very powerful principle of object oriented programming. For this reason I call inheritance the Ace of the object-oriented programming world.

Here, I explain inheritance with a simple illustration. From the figure below can you list the properties that are common to all the balloons?

 

The below properties are common to every balloon:

  1. Color
  2. Size
  3. Price and so on

These properties that are common to all classes of a given type say balloon is stored in a class called the base class or super class or parent class. Inheritance is a powerful object oriented programming feature that allows a class to reuse the properties and methods of an already existing class while adding its own functionality. It’s the process of creating a new class as an extension of an existing class primarily to enhance the code re-usability. The class that extends is called the derived / sub class and the class getting extended is called the super / base class.

Suppose there is a super class called superBalloon. There are different kinds of balloons such as birthday balloons, balloons that represent a theme like sports or Disneyland and so on. For class inheritance java uses the keyword extends. Now let’s write a small java program for the base class “superBalloon”.

 

package balloonInherit;
 
public class superBalloon
{
	/**
	 * The superBalloon class defines methods and variables common to all balloons such as
	 * size, color and price
	 */
	//commonly used fields in all balloons
	public int size;
        public String color;
	public double price;
	public int number_of_balloons_purchased;
	public double total_cost;
 
	public void purchase(double price, int number_of_balloons_purchased)
	{
		total_cost = (price * number_of_balloons_purchased);
	}
}

A derived class is more specific and can have additional functionality of its own apart from that of the base class. Thus a birthday balloon is a specialized version of the balloon class having a new field called decoration cost specific to it’s type to better suit the needs.

We can also override /replace the behavior of the base class in the derived class. Here, in the below example the birthday balloon inherits the superBalloon class and defines a new field called decoration cost specific to it’s kind. We use the super keyword to reuse the already existing properties of the base class.

package balloonInherit;
 
public class subPartyBalloon extends superBalloon
{
	/**
	 * Since this is a partyBalloon, an additional new field called decoration cost is added
	 * which is specific to this kind of balloon
	 */
 
	public static void main(String args[])
	{
		//Create an object for subPartyBalloon class and invoke the purchase method
                subPartyBalloon bDayBalloon = new subPartyBalloon();
		bDayBalloon.purchase();
	}
 
	public void purchase()
	{
		//Additional property specific to the type partyBalloon
		double decoration_cost = 2.00;
 
		//Reuse the existing properties of the base class using super keyword
		super.size = 20;
		super.number_of_balloons_purchased = 3;
		super.color = "red";
 
		//These are the prices of the balloon depending on it's size
		switch (super.size)
		{
			case 10:
				super.price = 5.00;
				break;
			case 20:
				super.price = 10.00;
				break;
			case 30:
				super.price = 15.00;
				break;
			default:
				System.out.println("Invalid size");
		}
 
		//Invoke the the purchase methods of the super class to calculate the base_cost
		super.purchase(price,number_of_balloons_purchased);
 
		//Since it is a birthday party balloon the additional cost for decoration is added
		total_cost = total_cost + decoration_cost;
 
		System.out.format("The total cost of %d %s birthday balloons is %.2f", number_of_balloons_purchased,color,total_cost);
	}
}

The switch-case control structure decides the price of the balloon based on the input size. Once a matching size is found it assigns value to the price variable and breaks the execution of the switch-case block. The total cost of the birthday balloons is calculated and the output is as below.

OUTPUT:

The total cost of 3 red birthday balloons is 32.00

Now let’s create a java program for another derived class called subThemeBalloon. A theme based balloon has theme and design as additional fields specific to it’s kind. For example to represent the theme of Disneyland, a balloon is designed with the theme Disneyland and with the shape Mickey mouse.

The subThemeBalloon class reuses the existing properties of the base class superBalloon using the super keyword. It also has three additional new fields viz. theme, design and decoration cost specific to it’s kind to better suit the needs.

package balloonInherit;
 
public class subThemeBalloon extends superBalloon
{
	/**
	 * A theme based balloon has theme and design as additional fields. For example to represent
	 * the theme of Disneyland a balloon is designed with the theme Disneyland and with
	 * the shape mickey mouse
	 **/
 
	public static void main(String[] args)
	{
                //Create an object of subThemeBalloon class and invoke the purchase method
                subThemeBalloon themeBalloon = new subThemeBalloon();
		themeBalloon.purchase();
	}
 
	public void purchase()
	{
		//Declare three additional properties that are specific to the type themeBalloon
		String theme = "Disneyland";
		String design = "Mickey Mouse";
		int decoration_cost = 5;
 
		//Reuse the existing properties of the base class using super keyword
		super.number_of_balloons_purchased = 20;
		super.price = 15.00;
 
		//Invoke the purchase method of the base balloon class to calculate base cost using super keyword
		super.purchase(price, number_of_balloons_purchased);
 
		//Add the decoration cost for the balloon
		total_cost = total_cost + decoration_cost;
 
		//Print the total cost of the balloon
		System.out.format("The total cost of a %s %s balloon is %.2f", theme, design, total_cost);
	}
}

OUTPUT:

The total cost of a Disneyland Mickey Mouse balloon is 305.00

 

This is all about inheritance. Feel free to post any comments /questions.

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.