Monday, 25 February 2008

XML automation - part II (XML templates)

When I automate XML requests I normally use templates for my requests.

Usage example:

<?xml version="1.0" ?>
<Templates>
<myTemplate_01>
<createNewSomething>
<anElement>somevalue</anElement>
<anotherElement>someothervalue</anotherElement>
</createNewSomething>
</myTemplate_01>
<myTemplate_02>
<createNewSomething>
<anElement>somevalue</anElement>
<anotherElement>someothervalue</anotherElement>
<optionalElement>yetanothervalue</optionalElement>
</createNewSomething>
</myTemplate_02>
</Templates>
So to use a template in an automated test case you need to pass a parameter which template to use

strXML = "<?xml version=""1.0"" ?><Templates><myTemplate_01>" &_
"<createNewSomething><anElement>somevalue</anElement>" &_
"<anotherElement>someothervalue</anotherElement>" &_
"</createNewSomething></myTemplate_01><myTemplate_02>" &_
"<createNewSomething><anElement>somevalue</anElement>" &_
"<anotherElement>someothervalue</anotherElement>" &_
"<optionalElement>yetanothervalue</optionalElement>" &_
"</createNewSomething></myTemplate_02></Templates>"
strMyTemplate = "myTemplate_01"
Msgbox getXMLTemplate (strXML, strMyTemplate)
Function getXMLTemplate (strXML, strMyTemplate)
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
'loadXML
xmlDoc.loadXML(strXML)
'Check XML syntax
If (xmlDoc.parseError.errorCode <> 0) Then
Set myErr = xmlDoc.parseError
Msgbox "ERROR: " & myErr.reason
End If
Set root = xmlDoc.documentElement
'Select template and return
Set node = root.selectNodes("//" & strMyTemplate)
If node.Length > 0 Then
getXMLTemplate = node.Item(0).childNodes.Item(0).xml
Else
getXMLTemplate = "No template found!"
End If
'Clean up
Set xmlDoc = Nothing
Set root = Nothing
Set node = Nothing
End Function
Up next: Part III - Insert test parameters into XML
 

No comments: