问题
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.
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
回答1:
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)
回答2:
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.
来源:https://stackoverflow.com/questions/30845177/how-to-get-test-variable-through-custom-code-in-hp-uft-apitest