≡ Menu
Joe Colantonio – Selenium-UFT-QTP-SoapUI-ALM-LoadRunner & more

How I cracked HP Service Test’s custom code functionality

UPDATE: HP has changed the syntax for input and output variables in ST 11.10. Check out UPDATE: Service Test 11.10: How to Create Custom Code using Service Test 11.10 for more info.

HP’s documentation on Service Test custom code functionality is pretty slight. Reading the user guide I was unable to find an exact example of how to pass data to and from my custom code. The key to cracking the custom code feature is to become familiar with the args object, as well as two of its properties: Input and Output.

These two properties are what you need to use in order to pass value to and from ST’s custom code. So — to assign a value to a input variable that you created, you need to use the following statement:

args.Input.theNameOfYourInputVariable;

And to assign a value to a variable for later use in your script, you would use:

args.Output.theNameOfYourOutputVariable;

The easiest way for me to learn something is to do it. The following is a small example I came up with to help me understand how to use Service Test’s custom code Feature:

1. Create a text file on your local drive called myCounter.txt

2. Open the text file and add the value of 100. Save and Close.

3. Open Service Test 11

4. Under ST’s ToolBar, navigate to the File System folder. Drag the ‘Read from File” objects onto your test’s main canvas area.

ST11 read from file

 

 

 

 

 

 

 

 

 

5. Go to the Custom Code’s property sheet and click on the “test input parameters” button (The Step Icon).

6. Point your File path to the location of the myCounter.txt file. Set “Encoding” to Automatic

7. Go to the Miscellaneous folder under ST’s Toolbox section, and drag the Custom Code on to your test’s canvas area.
Service Test Custom Code Menu

 

 

 

 

 

8. Go to the Custom Code’s property sheet and click on the “test input parameters” button (The Step Icon).

9. Click on the “Add Property” button (The + icon) and select the “Add Input Property” option from the menu

10. In the Add Input Property, enter the following:

Type = int<br /> Name = inCounter<br /> Description = counter example

In Counter in Service Test

 

 

 

 

 

11. Click OK (You should now have a value called inCounter located under the Input\Properties section).

12. For the inCounter value, point the Read From File’s ‘Content’ value (this will contain the value that is read from the myCounter.txt file)

13. Click on the Add Property button (The + icon) and select “Add
Output Property” option from the menu.

14. In the Add Output Property fill out the following:

 Type = int<br /> Name = outCount<br /> Description = Adds 1 to the current count<br />

Out Property Service Test 11

 

 

 

 

 

