IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Rational  >

在 IBM Rational Functional Tester 中集成外部特性

以更多的方式使用已有的自动化工具

developerWorks
文档选项

未显示需要 JavaScript 的文档选项

英文原文

英文原文


级别: 初级

Shalini Gilra, 自动化负责人, IBM

2009 年 10 月 23 日

您可以使用 IBM® Rational® Functional Tester 的功能,来改善您的报告和脚本。本文向您展示了如何通过使用可用的特性,来使得这些功能变得更加强大,以处理流程、发送电子邮件、创建屏幕截图、定制报告、创建外部属性文件,并生成唯一数据。

本文向您介绍怎样使用不同的工具以及模型,来改善 IBM® Rational® Functional Tester 提供的自动化功能。尽管有许多这些特性可能已经成为您用于编写测试用例的框架的一部分,您可能忽视了这一点,或者根本没有注意到它。如果您正在编写测试用例,这些特性就变得十分有用了。

本文关注于怎样:

  • 使用外部数据或者属性文件
  • 启动子过程以改善功能
  • 记录自动化运行结果以及崩溃点的屏幕截图
  • 在电子邮件信息中发送报告
  • 从命令行中运行功能

外部属性文件

将测试数据与源代码隔离开来,是编写测试用例的有效方法。这使得集成更改并创建动态测试用例变得更加容易。您可以完成以下步骤来做到这点:

  • Rational Functional Tester 中的数据汇
  • 一个外部数据库,例如 IBM® DB2®
  • 一个属性文件

查看 参见参考资料 以得到数据汇和外部数据库的帮助。

如果您的数据值是二维的,而且您不需要在运行时保存数据,那么您可以选择使用外部属性文件,而不是数据汇或者外部数据库。这种替代处理起来更加容易,您可以编辑文件,而不用再一次汇编源代码。列表 1 显示了一个范例属性文件,列表 2 提供了一个范例代码以从 dataset.properties文件中阅读 gsTestURL属性。


列表 1. 属性文件(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

       


列表 2. 阅读以上属性文件的代码
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 + ".");
}

       

为每一次自动化生成一个单独的输入值,是一种很好的操作。您可以向数据值添加一个基于日期的值来实现这一点,这样就能产生一个单独的值了。例如,假如您以 YYYYMMDDHHmmss的格式来(例如 20090521125034)向 gsMailSubject添加一个日期。结果就会产生 RFTAutomation_20090521125034,对于非平行操作来说这就是一个独一无二的值。





回页首


与子过程协作

有时您需要激活 Rational Functional Tester 中的子过程。一个典型的例子便是浏览器崩溃,同时显示一个模型对话框,如图 1 所示。这种环境导致浏览器冻结,这样 Rational Functional Tester 在试着与该浏览器协作时会抛开例外,但是不会关闭该浏览器。在这种情况下,您最好结束浏览器过程,然后启动一个新的过程。


图 1. 浏览器崩溃的屏幕截图
Internet Explorer 对话框说它需要关闭

列表 3 代码中的范例使用 Runtime类,来访问 Windows XP Professional 中的 Taskkill.exe程序,以消除 iexplore.exe过程。 Runtime类的 exec 方法可以用于访问 Rational Functional Tester 中的任意其他程序或者过程。可以取代它的是 shellExecute();


列表 3. 使用 Runtime 类的 exec 方法
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...
}

       





回页首


组织您自己的报告

测试自动化过程的主要任务,是通过使用脚本语言来编写和运行测试用例。但是,另一项同等重要的任务,便是有效获取并记录每一次结果。如果您不这样做,那么自动化的目的便没有达到。自动化的结果不但可以用于指示 Pass 或者 Fail,还可以调试所有潜在的自动化编码缺陷,使用这些信息可以再现您发现的目标程序缺陷。

尽管 Rational Functional Tester 在每一次运行的结尾会生成一份报告,但是阅读和分析这些报告有时会变得十分困难、耗时或者令人沮丧。所以试着考虑一下编写自己的报告,让它变得更加友好,更容易阅读,并根据公司的需要对其做特定的编辑。您可以记录每一次操作,在您调试和分析运行以决定脚本失败点时,这样做会变得分外有用。您还可以向该报告指出例外情况,并粘贴屏幕截图。

屏幕截图

获取屏幕截图,可以帮助您决定崩溃边缘的程序的状态。列表 4 通过使用 Robot类,提供了范例代码,以获取整个的桌面视图。


列表 4. 屏幕截图图片的范例代码
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();
	}
}
       





回页首


怎样从 Rational Functional Tester 中发送电子邮件

从代码中发送电子邮件的功能有多种用途。例如,您可以发送没有漏洞的报告或者列表。

列表 5 代码中的范例,向多个用户发送带有文件附件的电子邮件。您需要在类路径中包含 mailapi.jaractivation.jar


列表 5. 从您的代码中发送电子邮件
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();
}     
       





回页首


从命令行中运行脚本

从命令行中运行脚本,可以让程序使用更多的 RAM。列表 6 就是运行 testpackage.TestScript的命令行,它是 c:\TestDatastore directory中的脚步。


列表 6. 从命令提示符中运行脚本
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();
}     
       

您也可以选择包含 –Xmx选项,来指定 RAM 的位置。对于在 2GB RAM 机器上运行的程序来说,使用 -Xmx1024m,而对于 1GB 的机器,则使用 -Xmx512m。默认条件下是 256Mb。



参考资料

学习

获得产品和技术

讨论


关于作者

作者照片

Shalini 是位于印度 Pune 的 IBM 的一名自动化主管和部署工程师。她在功能测试、自动化和 IBM WebSphere Portal 的 2 级支持方面具有丰富的经验。 她也持有多个认证。




对本文的评价








IBM 公司保留在 developerWorks 网站上发表的内容的著作权。未经IBM公司或原始作者的书面明确许可,请勿转载。如果您希望转载,请通过 提交转载请求表单 联系我们的编辑团队。
    关于 IBM 隐私条约 联系 IBM 使用条款