• Home  / 
  • ST
  •  /  Service Test 11 – How to save elements in a web service response to an XML file

Service Test 11 – How to save elements in a web service response to an XML file

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


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

  6. In the GetFlights Property Sheet enter the following:

    DepartureCity= Denver
    ArrivalCity = London

  7. Under the Toolbox Miscellaneous folder drag a custom code onto the canvas
  8. Under the Custom code’s property sheet click on ‘Add Input Property’

  9. Name the input property flightXML type ‘String’
  10. Link the flightXML value to the GetFlights output GetFlightResponse

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

XmlDocument xml1 = new XmlDocument();
//Get the number of flight returned
XmlNodeList fNList = xml1.GetElementsByTagName("FlightNumber");
 int fCount = fNList.Count;
// Create the xml document to hold flight info
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec); // Create the root element
XmlElement root = doc.CreateElement("GetFlights");
// Create a Flight retunred count element
XmlElement FlightCount = doc.CreateElement("FlightCount");
FlightCount.InnerText = fCount.ToString();
for(int i=0;i)
XmlElement Flights = doc.CreateElement("Flights");
//Create a flight element that will hold the flight num
XmlElement flight = doc.CreateElement("flight");
/Assign the retunred flight num to element
flight.InnerText = fNList[i].InnerText;
string xmlOutput = doc.OuterXml;
//Write XML to file

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.

Succeeding with Automation Awesomeness. I’ll show you how!

Test automation, like all development efforts, is difficult. Most projects don't succeed. You can do better! Sign up and receive exclusive content like my free 10 Proven Actionable Steps for Automation Awesomeness from some of the biggest testing leaders in the industry. Let me help YOU succeed with Test Automation! Sign up now:

Click here to add a comment

Optin Architect