Tuesday 25 March 2008

XML automation - Part V - Validate XML response

This is the last part...but I will try to summarize the posts in an article later on...maybe I will add more features, for example how to handle attributes and multiple elements.

All the example code and files are available in compressed format here for download.

Please note that I have changed the test case parameter ConvertTempResult to #ConvertTempResult in order to mark up output parameters.



Here is the function for response validation:



Function validateXMLResponse (objDictionary)

Set xmlDoc = CreateObject("Msxml2.DOMDocument")
'loadXML
xmlDoc.loadXML(objDictionary.Item("XMLResponse"))
'Check XML syntax
If (xmlDoc.parseError.errorCode <> 0) Then
Set myErr = xmlDoc.parseError
objDictionary.Item("XMLResponseResult") = "Failed"
objDictionary.Item("XMLResponseResultDetails") = "ERROR: " & myErr.reason
Set validateXMLResponse = objDictionary
Exit Function
End If
Set root = xmlDoc.documentElement
'Put all test parameters in array
arrAllKeys = objDictionary.Keys
arrAllValues = objDictionary.Items
'Update values
For i = 0 to Ubound(arrAllKeys)
'Only process #-type parameters
If Left(arrAllKeys(i),1) = "#" Then
strParameterElementName = arrAllKeys(i)
strParameterElementName = Right(strParameterElementName,Len(strParameterElementName)-1)
strParameterElementValueExpected = arrAllValues(i)
'Get element and value
Set node = root.getElementsByTagName(strParameterElementName)
If node.Length > 0 Then
For n = 0 to node.Length-1
'Return actual value in [element]_Out
objDictionary.Item(strParameterElementName & "_Out") = node.Item(n).Text
strParameterElementValueActual = node.Item(n).Text
'Set result
If strParameterElementValueExpected = strParameterElementValueActual AND _
objDictionary.Item("XMLResponseResult") <> "Failed" Then
objDictionary.Item("XMLResponseResult") = "Passed"
Else
objDictionary.Item("XMLResponseResult") = "Failed"
objDictionary.Item("XMLResponseResultDetails") = objDictionary.Item("XMLResponseResultDetails") & _
strParameterElementName & " = " & strParameterElementValueActual & " , expected value " & _
strParameterElementValueExpected & " ;" 'use ; as separator
End If
Next
End If
Set node = Nothing
End If
Next
'Return
Set validateXMLResponse = objDictionary
'Clean up
Set root = Nothing
Set xmlDoc = Nothing
End Function


So finally we have these steps all together:



' Example of XML/Web Service automation in VBScript using WinHttp, XML DOM and ADO
' by Stefan Thelenius march 2008

'Set test case id and data source file
intTestCaseID = 1
strMyDataFile = "c:\MyTestData.xls"

'Get test case parameters
Set objMyTestParameters = getTestParametersIntoDictionary (strMyDataFile, _
"TestCase", "TestCaseID", intTestCaseID)

'Get global parameters
Set objMyGlobalParameters = getTestParametersIntoDictionary (strMyDataFile, _
"Global", "GlobalID", objMyTestParameters.Item("GlobalID"))

'Merge parameters
Set objMyTestParameters = mergeDictionaries (objMyGlobalParameters, objMyTestParameters)

'Create request from template
objMyTestParameters.Item("XMLRequest") = getXMLTemplate (objMyTestParameters.Item("XMLTemplateFile"), _
objMyTestParameters.Item("XMLTemplate"))

'Insert test parameters
objMyTestParameters.Item("XMLRequest") = setTestParametersInXMLRequest (objMyTestParameters)

'Send request
objMyTestParameters.Item("XMLResponse") = sendXMLRequest (objMyTestParameters)

'Validate response
Set objMyTestParameters = validateXMLResponse (objMyTestParameters)

'Display output
Msgbox objMyTestParameters.Item("XMLResponse")
Msgbox objMyTestParameters.Item("XMLResponseResult")
Msgbox objMyTestParameters.Item("XMLResponseResultDetails")
Msgbox objMyTestParameters.Item("ConvertTempResult_Out")

20 comments:

Anonymous said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Smartphone, I hope you enjoy. The address is http://smartphone-brasil.blogspot.com. A hug.

Stefan Thelenius said...

Thanks! I checked your blog but I am not that good in reading Portuguese. I like the concept of smartphones but I'm more into smaller cell-phones like my Nokia 6500 Classic

Sanjay Kumar said...

This code really rocks. Good Work...

Unknown said...

Hello Stefan your code is really good :) It realle helped me testing web services, I just have a question regarding the size of the response I am having problem when the response is too big, do you suggest any solution to this. Lets say the response is 35879 characters. I saw this text in the http info "This property can only be invoked after the Send method has been called.

When using this property in synchronous mode, the limit to the number of characters it returns is approximately 2,169,895."

Any idea?

Stefan Thelenius said...

Hi,

I have not encountered a response out of range...yet

What kind of problem do you get?

Unknown said...

The XML gets truncated.
I am receiving this error directly from WinHttpReq.ResponseText. There is not error just XML gets truncated so later will fail on validation since format is not correct.

I have not found the reason, the only thing I know is that the response is way bigger than other request that I have done and have worked.

Stefan Thelenius said...

OK,

So you get a part of your response...have many characters do you get in the response before truncation? Is it below 2,169,895 ?

Unknown said...

The number os characters is 2055, below than 2,169,895.

Maniz said...

Hi

I am trying to send a SOAP request using vbscript. I am getting this error

No credentials were found and the authenticate action was configured to fail in this situation.

Can you please help me on this

Stefan Thelenius said...

Hi,

Have you provided credetials (WinHttpReq.SetCredentials)?

/Stefan

Maniz said...

Hi
Thanks for your reply.

I have sent a mail with my request and response.

Please help me on this.

Maniz said...
This comment has been removed by the author.
Maniz said...

Hi,
Can you please let me know how to use UsernameToken credentials ?

Thanks
Mani

www.teruel-3d.com said...

Wow, there is really much worthwhile info here!

Unknown said...

Hi Stefan,
I am not sure whether this post is still alive. One question about your XML aumtomation for SOAP based webservices.Can the code be used to test rest based webservices? If yes could you please post sample code abuot that as well.

Stefan Thelenius said...

I have no REST examples but I guess it should work.

Unknown said...

I need to automate following scenario for UFT Proof of concept.

1) Load of XML file
2) Call Web service using XML file/data
3) Receive that Web service as service request from API
4) Receive web service call from API
5) Extract XML payload
6) Load expected XML output
7) Compare actual andexpected XML files
8) Record result Pass / FAIL and no further

In UFT, is it possible to create and load XML file then call web service using XML file/data?

Is it possible to create a Mock webservice in UFT for POC testing?

I think point 2 is the tricky one here and I don’t know how to call webservice in UFT without using WSDL.

Thanks in advance for your help.

Looking forward for your quick reply

Regards
KAPIL

Stefan Thelenius said...

@invisible0786

Hi,

In theory each of your steps should work in UFT. Use a public web service and ellaborate with my example code above, should work in UFT I guess. I don't use QTP anymore so I can't support you in this matter, I recommend AdvancedQTP for further assistance.

Good luck!

/Stefan

Unknown said...

Thanks, is the public web service inbuild within UFT? how to call Web service using XML XSD??

Stefan Thelenius said...

Please check out the complete series for how to call etc, search for XML Automation in the blog.

The public Web Service in my example is on the Internet and I am not sure if it is still there.

Good luck!