Book Image

Advanced UFT 12 for Test Engineers Cookbook

Book Image

Advanced UFT 12 for Test Engineers Cookbook

Overview of this book

Table of Contents (18 chapters)
Advanced UFT 12 for Test Engineers Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Reading values from an INI file


Files with the extension .ini are the legacy of the old Windows versions (16 bit). In the past, they were extensively used—and still are to some extent—ubiquitously to store the settings for applications. Nowadays, it is common practice to store settings in the registry. Though textual, such files have a very well-defined structure; there are sections and key-value pairs. A section starts with a label enclosed in square brackets: [section-name] and a key-value is implemented as <variable name>=<value>. Such a structure could be useful, for instance, if we wanted to keep the settings organized by environments or by user profiles within an.ini file.

Note

In this recipe, you will also see an example of how to use the Extern reserved object to define references to methods in external DLLs, such as those of the Win32API. These methods can then be loaded and executed during runtime. A more elaborate description is available in the Drawing a rectangle on the screen with Win32 API methods (Extern) recipe of Chapter 8, Utility and Reserved Objects.

Getting ready

To complete this recipe, we need to use the global Extern object, which with proper use provides the UFT with access to the methods of an external Dynamic Link Library (DLL). We will define a variable and assign it a reference to the global Extern object (this is done to avoid persistence, as Extern is a reserved object not released from memory until UFT closes):

Dim oExtern
set oExtern = Extern

Then, we will declare the method or methods we wish to call from the relevant Win32API. In this case, the method is GetPrivateProfileString, which retrieves the value of a given key within a specific section:

oExtern.Declare micInteger,"GetPrivateProfileString", "kernel32.dll","GetPrivateProfileStringA", _
               micString, micString, micString, micString+micByRef, micInteger, micString

How to do it...

After defining the connection to the DLL with its returned value and arguments, we can retrieve the value of any key within a given section. In the following example, the ConfigFileVersion key specified in the file wrls.ini is located in the UFT/bin folder. In the end, the Extern object reference is destroyed at the end of the run:

call oExtern.GetPrivateProfileString("ProgramInformation", "ConfigFileVersion", "", RetVal, 255, "C:\Program Files\HP\Unified Functional Testing\bin\wrls_ins.ini")
print  RetVal

set oExtern = nothing

The output to the console in this case was the string 1.05.