Writing a key to the Windows® system registry

The purpose of writing registry keys and values is so that they can be used within automated tests. The reason is that, in general, automation should refrain from making changes to the operating system, and this includes the registry. This is necessary to keep our automated tests as less invasive as possible. However, it is important to know how this can be accomplished.

To write a value to a custom registry key that passes UFT data gathered by a process running in parallel to the automated test, UFT uses the RegRead method at the appropriate time to retrieve the data. This allows, in parallel, the asynchronous usage of the testing tool and external executables.

In this recipe, we will see how to write a custom key to the registry.

Getting ready

From the File menu, navigate to New | Test, or use the Ctrl + N shortcut. You can use the same test as in the previous recipe.

How to do it...

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

object.RegWrite sKeyPath, sKeyVal, sKeyType

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

An example of how to write a new custom string (REG_SZ) type key is shown using the following code snippet:

Dim oWshShell, sKeyPath, sKeyVal, sKeyType

sKeyPath = "HKEY_CURRENT_USERMyCustomKeyMyCustomDataMyValue"
sKeyVal = "This is a UFT made registry key"
sKeyType = "REG_SZ"

Set oWshShell = CreateObject("Wscript.Shell")
oWshShell.RegWrite sKeyPath, sKeyVal, sKeyType

sKeyVal = oWshShell.RegRead(sKeyPath)

Print sKeyVal

Set oWshShell = Nothing

How it works...

We simply create an instance of the WScript.Shell class and use the RegWrite method to write the value of our custom registry key. We then use RegRead to print the value to the output pane.

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

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