Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Automate web application testing with Sahi

Gang Li (liggli@cn.ibm.com), Staff Software Engineer, IBM
Gang Li photo
Gang Li currently works on IBM CDL and is responsible for quality assurance of IBM Initiate products with a focus on the automation testing foundation.

Summary:  There are many commercial tools and open source frameworks for automated testing of web applications. Sahi, a tool for automating web application testing, is gaining popularity. Sahi is an open source tool that allows recording and replaying across browsers. It provides different language drivers for writing test scripts, and supports Ajax and highly dynamic web applications. In this article, learn about Sahi by using a sample application and test case. Learn to format scripts for Junit and to test with Apache Ant.

Date:  14 Jun 2011
Level:  Intermediate PDF:  A4 and Letter (137KB | 12 pages)Get Adobe® Reader®
Also available in:   Korean  Japanese  Spanish

Activity:  9897 views
Comments:  

Introduction

QTP, Rational Functional tester (RFT), Selenium, and Webdrive are a few examples of the many commercial tools and open source frameworks for automated testing of web applications. In this article, learn about Sahi, a tool that's gaining popularity in the realm of automated testing. Sahi is an open source tool that provides: powerful abilities for recording and replaying across browsers; different language drivers for writing test scripts (Java, Ruby); and support for AJAX and highly dynamic web applications. Sahi also supports https and NTLM authentications. Figure 1 shows how Sahi fits in a simulated user operation.


Figure 1. Simulated user operation
Sahi working as proxy server, which passes the user request to real application and gets response

About Sahi

Sahi has several powerful features:

Sahi controller (IDE)
Works across browsers and records scripts in two modes: Sahi or Java format.

Robust object identification
Is independent of HTML structure. Its intuitive APIs, such as near and in method, can help locate browser elements even if the page has only a minor change, eliminating the requirement of XPaths (which can be hard to understand).

Implicit waits
Eliminates dealing with wait implementation, even for AJAX and page loads. Testers don't need to know the internals of an application.

Testing with Sahi

The rest of this article walks through a sample application to show you how to use Sahi for testing. You can download the sample application and test case to follow along.

Installation and configuration

To install and configure Sahi:

  1. Download the latest build (see Resources), and unzip the file. Sahi requires Java 1.5 or later.
  2. Start Sahi's proxy server. The startup scripts are in <sahi_root>\userdata\bin.
    • For Windows: start_dashboard.bat
    • For Linux: start_dashboad.sh
    As shown in Figure 2, you can select a browser icon to start the browser you want to use.

    Figure 2. Sahi Dashboard
    Sahi Dashboard: you can modify the configuration with it

    For this example, we don't need to be concerned about the browser configuration. Alternatively, Sahi provides GUI tools for you to start browsers. Run start_sahi.bat/start_sahi.sh to start it, then click a browser icon to start the proxy server and browser process.
  3. Enter start_sahi.bat/start_sahi.sh, which is required for silent execution with the assistance of build tools such as Ant.

    Note: For Internet Explorer (IE) only, you have to manually configure the browser to use Sahi's proxy. Or, you can use the tools provided by Sahi to set the browser's proxy while starting the Sahi proxy server. The proxy host and port are localhost:9999. (The port can be modified through <sahi_root>\config\sahi.properties.)

Recording testing scripts in Java format

Sahi provides two out-of-the-box script formats: Sahi and Java. You can modify the default option, which is Sahi, in <sahi_root>\config\sahi.properties. The Sahi format is a Java script-like format, running only within the Sahi Java script engine. The example in this article uses the more common Java format because the scripts can easily integrate with other testing tools and frameworks for large testing projects. The Java format involves a common Java code snippet that can be integrated with tools such as Junit and TestNG.

To run a brief test:

  1. Open the web application that you want to test. Press the ALT key and double-click on the page. (Firefox 3 was used for demonstration.)
  2. Select Record, ss shown in Figure 3:

    Figure 3. Sahi controller for recording
    Screen shot of Sahi Controller to record all executions

    After selection, it will change to the Stop button.
  3. Do some operations in the web application, and then select Stop. All the steps you executed in the application are recorded, as shown in Figure 4:

    Figure 4. Recorded scripts by controller
    Screen shot of Sahi Controller recording the scripts

    While your operations are recording, you can insert some assertions if you want. The tools also provide some options for insertions, but it is suggested you add them manually.

Integration with Junit

JUnit, the unit testing framework, is very important in establishing your test-driven development. In many cases, you can create test scripts in Junit format for convenience and reporting. The previous example recorded test scripts in Java format.

To format the above scripts in Junit style, create a Java project in Eclipse, and then add the junit4 library and sahi.jar located in <sahi_root>\lib. When creating a junit4 format test script, you have to initiate some variables required for the Sahi environment. The example in this article extends BaseTestCase.java so that all others test cases can be executed by extending this class.

The demo4sahi class extends the BaseTestCase class to implement all initiating tasks related to the Sahi proxy and then verifies two points:

  • If the email format is OK, you will get OK.
  • If the email format is incorrect, you will get Error.

Listing 1 shows a typical test script in Junit4 format.


Listing 1. Test script in Junit4 format
			
public class test4demo extends BaseTestCase {

    @Before
    public void setUp() {
        login();
    }
 
    @After
    public void tearDown() {
        logout();
    }
   
    @Test
    public void testDemo1() {
        browser.textbox("email").setValue("demo for sahi");
        browser.submit("Submit").click();
        assertTrue(browser.label("error").exists());
    }
   
