How to pass variable from one Action to Other in UFT

22,242

Solution 1

Version: UFT 12.51

Use the syntax: RunAction strActionName, oneIteration, Param1, Param2

  1. The Input Parameters <Param1>, <Param2> is declared in your first Action, I called it Action1.
  2. Pass the value to your Action1 local variable, example: Param1 = "a1_var1 --> from Action 1"
  3. Execute the RunAction command to pass the value, RunAction "Action2", oneIteration, Param1
  4. In Action2, I retrieved the Parameter Value and store in Param2 Action 2 Local variable, Param2 = Parameter("Param1")

Refer to screenshot.
enter image description here

Solution 2

The easiest way is to use Environment variable, but you can also set input Parameter to Action2 and invoke it in Action1:

RunAction "Action2", oneIteration, parameter1,parameter2

Other possible solution is TestArgs("argumentName") - it's the same like Parameter but scope of TestArgs is for whole TestSet. Parameter is for given Action only

Solution 3

In first action, you must initialize enviroment value:

Environment.Value("yourName") = yourVariable

and If you want to call that value, write simply

yourVarFromSecondAction= Environment("yourName")

As @HgCoder said you can use implemented excell sheet, but I had got some problems when I worked in connection with ALM, so be careful.

Solution 4

The variables you declare in an action are scoped to be available only to that action, so you have to find something with global scope to make it available to both actions.

Some options...

  1. Create a function library with public variables that will hold your data
  2. Store the value in an Environment variable (see QTP help for details on user-defined value in Environment)
  3. Store the value in the Global data table

Each of these options make the data available to all the actions in your script.

Solution 5

You can pass arguments from one action to another which is reusable, but for that please follow the below steps;

  1. from within the action, go to action properties(View -> Properties)
  2. Go to parameters from action properties
  3. Add input parameter to the same

Now you can pass the number of parameters from the calling action;

RunAction strActionName, oneIteration, intIteration*

You can access the passed argument by;

var_to_hold_arg = Parameter("passed_arg")

Share:
22,242
user3435903
Author by

user3435903

Updated on July 27, 2022

Comments

  • user3435903
    user3435903 almost 2 years

    I have Action 1 where i am querying the db and getting a field which i am storing in the variable (lets say x). In Actions 2, i am again querying the db where i want to use the value stored in x in the where clause.

    Snippet from Action 1:

    SQL1 =  "   SELECT      Identitynumber " &_
            "   FROM        VLC_CRM.dbo.Person " &_
            "   WHERE       Identitynumber = '" &IdentityNumber_ui&"'"
    
    
    Set oRecordSet = oConnection.Execute(SQL1)
    
    Do While NOT oRecordSet.EOF
        IdentityNumber_db_tmp = CStr(oRecordSet.Fields("Identitynumber").Value)
        IdentityNumber_db = RTrim(IdentityNumber_db_tmp)
        oRecordSet.MoveNext
    Loop
    

    Action 2 SQL (i want to use IdentityNumber_db from Action 1 in my Where Clause below)

    SQL1 =      "   SELECT  TOP 1   CAST(la.LogonDate AS DATE) AS LogonDate," &_
                "                   la.LogonDate AS LastLogonDateTime"&_
                "   FROM            dbo.LogonAudit la" &_
                "   INNER JOIN      dbo.Person p" &_
                "   ON              la.EntityID = p.PersonID" &_
                "   WHERE           p.IdentityNumber = '"&IdentityNumber_db&"'"  &_
                "   ORDER BY        LastLogonDateTime DESC"
    

    Can someone please suggest how i can pass this value from Action 1 to Action 2 so i can use it in my SQL query?