Forum Discussion

rajvansh's avatar
rajvansh
New Contributor
10 years ago
Solved

Is it possible to set the Default values and Local values of a project variable by a function call?

Please consider the following scenario:

 

1.We created a function abc (temp) to retrun a string (say testvalue).

2. We added avariable with Name=Var1, Type=String,  Default Value= abc  ("xyz"), Local Value= abc  ("xyz")

3. We printed the value for variable Var1.

 

Expected:

String "testvalue" should be printed.

 

Actual:

abc  ("xyz") is printed. 

 

Is there a possibility of setting variable value via functiuon call?

 

  • I do something similar using event handlers, I think the following should work:

     

    Create a new event for OnStartTest, this should give you a function like:

    function GeneralEvents_OnStartTest(Sender){
    
    }

     

    In the same file (or elsewhere and import it) you can add your function:

    function abc(){
      return "testvalue";
    }

    Now back in the event handler, you can set your project variables like:

    function GeneralEvents_OnStartTest(Sender){
      Project.Variables.Var1 = abc();
    }

    At the start of every test item, the Var1 value will be set to the return value of abc(); So when you print the value of Var1 in your test item, it should print "testvalue";

     

    I havent reproduced the code locally, but I am fairly confident this works. 

     

1 Reply

  • I do something similar using event handlers, I think the following should work:

     

    Create a new event for OnStartTest, this should give you a function like:

    function GeneralEvents_OnStartTest(Sender){
    
    }

     

    In the same file (or elsewhere and import it) you can add your function:

    function abc(){
      return "testvalue";
    }

    Now back in the event handler, you can set your project variables like:

    function GeneralEvents_OnStartTest(Sender){
      Project.Variables.Var1 = abc();
    }

    At the start of every test item, the Var1 value will be set to the return value of abc(); So when you print the value of Var1 in your test item, it should print "testvalue";

     

    I havent reproduced the code locally, but I am fairly confident this works.