Reading a key from the Windows® system registry

Reading registry keys and values is an important task. For example, it can assist us in testing the correctness of an application installation process, or in reading specific settings of applications or even operating system environment variables. In this recipe, we will see how to read the value of a key from the registry, specifically the Java Options environment variable.

Getting ready

From the File menu, navigate to New | Test or use the Ctrl + N shortcut.

How to do it...

The syntax to retrieve the value of a registry key is as follows:

Registry_ReadKey

Here, object is an instance of the Wscript.Shell class and sKeyPath, a valid key.

An example of how to retrieve the value of the _JAVA_OPTIONS environment variable is as shown in the following code snippet:

Dim oWshShell, sKeyVal, sKeyPath, sKeyExpected

sKeyPath = "HKEY_CURRENT_USEREnvironment\_JAVA_OPTIONS"
Set oWshShell = CreateObject("Wscript.Shell")
sKeyVal = oWshShell.RegRead(sKeyPath)

Print sKeyVal

Set oWshShell = Nothing

We can verify the correctness of the retrieved value using a simple conditional structure (assuming that the expected value appears in the action's local datasheet):

sKeyExpected = DataTable("Expected_Key", dtLocalSheet)

If sKeyVal <> sKeyExpected Then
Reporter.ReportEvent micFail, "Registry Verification", _
"Actual Key: " & sKeyVal &" differs from Expected Key: "& _
sKeyExpected
End If

Note

The Windows OS stores the names and values of its environment variables (not to be confused with UFT's environment variables) under the registry key HKEY_CURRENT_USEREnvironment.

How it works...

We simply create an instance of the WScript.Shell class, and use the RegRead method to retrieve the value of a valid registry key.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset