How to Save Elements in a Web Service Response to an XML file

Test Automation Published on:
UFT API Cover

How to Save Elements in a Web Service Response to an XML file using UFT API

Today we'll take a look at how to use Services Test's ‘Custom Code' activity to save some elements returned by a web service's response to a XML file.

    1. Start the Sample Application that comes with Service Test (Start\Programs\Hp Service Test 11.10\Sample Application.
    2. Write down the WSDL's endpoint: http://localhost:24240/HPFlights_SOAP?WSDL

       

  1. Create a new test in ST and Click on the Import Service
  2. In the Import Service from URL or UDDI's Address field enter: http://localhost:24240/HPFlights_SOAP?WSDL
  3. Under ST's ‘Toolbox' under the ‘Web Services' folder you should have a GetFlights operation. Drag that to the main canvas area.

  4. In the GetFlights Property Sheet enter the following:

    DepartureCity= Denver
    ArrivalCity = London

  5. Under the Toolbox Miscellaneous folder drag a custom code onto the canvas
  6. Under the Custom code's property sheet click on ‘Add Input Property'

  7. Name the input property flightXML type ‘String'
  8. Link the flightXML value to the GetFlights output GetFlightResponse

  9. Click on the Custom Codes property sheet ‘Events'(thunderbolt) icon
  10. Select ‘Create a default handler' under the ‘ExecuteEvent'
  11. Save your ST test up to this point
  12. In your custom code CodeActivity_OnExecuteEvent enter the following:

15. Run the test. The test should run without error and you should now have an XML file that looks like the following:

Next time we'll take a look at how to read this XML and data drive the CreateFlights operation.

7 responses to “How to Save Elements in a Web Service Response to an XML file”

  1. Hi Joe, i am getting at error at the line “for(int i=0;i)” . I tried to change the loop to for (int i=0;i<100;i++). Now i am getting custom code functionality error saying that "Object reference not set to an instance of an object.". Can you please help me out on this?

    • Change the for loop to for(int i=0;i<fCount;i++).
      fCount is the number of elements found in the response.

  2. I am getting an error “Data at the root level is invalid. Line 1, position 1.”.

    At root level I have

    namespace Script
    {
    using System;
    using System.Xml;
    using System.Xml.Schema;
    using HP.ST.Ext.BasicActivities;
    using HP.ST.Fwk.RunTimeFWK;
    using HP.ST.Fwk.RunTimeFWK.ActivityFWK;
    using HP.ST.Fwk.RunTimeFWK.Utilities;
    using HP.ST.Fwk.RunTimeFWK.CompositeActivities;
    using System.Windows.Forms;

    Any idea about the possible reason behind this?Thanks in Advance!

      • When I try web service response in XML and try toparameterize the test, I can just see the values of just last element in XML.Looks like values are “overwritten” because of doc.Save(); Is there anyway,I can see values of all the elements in the data table/sheet??

  3. Hi Joe,

    I am also getting the same error, “Object reference not set to an instance of an object”

    I am using the version 11.20.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

UFT API Cover