Incorporating external features in IBM Rational Functional Tester

More ways to use existing automation tools

You can use IBM® Rational® Functional Tester capabilities to improve your reports and scripts. This article shows you how to make them more powerful and feature-packed by using available features to work with processes, send e-mail, create screen captures, customize reports, create external property files, and generate unique data.

Share:

Shalini Gilra (shagilra@in.ibm.com), Automation Lead, IBM

Author PhotoShalini is an Automation Lead and Deployment Engineer at IBM in Pune, India. She has a varied and rich experience in functional testing, automation, and Level 2 support for IBM WebSphere Portal. She also holds several certifications.



02 July 2009

Also available in Chinese Spanish

The article teaches you how to use various tools and modules to enhance the automation provided by IBM® Rational® Functional Tester. Although many of these features may already be a part of the framework that you use to write test cases, you may either have overlooked or not be aware of them. Also, they can be really helpful if you are writing test cases without an underlying framework.

This article focuses on how to:

  • Use external data or properties files
  • Launch child process to enhance capabilities
  • Log automation playback results and screen captures at the point of failures
  • Send reports in an e-mail message
  • Run functions from the command prompt

External properties file

Separating your test data from the source code is an effective way to write test cases. This makes it easier to incorporate changes and create dynamic test cases. You can achieve this by using:

  • Datapools in Rational Functional Tester
  • An external database, such as IBM® DB2®
  • A properties file

See Resources for help with data pools and external databases.

If your data values are two-dimensional and you don't need to save data at runtime, you can choose to use an external properties file rather than a data pool or external database. This alternative is far easier to handle, and you can edit the file without compiling the source code again. Listing 1 shows a sample properties file, and Listing 2 provides a sample code to read the gsTestURL property from the dataset.properties file.

Listing 1. Properties file (dataset.properties)
#Test Server URL
gsTestURL=https://www.gmail.com/

#Test user name
gsTestUsername=testmail@gmail.com

#Test user password
gsTestPassword=passw0rd

#Compose mail subject
gsMailSubject=RFTAutomation
Listing 2. Code to read the above property file
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
....
....

Properties prop = new Properties();
String sPropVal = "";
String sFilename = "c:\\dataset.properties";
		
