Manipulate input/output properties for Web Service or HTTP/SOAP/CoAP Request steps

Relevant for: API testing only

Use code to access and set the properties of your HTTP/SOAP/CoAP Request or Web Service steps.

Note: Use event handler code for REST services and WADL steps in the same way as you do for standard API testing activities.

For details, see Access and set the value of step input, output, or checkpoint properties.

Access and set property values for input properties

  1. In the canvas, select a Web service, a SOAP Request, or a Send CoAP Request step.

  2. Load an XML containing the body of your request.

    In the Properties pane, open the XML Body tab (SOAP Request) or the CoAP tab (CoAP request).

  3. Click the Load XML button and navigate to your request file.

  4. In the Properties pane, open the Events tab .

  5. In the Events tab, create event handlers.

Back to top

Add checkpoint property values

Use code to add checkpoint values in a Web service or SOAP request step. This is useful if the response on which the checkpoints is based is dynamically created.

Note: You cannot change the value of already existing checkpoints set in the Web service call.

  1. Create an event handler for the CodeCheckpointEvent, as described in Set the value of a checkpoint.

  2. Following the line containing your code for enabling the checkpoint (args.Checkpoint.RunUICheckpoints = true), enter the value of your checkpoint, using the following syntax:

    args.Checkpoint.Assert.Equals(<actual value>,<expected value>);

    In the <actual value> and <expected value> parameters, you access the checkpoint properties through the step's output envelope. To access the output parameters, use the same syntax as described in Access and set property values for input properties above. However, you must change the InputEnvelope to OutputEnvelope to access the correct properties.

Back to top

Specify a SOAP Fault and SOAP Fault values

Using code, set your Web service call or SOAP Request to expect a SOAP fault and specify the expected fault properties.

  1. In the canvas, select a Web service or SOAP Request step.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler for the AfterGenerateRequest event. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, specify the expected fault, using the following syntax:

    this.<activity>.FaultExpected = true;
  5. In the Events tab, create an event handler for the CodeCheckpointEvent event.

  6. In the TODO section of the TestUserCode.cs file, in the CodeCheckpointEvent section, specify the expected fault information, using the following syntax:

    string xpath = "<path to fault property>";
    string actualValue = this.StServiceCallActivity<activity #>.OutputEnvelope.SelectSingleNode(xpath).InnerText;
    string expectedValue = "soap:Server";
    args.Checkpoint.Assert.Equals(actualValue,expectedValue);

    Note: For the specified string names in the syntax above, you can use your own names.

Back to top

Assign a specific request file to a test step

  1. In the canvas, select a Web service or SOAP Request step.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler for the AfterGenerateRequest event. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, specify the expected fault, using the following syntax:

    this.<activity>.InputEnvelope.LoadXml(@"<path to response file>");

    Note: You can also load the response from a previous step instead of from a file. In this case, you need to access the OutputEnvelope from a previous step in place of the @"<path to response file>" string. For details on accessing an output property from a step, see Set the value of a checkpoint.

Back to top

Assign a specific request file to a Web service step in the OnSendRequest event

  1. In the canvas, select a Web service or SOAP Request step.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler for the OnSendRequest event. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, specify the expected fault, using the following syntax:

    System.Xml.XmlDocument envelope = new XmlDocument();
    envelope.LoadXml(System.Text.Encoding.UTF8.GetString(args.Message));
    
    string xpath = "<fully qualified XPath to Property>";
    envelope.SelectSingleNode(xpath).InnerText = "<value to enter>";
    	
    args.Message = System.Text.Encoding.UTF8.GetBytes(envelope.OuterXml);

Back to top

Set asynchronous Web service call properties

  1. In the canvas, select a Web service or SOAP Request step.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler for AfterGenerateRequest event. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, specify the asynchronous call, using the following syntax:

    <activity name>.IsAsync = true;
  5. Below the code for the IsAsync, specify the port on which to listen for the Web service or SOAP Request response, using the following syntax:

    this.<activity>.ListenOnPort = <port number>;

Back to top

Add an input attachment to a Web service call

Use code to send an attachment with a Web service call. This is very useful when the attachment is generated dynamically, and you cannot add this attachment using the API testing interface during test design.

