How to create global variable in QTP to be used in all actions

qtp
20,811

The two quickest options for variables with global scope (though there are other ways of passing data around):

Environment variables

Assign anywhere like so:

Environment("myVar") = "Hello there"

Retrieve anywhere like so:

x = Environment("myVar")

Declare variables in library files

Declare a variable in an associated library file and it will be accessible anywhere in your test.

Declare in an attached library file:

Dim foo

Assign anywhere like so:

foo = "bar"

Retrieve anywhere like so:

x = foo

Option 1 is probably preferable from a code-maintainability standpoint, as you can pre-define the environment variables you expect to use beforehand as "user-defined environment variables" rather than just magicaly creating global vars in obscure places.

You also have the option of using the DataTable to pass things around. Read the manual for that one.

Share:
20,811
user2669118
Author by

user2669118

Updated on August 26, 2020

Comments

  • user2669118
    user2669118 almost 4 years

    Can someone tell me how do we define a variable in QTP which would store information at runtime in one action and then variable would be used in other actions from same test.

  • Xiaofu
    Xiaofu almost 11 years
    UFT/QTP encourages some weird programming practices due to its structure and strange scoping rules, but please try and avoid global variables where possible if doing 'normal' programming. If using them in UFT make sure they are clearly documented along with what Actions depend on them.
  • user2669118
    user2669118 almost 11 years
    Thank you very much however the first option wouldn't work for me but library files option was good to go in my case.
  • TheBlastOne
    TheBlastOne almost 11 years
    Then accept the answer that you found to be most useful. Please!