15. Click OK (your Custom Code’s property sheet should now have the following:

 

Service Test Param

 

 

 

 

 

 

 

 

 

16. In the Custom Code’s property sheet, click on the “Events”
(thunderbolt) button.

17. Click on “ExecuteEvent” and select Create a
default handler.

18. A SharedUserCode window should appears in ST. Click on it, then
navigate to the CodeActivity_OnExecuteEvent section and add the following code:

<br /> into myCounter:<br /> myCounter = args.Input.inCounter;<br /> myCounter = myCounter + 1;<br /> args.Output.outCounter = myCounter;

Service Test Custom Code Menu

 

 

 

 

 

19. Go to the “String Manipulation” folder under ST’s Toolbox section
and drag the ‘Concatenate Strings’ object onto your test’s canvas area
under the Custom Code.

20. Go to the “Concatenate Strings” property sheet and click on the
“test input parameters” button (the Step Icon), and fill out the following info:

 Prefix = The current counter number<br /> Suffix = click on ‘link to data source’ and point to the Previous<br /> Step Custom Code’s outCounter

21. Run the script.

22. The test should pass with the following Output:

Step 'Concatenate Strings6': Output Parameter 'Result', Value: 'The<br /> current counter number=101'<br /> Step 'Concatenate Strings6' ended successfully

ST11 results

 

 

 

I know this example was simplistic, but by using these same steps, you should be well on your way to creating your own Custom Code script masterpiece.

27 comments… add one

  • Ali

    Dear Joe,
    What about using local tables as a custom code’s output?
    When i want compare db result with web service output, i exactly do not know how do that. i could not using local table. I create a DataTable class c# code but how can i set it as a custom code output?
    Thnx.

    • Joe Colantonio

      Ali » Hi Ali, Sorry not sure I understand — did you create some custom code that runs a query and returns data that you want to then compare against the response values of a web service? Let me know — once I understand I’m sure we can figure this out together. Cheers Joe

      • Ali

        sorry Joe for my delay,
        Yes, Exactly.

    • Joe Colantonio

      Ali » Hi Ali – you should take a look at Service Test 11.10 – it has new built in database functionality that should allow you to get the behavior you are looking for.

      • Ali

        Thank you Joe.
        you are great.

  • Ali

    Dear Joe,
    Thanks a lot but an other question: after using new toolbox for DB connection in ST11.10, i need to check any row of result table from DB query by any corresponding row of xml result of my web service.
    what is your solution for handling this senario when the result has unknown rows?
    do you think i need to handle it my self by code or can i handle it by visual wizard?
    if i have to handle it my self where i have to write my code?

  • Motti

    Hi Joe,
    I was testing this application and there was one big question that came to my mind, can I add / create new actions and add them to the ToolBox Tree ?
    For an example … lets say I want to add the “Files_CreateFolder” function to the “File System” to future use.

    Is there any legit ( or not ) way to do this ? Any way will be good, is it possible ?

    • Joe Colantonio

      Motti » Hi Motti – As far as I know this functioanly is not currently possible. What I think might work (I have not tried yet but I think would make a good blog post) would be to create a dll in visual studio and add a reference to that in ST. That would allow you to create reusable code that can be associated and reused with multiple tests. Your idea for creating a reusable action that would show in the Toolbox is
      a great idea — you should put it in as a enhancement request with HP!
      Cheers,
      Joe

  • Anil

    Hi Joe,

    Is there any Comparsion factor in Hpst 11.10 where we can compare old responses with the regerssion responses?
    I used comparsion in Hpst 9.5 , unable to see in new version.

    • Joe Colantonio

      Anil » Hi Anil – sorry for my late response I just noticed your comment. Not sure I understand the functionality that you are trying. You are getting a response back from a service and when you rerun the test you want to compare to make sue the response is the same as the previous runs respone?

  • Anil

    Hi Joe,

    Yes you got correctly. Is that possible from Hpst 11.0.

  • Asheena Andrews

    Hi Joe,

    I am using ST 11.10 and tring to use the custome code snippet you had in Step 18. I am not able to cimpile because it does not know the args.Input and args.output . Also the first line, it does not know what “into” is and it complains about the colon after myCounter. Can you help?

    Thanks,

  • Samer

    Hi Joe,

    Wondering if you could help me out with HP Service Test – Is it possible to correlate viewstate from one HTTP request to another? If Yes how.

    And one more question, i have created local table with one column (e.g. COLUMN1)

    How can i set the value of COLUMN1 programatically from TestUserCode.cs?

    Thanks,
    Samer

    • Joe Colantonio

      Samer » Hi Samer,
      Sorry – I don’t know what viewstate is so I’m not sure how to handle it. I plan on doing a quick post on this but this C# code inserts a value into an Excel column:
      string price;
      //Get custom code’s input variable
      price = this.CodeActivity12.Input.joePrice.ToString()

      System.Data.OleDb.OleDbConnection MyConnection;

      System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();

      string sql = null;
      //Make sure that you have the ACE driver installed on your machine if your are using Excel 2010
      MyConnection = new System.Data.OleDb.OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;Data Source=’D:\\STDEMO.xlsx’;Extended Properties=Excel 12.0;Persist Security Info=False”);

      MyConnection.Open();

      myCommand.Connection = MyConnection;

      sql = “INSERT into [Sheet1$] (COLUMN1) values(‘” + price + “‘)”;

      myCommand.CommandText = sql;

      myCommand.ExecuteNonQuery();

      MyConnection.Close();

  • Jessie

    Hi Joe,

    I’m fresh for using ST11.2. So far I have a question about DataTable. I knew how to get data from DataTable, but I have no idea how to write data into datatable in Custom Code? Can you help me? Thank you.

  • Madan

    Thank You very much for this post. It saved me lot of time and effort.. :)

    • Joe Colantonio

      Madan » Cool – glad it helped!

  • Roopak Raj

    Hi Joe,

    I have one query regarding TestVariables and using them in parameterizing input fields in the request in HPST 11.20.

    Is it possible to use TestVariables for parameterizing the input fields in the request?

    What I am trying to do is I have created a Test Variable and datadrived it with an excel. Now I want to use this TestVariable in my request. When I am selecting “Link to data source” for any field, under Available steps, I can see the TestVariab;le but I am not able to select that. Can you please let me know the reason and the work around.

    Also is there any custom code available using ExecuteEvent function which can be used to set variale values with excel.

    Really appreciate if you can help me out.

    Regards,
    Roopak Raj

    • Joe Colantonio

      Roopak Raj » Hi Roopak what version of ST are you using? I was not able to repro your TestVariable issue. When I add a test variable and click on a ‘Select Link Source’ I’m able to select the ‘test variable’ radio button and point to the user variable that I created. What ST operation are you trying to link to the testvariable? You should be able to create custom code that writes to an Excel file. For example here is a post I wrote that shows how to write to an excel file: How to write response to an excel file You also can programmatically get and set a test variable using something like this:
      string myTestVar = this.StServiceCallActivity4.Context.TestProfile.GetVariable(“myTestVariableName”)
      Hope this helps.

      • SwethaReddy

        Hi Joe,

        I just started using HP ST. I want to use data sheet from ALM like how we use in QTP by associating the data table from ALM. Can you please give me the solution for this.

        Regards,
        Swetha.B

        • SwethaReddy » Hi Swetha.B how do you use the data sheet from ALM in QTP? Do you have it as a resource that QTP downloads and reads from?

  • Swetha

    Hey Joe, I am new to service test. How do I pass values from QTP into service test. I was a QTP script to send the certain values into service test which service test needs to use. Can you please help? thank you.

  • jeetu

    Hi Joe ,
    Can you please post some sample script for how to send requests and receive response from WS using services test.I am new to this tool.I want to learn this tool.

    Thanks

  • Benzx21

    hi guys where can i get step by step tutorial for HP service test
    iam reallt desperate to learn thats testing tool i need to use it for SOA testing

  • Azfar Sajid

    Hi Joe – I find your website very informative. Do you have any insight/example into how to programmatically data drive an API test. i.e., I want to load the external data source at runtime and then associate the input parameters with the data in the external data source.

Leave a Comment

css.php