The Environment
object can be best described as a kind of Dictionary that holds pairs of keys and values. Unlike the native Dictionary, it has extended capacities such as the following:
ActionName
, ActionIteration
, TestName
, TestIteration
, TestDir
, and OS
Proceed with the following steps:
ActionIteration
and ActionName
, while the variables OS
, TestName
, and TestDir
are examples for such values that UFT can retrieve, independent of its running state:Retrieving the values of built-in variables during runtime is done using code such as the following:
Print Environment("ActionIteration")
This prints the value of the current Action
iteration (when runs with input data from the local DataSheet
). It is possible, of course, to use such variables to control the flow, as is shown in the Importing an Excel file to a test recipe in Chapter 1, Data-driven Tests.
Built-in variables are read-only.
Environment
variables according to the requirements or needs. It is important to keep in mind that, being an object having global scope, the Environment
object is very useful to store configuration data that is used across tests (for example, website URL, super username and encrypted password, and so on). Though technically feasible, it is not really recommended to use this object as a means to store runtime data that needs to be shared across actions. For that purpose, using a globally defined Dictionary would be much more suitable.To add, click on the + icon. The Add New Environment Parameter dialog will pop up. Enter a variable name and value in the appropriate fields, and click on the OK button:
Add two variables, myvar
and Myvar
. The next screenshot shows that variable names are case sensitive:
As you can see, the third column labeled Type indicates that the variables are Internal
. This means that the variables are specific to the current test.
To delete, select the second variable and click on the x icon. The following dialog will appear:
To edit, in order to change the value of a variable, select it and click on the edit icon. The Edit Environment Parameter dialog will appear:
Edit the value as per your will and click on OK to approve. Your changes will be kept and seen on the variables list in the Test Settings dialog.
Though the Name field appears to be enabled, it is actually a read-only field. This means that one can only change the value of an Environment
variable, not its name. This is important to avoid problems related to existing code, which already refer to previously defined Environment
variables. Still, if one wishes to make such a name change, the option of adding a new variable and deleting the old one is always open. Just keep in mind that you may need to make changes to your code.
External
. Consistent with the logic of reusability, these variables cannot be changed from within the Test Settings dialog. They are read-only. You can check this by clicking on the edit icon or instead, by using the Edit Environment Parameter dialog, you get the View Environment Parameter dialog. The fields are both read-only:It is also possible to import an XML file during runtime. The syntax is as follows:
Dim sFilePathname = "C:AutomationConfigEnv_1.xml" Environment.LoadFromFile sFilePathname
Also, if you load it before the test starts to run (using the AOM with an external VBS file, for instance), then the optional Boolean
argument, KeepLoaded
, is also required:
Environment.LoadFromFile sFilePathname, True
Otherwise, the variables and values will be lost later.
Environment("MyVarName") = "MyVarValue"
This can be easily done with code as follows:
Print Environment("MyVarName")
If the variable does not exist, an error, as shown in the following screenshot, will pop up:
The previous section was quite thorough in describing the workings of the Environment
object, so here we will summarize.
We have seen the main uses of the Environment
object as a way to define variables that are required during the run session. We have described how to add new persistent variables and delete/edit existing ones using the test settings during design time. We have also explained how, during runtime, one can create new variables and change their values, as well as how to retrieve the values of any Environment variable (be it Internal, External, or runtime).
Finally, we discussed the features of Export and Import, stressing that this is how we attain reusability of required configuration variables across tests.