用 Functional Tester 读取 Windows 注册表

Windows® 注册表是 Windows 操作系统用于存储配置信息的数据库。 测试员常常需要使用 Functional Tester 命令从此数据库读取信息。本主题提供关于执行此操作的示例。

以下示例适用于在 Windows 上运行的脚本:

import javax.swing.JOptionPane;

import resources.RegistryExampleHelper;
import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.SAP.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;

/**
 * Description   : Functional Test Script
 * @author Administrator
 */
public class RegistryExample extends RegistryExampleHelper
{
	/**
	 * Script Name   : RegistryExample
	 * Generated     : Jul 20, 2006 1:48:49 PM
	 * Description   : Functional Test Script
	 * Original Host : WinNT Version 5.1  Build 2600 (S)
	 * 
	 * @since  2006/07/20
	 * @author Administrator
	 */
	public void testMain (Object[] args)
	{
	    try
	    {
	        //Use this code to extract String (REG_SZ) values from the registry.
	    	String regKeyString ="HKEY_LOCAL_MACHINE\\SOFTWARE\\Rational Software\\Rational Test\\8\\Rational FT Install Directory";
	    	
	        String regValueString = getOperatingSystem().getRegistryValue(regKeyString);
	        JOptionPane.showMessageDialog(null, regValueString,"String Registry Value",1);
	    }
	    catch (NoSuchRegistryKeyException e)
	    {
	        JOptionPane.showMessageDialog(null, "Error finding registry key.");
	        System.out.println ("No Such Registry Key Exception." + e);
	    }
	    try
	    {
	        //Use this code to extract Integer (DWORD) values from the registry.
	        String regKeyInt = "HKEY_CURRENT_USER\\Control " +"Panel\\Desktop\\LowLevelHooksTimeout";
	        Integer regValueInt = new
	           Integer(getOperatingSystem().getRegistryIntValue(regKeyInt));
	        JOptionPane.showMessageDialog(null,regValueInt, "Integer Registry " + "Value ",1);
	    }
	    catch (NoSuchRegistryKeyException e)
	    {
	        JOptionPane.showMessageDialog(null, "Error finding registry key.");
	        System.out.println ("No Such Registry Key Exception. (" + e + ")" );
	    }
	}
	}

Functional Tester 用户可使用两个命令来从注册表读取值。getRegistryValue 命令用于从注册表读取字符串值。getRegistryIntValue 用于从注册表读取整数值。术语“REG_SZ”描述字符串和整数类型。两个命令都采用包含要抽取的注册表键的 String 类型参数。

注: 输入键时,“\”是 Java™ 中的特殊字符且必须重叠为“\\”,才能被当作字面值。

示例从注册表抽取字符串和整数值。 首先查看 String 值段,请注意核心代码:

String regKeyString ="HKEY_LOCAL_MACHINE\\SOFTWARE\\Rational Software\\Rational Test\\8\\Rational FT Install Directory";
String regValueString = getOperatingSystem().getRegistryValue(regKeyString);
JOptionPane.showMessageDialog(null, regValueString,"String Registry Value",1);

第一行创建包含要抽取的注册表值的 String 类型变量。第二行执行该命令并将其存储在 String 类型变量 regValueString 中。 第三行使用 JOptionPane.showMessageDialog 类在屏幕上的消息框中显示注册表值。用户可能不熟悉最后这个类,它是 Java Swing 类,必须予以导入才能用于 Functional Tester。请注意脚本顶部的最后 import 语句。

第二段抽取 int 类型值。在该示例中,将简单类型 int 转换为 Integer 对象,以便在 JOptionPane 对话框中可以显示该对象。否则,该代码与第一段相同。

两个命令在失败时都抛出 NoSuchRegistryKeyException。因此,如该示例中所示,将这些方法放在 try/catch 块中比较恰当。可以将注册表键更改为不存在的注册表键,然后运行脚本。您将看到指示找不到该键的错误消息。


反馈