How To: Read data from the Windows Registry
Hello all,
I have seen a few posts here about registry access and editing. This is how I retrieve data from the Windows registry using JavaScript. I am using this to return the OS information and application path information. This is very useful when added to the EventControl_OnStartTest event code. This will allow you to return OS information and other needed data at each test run. Some test management systems may provide this information for you or it may be logged in the in data produced in a pipeline run. This will embed the information directly into your test log.
SmartBear KB Links:
- Storages Object
- Storages Object Methods
- Storages.Registry Method
- Section Object
- Get SubSection Method
This bit of code will return the Product Name and Current Build from the registry. This location may vary between OS's so you will want to check this with RegEdit.
let Section = Storages.Registry("SOFTWARE\\Microsoft\\Windows NT", HKEY_LOCAL_MACHINE);
let regKeyString = Section.GetSubSection("CurrentVersion").Name;
let productIdString = Storages.Registry(regKeyString, HKEY_LOCAL_MACHINE, 1, true).GetOption("ProductName", "");
let currentBuildString = Storages.Registry(regKeyString, HKEY_LOCAL_MACHINE, 1, true).GetOption("CurrentBuild", "");
Log.Message("Windows Version: " + productIdString + " Build: " + currentBuildString )
I have also found the need to find and set an application path and work folder in the project TestedApp for running through a pipeline because the pipeline deploys the application to a non-standard path.
let Section = Storages.Registry("SOFTWARE\\WOW6432Node\\<_yourSectionName>\\", HKEY_LOCAL_MACHINE);
let regKey = Section.GetSubSection(<_yourSubSectionName>).Name;
let Path = Storages.Registry(regKey, HKEY_LOCAL_MACHINE, 0, true).GetOption("", "");
let WorkFolder = Storages.Registry(regKey, HKEY_LOCAL_MACHINE, 0, true).GetOption("Path", "");
let appIndex = TestedApps.Find(<_yourAppName>);
if (appIndex >= 0){
if(TestedApps.Items(<_yourAppName>).Path != Path){
TestedApps.Items(<_yourAppName>).Path = Path
}
if(TestedApps.Items(<_yourAppName>).WorkFolder != WorkFolder){
TestedApps.Items(<_yourAppName>).Params.ActiveParams.WorkFolder = WorkFolder;
}
}
else{
Log.Error("TestedApp " + <_yourAppName> + " does not Exist.")
Runner.Stop(true);
}
I hope you find these links and code examples as useful as I have!
Have a great day!
Scott Holmes
WW Wood Products Inc.