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
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.
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
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...
}
|
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.
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.
Learn
- See the demo: Using data pools with IBM Rational Functional Tester, Java Scripting
- Read Effective test automation techniques for Rational Functional Tester
- Follow just three steps for Establishing an IBM DB2 database connection in IBM Rational Functional Tester
- Learn more about Rational Functional Tester.
- Visit the Rational software area on developerWorks for technical resources and best practices for Rational Software Delivery Platform products.
- Subscribe to the developerWorks Rational zone newsletter. Keep up with developerWorks Rational content. Every other week, you'll receive updates on the latest technical resources and best practices for the Rational Software Delivery Platform.
- Subscribe to the Rational Edge newsletter for articles on the concepts behind effective software development.
- Subscribe to the IBM developerWorks newsletter, a weekly update on the best of developerWorks tutorials, articles, downloads, community activities, Webcasts and events.
- Browse the technology bookstore for books on these and other technical topics.
- Learn about other applications in the IBM Rational Software Delivery Platform, including collaboration tools for parallel development and geographically dispersed teams, plus specialized software for architecture management, asset management, change and release management, integrated requirements management, process and portfolio management, and quality management.
- Explore Rational computer-based, Web-based, and instructor-led online courses. Hone your skills and learn more about Rational tools with these courses, which range from introductory to advanced. The courses on this catalog are available for purchase through computer-based training or Web-based training. Additionally, some "Getting Started" courses are available free of charge.
Get products and technologies
- Download a trial version of Rational Functional Tester.
- Download trial versions of other IBM Rational software.
- Download IBM product evaluation versions and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
Discuss
- Join the Functional and GUI Testing forum to post questions and comments about using Rational Functional Tester and about functional testing in general.
- Check out developerWorks blogs and get involved in the developerWorks community.






