Question

Iam testing a web service in UFT using Microsoft XML DOM and HTTP

When i trigger the request XML, i get response in two ways

Way 1 when it success

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <SearchResourceResponse xmlns="http://www.ICLNBI.com/ICLNBI.xsd">
         <MessageElements xmlns="">
            <MessageStatus>SUCCESS</MessageStatus>
            <MessageAddressing>
               <from>ICL</from>
               <to>QPortal</to>
               <messageId>1234</messageId>
               <action>SearchResource</action>
               <timestamp>2013-07-29T17:05:17.860Z</timestamp>
               <ServiceName>SearchResource</ServiceName>
               <ServiceVersion>2.0</ServiceVersion>
            </MessageAddressing>
       </SearchResourceResponse>
   </soap:Body>
</soap:Envelope>

Way 2 when it fails

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body/>
</soap:Envelope>

Iam capturing the <MessageStatus>SUCCESS</MessageStatus> by using the xpath

set ObjXml = Createobject("Microsoft.XMLDOM")

Set ObjNode= ObjXml.SelectSingleNode("/soap:Envelope/soap:Body/*/MessageElements/MessageStatus")

ResultText=ObjNode.text

when it is success it works damn good and when it fails and response appears as shown in way 2, i get a error like Object Required and it wont continue further.

is there any way so that if object not found it should not come out of function and it should return Status as failed and continue further

VB scipt iam using is

Set ObjNode= ObjXml.SelectSingleNode("/soap:Envelope/soap:Body/*/MessageElements/messageStatus")
ResultText=ObjNode.text

If ResultText="SUCCESS" or ResultText="Success" Then 

TcStatus = "Passed"

Else if 

ResultText="FAIL" or ResultText= "FAILURE" Then

TcStatus = "Passed"

But it is failing in Step 1 it self :( can we handle this ?

Was it helpful?

Solution

I doubt you are getting the error on the SelectSingleNode, perhaps that's just a typo in your question?

I suspect you are really getting the failure when trying to access ObjNode.Text. This is because SelectSingleNode will return Nothing if it can't find the node requested. So you just need to check the return value before deciding whether to access .Text.

Set ObjXml = Createobject("Microsoft.XMLDOM")

'Presumably you have a step to load the XML here.

Set ObjNode = ObjXml.SelectSingleNode("/soap:Envelope/soap:Body/*/MessageElements/MessageStatus")
If ObjNode Is Nothing Then
    MsgBox "Oh no! Failure!"
Else
    ResultText = ObjNode.text
End If

Oh, and you can probably shorten your XPath to //MessageStatus if that element never appears elsewhere in the document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top