Reading the Windows registry
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 HCL OneTest™ UI commands. This topic provides examples for doing this.
The following example is applicable for scripts running on 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 + ")" );
}
}
}
There are two commands available 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.
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\\Rational FT Install 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. 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.