try{
	File file = new File(fileName);
	if (!file.exists())
	{
	System.err.println("Error - property file " + fileName + " does not exist.");
	}
		
	//assign property value
	try{
		FileInputStream fInputStream = null;
		try {
			fInputStream = new FileInputStream(file);
			prop.load(fInputStream );
		} finally {
			if (fInputStream  != null) {
			fInputStream.close();
		}
	}
	sPropVal = prop.getProperty("gsTestURL");
} catch(Exception e){
	System.err.println("Error - reading property from file "
	+ fileName + ".");
}

It is good practice to generate a unique input value for every run of the automation. You can do this by appending a date-based value to your data value, which gives you a unique value. For example, suppose that you append a date in the format YYYYMMDDHHmmss (such as 20090521125034) to gsMailSubject. This will yield RFTAutomation_20090521125034, which is a unique value for a nonparallel playback.


Working with a child process

There are times when you need to invoke a child process within Rational Functional Tester. A classic example is a browser crash with a modal dialog box displayed, as shown in Figure 1. This circumstance causes the browser to freeze, so Rational Functional Tester throws exceptions when it tries to work with this browser, but cannot close it. In such a situation, it is best to simply end the browser process and start a new one.

Figure 1. Screen capture of a browser crash
Internet Explorer dialog saying it needs to close

The example in code Listing 3 uses the Runtime class to call the Taskkill.exe application in Windows XP Professional to kill the iexplore.exe process. The exec method of the Runtime class can be used to call any other application or process within Rational Functional Tester. An alternative to this is shellExecute();

Listing 3. Using the exec method of the Runtime class
Runtime rt = Runtime.getRuntime();
String[] callArgs = { "Taskkill.exe", "/F", "/IM", "iexplore.exe" };
try {

Process child = rt.exec(callArgs);
    	child.waitFor();
    	System.out.println("Exit code is: " + child.exitValue());

} catch (IOException ioe) {
// output your exception here...
} catch (InterruptedException ie) {
// output your exception here...
}

Format your own report

The main task in any testing automation is to write and run the test cases by using a scripting language. However, another equally important task is to capture and log the results effectively. If you don't accomplish this, the main purpose of the automation is not served. The results from automation can be used not only to indicate the Pass or Fail, but also to debug any potential automation coding defects and to use the information to reproduce the target application defects that you discovered.

Although Rational Functional Tester generates a report at the end of every run, reading and analyzing this report can sometimes be tricky, time-consuming, or frustrating. Consider writing your own report, and make it user-friendly, easy to read, and customized for your organization's needs. You can log each action, which will be useful when you debug and analyze your run to determine the point of script failures. You can also print the exceptions to this report and paste a capture of the screen display at the time.

Screen capture

Capturing the screen display helps you determine the state that the application was in at the point of failure. Listing 4 provides sample code to capture the entire desktop view by using the Robot class.

Listing 4. Sample code for capturing screen image
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File; 
import javax.imageio.ImageIO;
 
....
....

public void printScreen(String filename) {
try {
		Robot robot = new Robot();
 		BufferedImage screenShot = robot.createScreenCapture
		(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		ImageIO.write(screenShot, "JPG", new File(filename));
	} catch (Exception e) {
		System.err.println("Unhandled Exception :  " + e);
		e.printStackTrace();
	}
}

How to send e-mail from Rational Functional Tester

The ability to send e-mail from within your code serves many purposes. For example, you can send a report or list any bugs uncovered.

The example in code Listing 5 sends e-mail with a file attachment to multiple recipients. You will need to include mailapi.jar and activation.jar in the classpath.

Listing 5. Send e-mail from within your code
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

....
....

try {
	// Set the host smtp address
	Properties props = new Properties();
	props.put("mail.smtp.host", "smtpDnsName");

	Session session = Session.getDefaultInstance(props, null);
	session.setDebug(false);

	// create a message
	Message msg = new MimeMessage(session);

	// set the from and to address
	InternetAddress addressFrom = new InternetAddress("shagilra@in.ibm.com");
	InternetAddress[] addressTo = new InternetAddress[2];
	addressTo[0] = new InternetAddress("abc@yahoo.com");
	addressTo[1] = new InternetAddress("xyz@gmail.com");

	msg.setFrom(addressFrom);
	msg.setRecipients(Message.RecipientType.TO, addressTo);
				
	// set the Date: header
	msg.setSentDate(new java.util.Date());
				
	// Setting the Subject and Content Type
	msg.setSubject(sSubject);
				
	// create and fill the first message part
               MimeBodyPart mbp1 = new MimeBodyPart();
	mbp1.setContent(sBody, "text/html");

	// create the second message part
	MimeBodyPart mbp2 = new MimeBodyPart();

	// attach the file to the message
	FileDataSource fds = new FileDataSource("test.txt");
	mbp2.setDataHandler(new DataHandler(fds));
	mbp2.setFileName(fds.getName());

	// create the Multipart and add its parts to it
	Multipart mp = new MimeMultipart();
	mp.addBodyPart(mbp1);
	mp.addBodyPart(mbp2);

	// add the Multipart to the message
	msg.setContent(mp);

	Transport.send(msg);
} catch (Exception e) {
	System.err.println("Unhandled Exception :  " + e);
	e.printStackTrace();
}

Run the script from the command line

Running the script from a command prompt allows the application more RAM for playback. Listing 6 is a command line to run a testpackage.TestScript, which is the script in the c:\TestDatastore directory.

Listing 6. Running a script from the command prompt
"C:\Program Files\IBM\SDP\jdk\jre\bin\java" -Xmx1024m -classpath "C:\Program Files\IBM\
SDP\FunctionalTester\bin\rational_ft.jar " com.rational.test.ft.rational_ft -datastore 
"c:\TestDatastore" -playback testpackage.TestScript

Alternatively, you can include the –Xmx option to specify the RAM allocation. For a playback on machines with 2GB RAM, use -Xmx1024m, but for machines with 1GB, use -Xmx512m. By default it is 256Mb.

Resources

Learn

Get products and technologies

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


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. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

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.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

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

 


All information submitted is secure.

Dig deeper into Rational software on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational
ArticleID=398327
ArticleTitle=Incorporating external features in IBM Rational Functional Tester
publish-date=07022009