QTP – Environment Variables

A test automation suite might be containing lot of tests / actions. Hence, while executing the test cases in batch mode we might need to refer to the Iteration, name of the test, Results directory, etc. But how to get them? QTP provides global variables called Built-in Environment Variables through which we can access these data.

There is one more type of environment variable that is User-defined Environment Variable.

Below are the different Built-in Environment Variables available:

  1. ActionIteration
  2. ActionName
  3. LocalHostName
  4. OS
  5. OSVersion
  6. ProductDir
  7. ProductName
  8. ProductVer
  9. ResultDir
  10. SystemTempDir
  11. TestDir
  12. UserName
  13. TestName
  14. TestIteration
  15. UpdatingActiveScreen
  16. UpdatingCheckPoints
  17. UpdatingTODescriptions
  18. ScenarioId
  19. VUserId
  20. ControllerHostName
  21. GroupName

I do not think that explanation is needed for these variables as they are self explanatory.

Let us execute the below script and see the result.

spc=20
EnvVal="Env Variable"&space(spc+spc)&"Value"&vbnewline
EnvVal=EnvVal&"______________________________________________________________"&vbnewline
EnvVal=EnvVal&"ActionIteration"&space(spc+spc)&Environment.Value("ActionIteration")&vbnewline
EnvVal=EnvVal&"ActionName"&space(spc+spc+2)&Environment.Value("ActionName")&vbnewline
EnvVal=EnvVal&"LocalHostName"&space(spc+spc-4)&Environment.Value("LocalHostName")&vbnewline
EnvVal=EnvVal&"OS"&space(spc+15)&Environment.Value("OS")&vbnewline
EnvVal=EnvVal&"OSVersion"&space(spc+24)&Environment.Value("OSVersion")&vbnewline
EnvVal=EnvVal&"ProductDir"&space(spc+4)&Environment.Value("ProductDir")&vbnewline
EnvVal=EnvVal&"ProductName"&space(spc+13)&Environment.Value("ProductName")&vbnewline
EnvVal=EnvVal&"ProductVer"&space(spc+28)&Environment.Value("ProductVer")&vbnewline
EnvVal=EnvVal&"ResultDir"&space(spc+5)&Environment.Value("ResultDir")&vbnewline
EnvVal=EnvVal&"SystemTempDir"&space(spc+8)&Environment.Value("SystemTempDir")&vbnewline
EnvVal=EnvVal&"TestDir"&space(spc+7)&Environment.Value("TestDir")&vbnewline
EnvVal=EnvVal&"UserName"&space(spc+15)&Environment.Value("UserName")&vbnewline
EnvVal=EnvVal&"TestName"&space(spc+15)&Environment.Value("TestName")&vbnewline
EnvVal=EnvVal&"TestIteration"&space(spc+25)&Environment.Value("TestIteration")&vbnewline
'EnvVal=EnvVal&"UpdatingActiveScreen"&space(spc)&Environment.Value("UpdatingActiveScreen")&vbnewline
'EnvVal=EnvVal&"UpdatingCheckPoints"&space(spc)&Environment.Value("UpdatingCheckPoints")&vbnewline
'EnvVal=EnvVal&"UpdatingTODescriptions"&space(spc)&Environment.Value("UpdatingTODescriptions")&vbnewline
'EnvVal=EnvVal&"ScenarioId"&space(spc)&Environment.Value("ScenarioId")&vbnewline
'EnvVal=EnvVal&"VUserId"&space(spc)&Environment.Value("VUserId")&vbnewline
'EnvVal=EnvVal&"ControllerHostName"&space(spc)&Environment.Value("ControllerHostName")&vbnewline
'EnvVal=EnvVal&"GroupName"&space(spc)&Environment.Value("GroupName")&vbnewline

print  EnvVal

EnvVariableResult

In the code above, I have commented out some of the variables because they contain blank or no values.

User-defined Environment Variable:

These act more or less like global variables in other programming / scripting languages.  Some of the examples of these are as below:

  1. Maximum wait period
  2. Framework root folder
  3. Application username / password

How to set User-defined Environment Variable?

Click File –> Settings –> Environment Tab

Select User-defined from the Variable type dropdown

UserDefined

CreateVariable

varscreated

As per the above figures, we have created two variables username and password.

To refer these variables in the test action, we need to use Environment.Value("variable name")

There is one more way of adding user-defined environment variables dynamically that is using xml file.

Create a xml file containing variables, then load the variables by uploading the external xml file.

To do this, check the checkbox “Load the variables and value from external file” and then upload the corresponding file.

<Environment>
    <Variable>
        <Name>username</Name>
        <Value>admin</Value>
    </Variable>
    <Variable>
        <Name>password</Name>
        <Value>mercury</Value>
    </Variable>
    <Variable>
        <Name>MaxWait</Name>
        <Value>120</Value>
    </Variable>
</Environment>

Save this file as “ScriptsSettings.xml” and then upload the file.  Then, click Apply and OK.

ExternalXMlvariables

Now, we can see the variables inserted into the table. This is an excellent feature provided by the QuickTest Professional, which needs to be utilized whenever necessary.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.