3

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.

user2669118
  • 41
  • 1
  • 1
  • 5

2 Answers2

4

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.

Xiaofu
  • 15,523
  • 2
  • 32
  • 45
  • 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. – Xiaofu Aug 16 '13 at 05:24
  • Thank you very much however the first option wouldn't work for me but library files option was good to go in my case. – user2669118 Aug 16 '13 at 15:13
  • Then accept the answer that you found to be most useful. Please! – TheBlastOne Sep 09 '13 at 21:09
0

You can specify that an action has an output parameter and the consume this parameter in later actions.

Motti
  • 110,860
  • 49
  • 189
  • 262