    @Test
    public void testDemo2() {
        browser.textbox("email").setValue("demo@sahi.com");
        browser.submit("Submit").click();
        assertTrue(browser.label("ok").exists());
    }
}

To run the test scripts unattended, you need to implement all initialization and finalization for the Sahi runtime. There are four basic initialization parts, as outlined in Table 1:


Table 1. Initialization of the Sahi environment
Method in BaseTestCaseDescription
createConn()Initiates SAHI_BASE_PATH, USER_DATA_PATH, and browser type, and creates a browser process.
delConn()Cleans the browser process and reverts the system back to the initial status.
setIEProxy()Sets the IE proxy before executing the test. Use only for IE.
login(), logout(), and other commonly used high-level utilitiesPublic methods for all scripts.


BaseTestCase, as the parent class of all test scripts, will invoke the createConn method during runtime, ahead of all other methods, so it can open the browser to simulate the user's operations. Listing 2 shows some of the code for BaseTestCase.


Listing 2. BaseTestCase
			
public class BaseTestCase {
	protected Browser browser;
	protected Proxy proxy;
	protected boolean isProxyInSameProcess = false;
	protected String browserType = "firefox";
	@Before
	public void createConn() throws Exception {
		Configuration.initJava("f://Program/sahi", "f://Program/sahi/userdata/");
		if (isProxyInSameProcess) {
			proxy = new Proxy();
			proxy.start(true);
		}
		browser = new Browser(this.browserType);
		browser.open();
	}
	@After
	public void delConn() throws Exception {
		browser.setSpeed(100);
		browser.close();
		if (isProxyInSameProcess) {
			proxy.stop();
		}
	}
	public void login(){
		browser.navigateTo("http://localhost:8080/demo");
		browser.textbox("username").setValue("ligang");
		browser.password("password").setValue("welcome");
		browser.submit("Login").click();
	}
		/**
	 * Setting IE's proxy during testing environment initialization
	 * 
	 * @param enable
	 */
	public void setIEProxy(boolean enable) {
		try {
			Runtime.getRuntime().exec(
			new String[] {
				SetEnv.sahiBasePath
					+ "\\tools\\toggle_IE_proxy.exe",
				(enable ? "enable" : "disable") });
			Thread.sleep(1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    ......
}


Testing with Ant

Apache Ant, a common build tool, is used especially for Java-based projects. The Sahi framework includes a JAR file for Ant that you can use in the build.xml file. However, this out-of-the-box component is designed for Sahi's structure. Though you might want to use Junit to set up the whole infrastructure, it is not flexible or strong enough. For the regression testing of the example project, a build.xml was written that includes:

  • Starting and stopping the Sahi proxy server, as shown in Listings 3 and 4:

    Listing 3. Start Sahi proxy server
    			
    <target name="start_sahiserver">
    	<echo message="starting sahi server" />
    		<java fork="true" spawn="true" classname="net.sf.sahi.Proxy">
    			<classpath>
    				<fileset dir="${sahi.dir}">
    					<include name="**/*.jar" />       
    				</fileset>
    			</classpath>
                <arg value="${sahihome.dir}" />
                <arg value="${sahiuser.dir}" />
            </java>
            <antcall target="proxyon" />
    </target>
    



    Listing 4. Stop Sahi proxy server
    			
    <target name="stop_sahiserver">
    	<echo message="going to stop Sahi server" />
    	<sahi stop="true" sahihost="localhost" sahiport="9999" /v
    	<antcall target="proxyoff" />
    </target>
    

  • Opening and closing the IE proxy setting, as shown in Listing 5:

    Listing 5. Open/close IE proxy
    			
    <target name="proxyon">
    	<exec executable="${sahihome.dir}/tools/toggle_IE_proxy.exe">
    		<arg value="enable" />
    	</exec>
    </target>
    <target name="proxyoff">
    	<exec executable="${sahihome.dir}/tools/toggle_IE_proxy.exe">
    		<arg value="disable" />
    	</exec>
    </target>
    

  • Reporting.

The actions can be executed separately for easy debugging in the Eclipse IDE.


Advanced topics

In web application testing, it's very important to effectively identify an object. Unlike many other testing tools, Sahi has some powerful APIs to locate objects in a web page. Table 2 outlines a few examples. Refer to the Resources section for a link to online documentation with a complete list.


Table 2. Special locating methods
FeatureMethodSample code
Support regular expressionsAlmost all elements identifierbrowser.link(/Link to .*/)
Locating based on DOM relationsnear, in browser.button("delete").in(browser.cell("item1")).click()

browser.button("delete").near(browser.cell("item1")).click()

Locating based on positional relationsunderbrowser.button("delete").under(browser.cell("item1"))


Summary

This article has explored Sahi, a tool for automating web application testing. Sahi is an open source tool that allows recording and replaying across browsers. In this article, you learned about different language drivers for writing test scripts. A sample application and test case showed how to format scripts for Junit and how to test with Apache Ant. Sahi's advanced APIs were also outlined.



Downloads

DescriptionNameSizeDownload method
Sample web applicationwebapp4sahi.zip7KBHTTP
A Sahi test exampleDemo4SahiTest.zip287KBHTTP

Information about download methods


Resources

Learn

Get products and technologies

  • Sahi: Download Sahi.

  • IBM trial software: Innovate your next development project with IBM trial software, available for download or on DVD.

Discuss

About the author

Gang Li photo

Gang Li currently works on IBM CDL and is responsible for quality assurance of IBM Initiate products with a focus on the automation testing foundation.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development
ArticleID=680321
ArticleTitle=Automate web application testing with Sahi
publish-date=06142011
author1-email=liggli@cn.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers