site
stats
By Anish Pillai Anish Pillai Posted under QTP Basic Stuff | QTP Concepts

2 Different Ways to Create Global Variables in QTP

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Did you ever face any situation where you needed to create global variables in QTP so that these global values can be used across multiple actions/functions within your test script? How did you tackle the problem then? If you would have noticed, QTP doesn’t provide any direct method to create global variables. But you can use any of the below 2 methods to create variables with Global Scope in QTP.

By using Environment Variables

This is the most commonly used method to create variables in QTP with global scope. All you have to do is just create a User-Defined Environment variable anywhere in your script (be it at the beginning of your action, or even inside any of your functions). That’s all. Once your environment variable is created, it would be available throughout your test script (inside all your actions & functions within the test script). Example:

'Declare Environment Variable (Global Variable)
Environment.Value("gloVarBlogName") = "Automation Repository"


'Use Environment Variable (Global Variable)
msgbox Environment.Value("gloVarBlogName")

By using Function Library

You can also use a function library to create and use variables with global scope in your QTP script. This is how it can be done.

1. Create a function library(.vbs file) and associate it to your test script.

2. In your function library, declare a variable & assign required value to it. Example:

'Declare variable in Function Library
Dim gloVarBlogName
gloVarBlogName = "Automation Repository"

Once you have defined the variable in the function library & associated the function library with your test script, you can then use this variable anywhere in your script. For Example, writing msgbox gloVarBlogName in your action would display a message box with text – Automation Repository

Other than the above two methods you can also store your variables in some external file (such as excel sheet, text file,  xml file etc) and then you can write code to access these variable values from the external file. Usually this method is not used much because anyways you can get the job done with environment variables & function library with much less code.

 

If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×