Note: If you are loading an attachment from outside your test, skip to step 5.

  1. Add a custom code step to the canvas.

  2. In the Properties pane, open the Input/Output Properties tab .

  3. In the Input/Output Properties, click Add and select Add Input Parameter. In the Add Input Parameter dialog box, give the parameter a meaningful name.

  4. Link the parameter to the desired attachment. For details, see Assign data to API test/component steps.

  5. In the canvas, select a Web service or SOAP Request step.

  6. In the Properties pane, open the Events tab .

  7. In the Events tab, create the AfterGenerateRequest event. The TestUserCode.cs file opens.

  8. In the TODO section of the TestUserCode.cs file, specify the attachment to add, using the following syntax:

    string attachmentsInfo = 
    	@"<InputAttachments>
    		<Type> <attachment type> </Type>
    		<Attachments>
    			<Origin> <path to file> </Origin>
    			<OriginType> File </OriginType>
    			<ContentType> <type of content> </ContentType>
    			<ContentID>Auto</ContentID>
    		</Attachments>
    	</InputAttachments>";
    
    this.StServiceCallActivity<activity #>.InputAttachments = new XmlDocument();
    this.StServiceCallActivity<activity #>.InputAttachments.LoadXml(attachmentsInfo);

    You must define the attachment properties before the code that adds the attachment, as seen in the @<InputAttachments> of the code. These properties are then displayed for the test step in the Attachments tab in the Properties pane. For details on the necessary property values for attachments, see Attachments Tab (Properties Pane - API Testing).

    Note: You can define multiple attachments between the opening <InputAttachments> tag and the closing </InputAttachments> tag, using the syntax displayed above (between the <Attachments> and </Attachments> tags.

  9. Optional - to override an attachment already defined in the Attachments tab in the Properties pane, you can use the following syntax:

    string <string name> = "<fully qualified XPath to attachment defined in the Attachments tab>";
    this.StServiceCallActivity<activity #>.InputAttachments.SelectSingleNode(<string name>).InnerText = @"<path to file>";
    

    Note: This does not update the attachment's properties, but simply overwrites the attachment file.

Back to top

Access an attachment from a Web service call response

By default, UFT One saves attachments from a Web server response in the run results folder contained within the test's folder. However, you can also access these attachments using an event handler:

  1. In the canvas, select the Web service or SOAP Request step for which you want to save the Web service call.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler for the AfterExecuteStepEvent event. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, access the attachment information, using the following syntax:

    string <string name> = System.IO.Path.Combine(this.StServiceCallActivity<activity #>.Context.ReportDirectory,"Attachments");
    string[] <name> = System.IO.Directory.GetFiles(<string name>);

    This event handler returns an array which contains the full paths to the attachments returned with the Web service response.

Back to top

Add a HTTP Header for Web Service Calls

When you are editing your Web service call or SOAP Request steps, you can use an event to add an HTTP header. This is useful if the source for this information is created dynamically during a test run.

  1. In the canvas, select a Web service or SOAP Request step.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler for the BeforeApplyProtocolSettings event. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, add the header element, using the following syntax:

    this.StServiceCallActivity4.HttpRequestHeaders.Add("<header key>", "< key value>");
    

Note: You can also set the HTTP headers values by expanding the RequestHeader node in the Properties pane's Input/Checkpoints tab. You then link to a data source from the Name and Value rows. For details, see Select Link Source Dialog Box (API Testing).

If you modify the headers using code in an event handler, it will override the values in the Properties pane during the test run.

Back to top

HTTP Headers for REST Service Calls

To dynamically add an HTTP header to a REST service or to a REST service’s inner HTTP Request step (when working with API tests created in UFT One 11.51 or earlier or Service Test 11.51 or earlier):

  1. In the canvas, select a REST method step.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler for the BeforeExecuteStepEvent event. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, allocate the desired length for the array:

    (args.Activity as HTTPActivity).RequestHeaders = 

    new HP.ST.Shared.Utilities.Pair<string, string>[<# of headers>];
    (args.Activity as HTTPActivity).RequestHeaders = new HP.ST.Shared.Utilities.Pair<string, string>[2];
  5. Below the allocation code, provide the each array element with the <HeaderName> and <HeaderValue> for each array element:

     (args.Activity as HTTPActivity).RequestHeaders[0] = new HP.ST.Shared.Utilities.Pair<string, string>("<header name>", "<header value>")
    

    Note: You will need to provide a separate line for each of the headers, as specified in your allocation statement.

Back to top

Modify a SOAP Request security header in runtime

  1. In the canvas, select a SOAP Request step (or Web Service call step using SOAP).

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler for the AfterProcessRequestSecurity event. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, perform a string replace to add the new user name credential:
    string s = args.Message.InnerXml.Replace("<wsse:Username>User</wsse:Username>","<wsse:Username>New User</wsse:UserName>");
    args.Message.InnerXml = s;
  5. Below the string replace code, modify the header XML:

    XmlDocument xmlDocument = args.Message;
        XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(xmlDocument.NameTable);
        xmlnsManager.AddNamespace("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
        xmlnsManager.AddNamespace("wsu", http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        xmlDocument.SelectSingleNode("//wsse:Username",xmlnsManager).InnerText = "New Name";
        args.Message = xmlDocument;

Back to top

Access and modify the SOAP raw request before sending

  1. In the canvas, select a Web service or SOAP Request step.

  2. In the Properties pane, click the Events tab. Create an event handler for the OnConstructRawRequest events.

  3. When the TestUserCode.cs file opens, modify the property value in the TODO section.

    Use the following syntax:

    This.StServiceCallActivity<activity #>.RawRequest = @"<Expected request value>";

Back to top

See also: