How to get Test variable through custom code in HP UFT - ApiTest

5.2k views Asked by At

I want to set value to the Test Variable using Custom Code. Can you tell me how to do it as i am not able to access the variable from code.

UFT profile variable

I need to access the User Variable URL in my custom code and set the value.

Please help me if you know how to do it using Custom Code.

Thanks, Madhan

2

There are 2 answers

0
ManishChristian On BEST ANSWER

Based on your question:
1. Click on File menu and then Settings, it will open Properties pane.
2. Click on "+" to add user variable and give a name.
3. In your custom code, type below code:

  string s = "https://www.google.com/";
  this.Context.TestProfile.SetVariableValue("NameOfYourVariable",s);

To retrieve the value of given variable:

  string ss = this.Context.TestProfile.GetVariableValue("NameOfYourVariable");
  CodeActivity5.Report("Variable is : ", ss); //(This line will print your variable value)
1
csekar On

answer provided here is valid when the need is to set string values. The "SetVariableValue" method takes two "String" type parameters. This limits the ability to set Int32 type variables. Obviously, C# throws an error when trying to set an integer value.

Now, an integer value is particularly useful while setting values for loop iterations. I am not sure whether this is a limitation of the tool or whether my lack of knowledge. So, to work around this, i used the output property of custom code activity. To do this, create a custom code activity and create an output property of the desired type, say Int. Now, assign a value to this output property using the line:

this.ActivityName.Output.property name = property value

This is available in UFT help and can be useful while trying to pass values other than string between different activities in a flow.