Service Test 11: Programmatically Set and Get Test Variable Values

Test Variables in Service Test 11 are global variables that can be accessed later in the test script, either by linking to the variable in the GUI (Link to a Data Source) or by setting the variable in the GUI (Set Test Variable activity). Both can also be done programmatically using C#. For example, it may be necessary to set a Test Variable inside an activity Event (instead of using the Set Test Variable activity). In this post, we cover the methods available within Service Test to set and retrieve Test Variables.

Create a Test Variable

Before we can programmatically manipulate test variables, we must first declare (add) a Test Variable.

  1. Click an empty area in the Test Flow.
  2. In the Test Property Sheet, click the Test Variables button.
  3. Click the + button.
  4. Give the User Variable a name. In this example, we are going to use Flight_Number.

That’s all there is to it! Now we can use the Test Variable named Flight_Number anywhere in this Service Test script.

Set a Test Variable Value

The C# code below will set a Test Variable declared within the test Context. This code can be used within an activity Event or within a Custom Code activity:

this.Context.TestProfile.SetVariableValue("Flight_Number","<value>");

Or, access the Test Profile variable value by accessing the Context of the activity name (e.g., CodeActivity8):

this.CodeActivity8.Context.TestProfile.SetVariableValue("Flight_Number","<value>");

Get a Test Variable Value

The C# code below will retrieve a Test Variable declared within the test Context. Just like setting a Test Variable, the code below can be used within an activity Event or within a Custom Code activity:

this.Context.TestProfile.GetVariableValue("Flight_Number");

Or, access the Test Profile variable value by accessing the Context of an activity name (e.g., CodeActivity8):

this.CodeActivity8.Context.TestProfile.GetVariableValue("Flight_Number");

Conclusion

Remember that you should only programmatically set or retrieve Test Variables when it is not possible to accomplish the same task within the Service Test GUI. Use the Set Test Variable activity (Miscellaneous) or Link to a Data Source (Test Variable) when possible. Keep it simple.

Don’t hesitate to ask questions or let us know if you have any issues by posting a comment below!

What's Next?

Did you enjoy this article? Help spread the word by sharing:

Join the Northway Navigator Club today and get access to restricted content including our best tips and tricks. Membership is free! You will also receive free email updates by registering.

Engage in the conversation and leave a comment:

Brian MacKenzie

About Brian MacKenzie (24 articles)




Brian has over 14 years of experience working in IT, ranging from Software Quality Assurance, Software Development, Business Management to Systems Administration, in a wide range of technologies, languages, databases, systems and software. He has experience in both software quality assurance and software development roles. Brian specializes in HP LoadRunner, HP UFT (QuickTest Pro and Service Test), Mobile Testing and Automation, HP ALM and HP Service Virtualization, as well as delivering certified instructor training in HP UFT and mentoring in Service Test and Service Virtualization.



  • Swapnil

    I can link a data source value to a environment variable on GUI. but how do I achieve this programmatically in Custom code of HP Service Test?

    In your example you are assigning a to a variable FlightNumber but my is in a datasource.

    • http://northwaysolutions.com/ Brian MacKenzie

      You can access the value of a column in a datasource by referencing the QueryID. If you look at the “Data Navigation” section of the Test Flow, one of the columns is the QueryID.

      From within custom code, the value can be obtained via the following code:
      this..GetValue(,);

      Row number is a numeric value and is optional.
      Column name is a string and is required.

      • Swapnil

        Thanks for your reply.

        I have tried this but I am getting so many compilation errors.
        I have written like below(column1 is a column of my datasource)

        this.Query4.GetValue(column1);

        I am getting error The name ‘column1′ does not exist in the current context (CS0103) -

        tried declaring column name earlier this line then I am getting typecasting error as Error CS0266

        • http://northwaysolutions.com/ Brian MacKenzie

          The correct syntax is:
          this.Query4.GetValue(“column1″);

          Did you include the quotes?

          • swapnil

            yeah it worked. thanks.

          • http://northwaysolutions.com/ Brian MacKenzie

            You’re welcome!

          • Swapnil

            Hey Brian,

            I want to capture one field from my webservice response in one variable.

            Below is my response

            0

            0

            0

            0

            0

            0

            TMPONE, PBRONE

            1

            N

            1

            0

            0

            I have written below code.

            I have written below code onreceive response event handler

            XmlNode
            PrescriberInfoNode =
            this.StServiceCallActivity6.OutputEnvelope.SelectSingleNode(“/*[local-name(.)='Envelope'][1]/*[local-name(.)='Body'][1]/*[local-name(.)='PSPRSInqSvcC2Response'][1]/*[local-name(.)='recvBuffer'][1]/*[local-name(.)='prescriber-info'][1]“);

            string PrescriberInfo= PrescriberInfoNode.InnerText;

            MessageBox.Show(PrescriberInfo);

            —-
            When I run this script. it is runnig but I am not getting message pop up.
            What could be wrong??

          • http://northwaysolutions.com/ Brian MacKenzie

            MessageBox.Show in service test can often appear minimized in the task bar. It is probably displaying, but you just didn’t realize it.

            Set a breakpoint on MessageBox.Show and confirm that the test breaks there and that you can proceed with the remainder of the test.

            Alternatively, use the built in reporting function which appears in the run results: args.Activity.Report({SUBJECT}, {{DESCRIPTION});

  • Brian MacKenzie

    Yes, it is possible.

  • phathaway

    When I set breakpoints in my custom code and run the test, the test completes without stopping at any of the breakpoints. Is there anything special which I need to do in order for UFT to acknowledge the breakpoints?

    thanks
    Pete