Reading the Windows Registry with Functional Tester

The Windows registry is a database used by the Windows operating system to store configuration information. Often it becomes necessary for a tester to read information out of this database using Functional Tester commands. The following example is applicable for scripts running on Windows:

import resources.RegistryExampleHelper;
import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
import javax.swing.JOptionPane;


/** * Description : Functional Tester Script * @author Administrator */ public class RegistryExample extends RegistryExampleHelper {     /**     * Script Name   : RegistryExample     * Generated     : Dec 14, 2005 3:58:25 PM     * Modified      : Dec 14, 2005 3:58:25 PM     * Description   : Functional Tester Script     * Original Host : WinNT Version 5.0 Build 2195 (Service Pack 3)     *     * @since 2005/12/14     * @author Administrator     */     public void testMain (Object[] args)        {           setOption (IOptionName.BRING_UP_LOGVIEWER, false);           try        {           //Use this code to extract String (REG_SZ) values from the registry.           String regKeyString =HKEY_LOCAL_MACHINE\\SOFTWARE\\              Rational Software\\Rational Test\\8\\Data 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 + ")" );        }        } }

There are two commands available to Functional Tester users to read values from the registry. The getRegistryValue command is used to read string values from the registry. The getRegistryIntValue is used to read integer values from the registry. The terms "REG_SZ" describe the string and integer types. Both of the commands take a type String argument, which contains the registry key to extract.

Note: When entering keys, the "\" is a special character in Java and must be doubled to "\\" to be taken as a literal.

The example extracts both a string and an integer value from the registry. Looking first at the String value segment, notice the core code:

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

The first line creates a type String variable, which contains the registry value to extract. The second line executes the command and stores it in the type String variable regValueString. The third line uses the JOptionPane.showMessageDialog class to display the registry value in a message box on the screen. For those unfamiliar with this last class, it is a Java Swing class, which must be imported to be available to Functional Tester. Note the last import statement at the top of the script.

The second segment extracts the type int value. In the example, the simple type int is converted to an Integer object, so that it can be displayed in the JOptionPane dialog. Otherwise, the code is identical to the first segment.

Both of the commands throw a NoSuchRegistryKeyException when they fail. Therefore, it is a good idea to wrap these methods within a try/catch block, as in the example. You can change the registry key to one that does not exist and run the script. You will see an error message indicating the key could not be found.

Terms of use | Feedback
(C) Copyright IBM Corporation 2002, 2004. All Rights Reserved.