Forum Discussion
radov
Staff
14 years agoHi Corey,
If you need to verify XML data in your keyword test, you can use the XML Checkpoint operation that compares the contents of an XML document (XML data which you obtain from the tested application during the test run) against the baseline copy stored as an XMLCheckpoint project element that belongs to the Stores | XML collection.
If an XML document you need to verify contains variable date values only in attributes, I would like to suggest you the following two approaches:
1. Open the XMLCheckpoint project element containing the baseline copy of your XML document in the XMLCheckpoint Element editor, select the Ignore attributes check box and save the changes. In this case, XML Checkpoint will ignore all attribute values when comparing XML data from this XMLCheckpoint project element against the needed XML document obtained from the application at run time. It is the easiest way to ignore date values if they are stored only as attribute values. However, if you still need to compare some other XML attributes that do not contain variable date values in your document, this solution is not the best one in your case.
2. Remove attributes that contain variable date values from the baseline copy of your XML document and from the XML document that is obtained from the tested application and needs to be verified. If your document really contains thousands of such attributes, you can programmatically remove them from the baseline copy and from the actual XML document at run time and compare these modified documents. To modify the documents, you should programmatically iterate through their nodes and remove the needed attributes.
For implementing the second approach, you can use the following scenario:
a. Create an XML Checkpoint element in your project and save the baseline copy of your XML document in it (let's suppose the element is named XmlCheckpoint1).
b. Create another XML Checkpoint element to which you will save the actual XML document that you need to verify (let's suppose the element is named XmlCheckpoint2).
c. Add the following script routine to your project (note that it is written in JScript, so you can use it in a JScript TestComplete project):
This routine gets an XML document passed through its parameter as an object that implements the IXMLDOMDocument interface, removes all attributes whose names are specified in the attributeNames array and returns the resulting XML document. Note that you should edit a bit the routine's body and use the attributeNames array to specify all the attribute names which you need to be removed from the document's nodes (suppose you know exactly what attributes must be removed).
d. Add a Run Script Routine operation to your keyword test and specify the above-mentioned RemoveXMLAttributes routine in the operation's parameters. Also, specify the XML.XmlCheckpoint1.Document value as the routine's parameter. When you execute this operation, the XmlCheckpoint1 element will contain the updated version of the baseline XML document (without the specified attributes).
e. Let's suppose that you have stored the actual XML document in a separate file (for example, C:\mydata.xml). Before comparing it with the baseline copy, load its contents to the XmlCheckpoint2 helper element. For this purpose, you can use the Run Code Snippet operation in your test and specify the following code in its parameters:
If you need to verify XML data in your keyword test, you can use the XML Checkpoint operation that compares the contents of an XML document (XML data which you obtain from the tested application during the test run) against the baseline copy stored as an XMLCheckpoint project element that belongs to the Stores | XML collection.
If an XML document you need to verify contains variable date values only in attributes, I would like to suggest you the following two approaches:
1. Open the XMLCheckpoint project element containing the baseline copy of your XML document in the XMLCheckpoint Element editor, select the Ignore attributes check box and save the changes. In this case, XML Checkpoint will ignore all attribute values when comparing XML data from this XMLCheckpoint project element against the needed XML document obtained from the application at run time. It is the easiest way to ignore date values if they are stored only as attribute values. However, if you still need to compare some other XML attributes that do not contain variable date values in your document, this solution is not the best one in your case.
2. Remove attributes that contain variable date values from the baseline copy of your XML document and from the XML document that is obtained from the tested application and needs to be verified. If your document really contains thousands of such attributes, you can programmatically remove them from the baseline copy and from the actual XML document at run time and compare these modified documents. To modify the documents, you should programmatically iterate through their nodes and remove the needed attributes.
For implementing the second approach, you can use the following scenario:
a. Create an XML Checkpoint element in your project and save the baseline copy of your XML document in it (let's suppose the element is named XmlCheckpoint1).
b. Create another XML Checkpoint element to which you will save the actual XML document that you need to verify (let's suppose the element is named XmlCheckpoint2).
c. Add the following script routine to your project (note that it is written in JScript, so you can use it in a JScript TestComplete project):
// XMLDoc - an object that implements the IXMLDOMDocument interface
// and contains the initial XML document
function RemoveXMLAttributes(XMLDoc)
{
var selectedNodes, node;
// Create an array with the names of the attributes to be removed
var attributeNames = Array("date_attr_1", "date_attr_2");
for(var i = 0; i < attributeNames.length; i++)
{
// Get the collection of nodes with the attributeNames attribute specified
selectedNodes = XMLDoc.selectNodes("//*[@" + attributeNames + "]");
// Iterate through the selected nodes
node = selectedNodes.nextNode();
while(node != null)
{
node.removeAttribute(attributeNames);
node = selectedNodes.nextNode();
}
}
return XMLDoc;
}
This routine gets an XML document passed through its parameter as an object that implements the IXMLDOMDocument interface, removes all attributes whose names are specified in the attributeNames array and returns the resulting XML document. Note that you should edit a bit the routine's body and use the attributeNames array to specify all the attribute names which you need to be removed from the document's nodes (suppose you know exactly what attributes must be removed).
d. Add a Run Script Routine operation to your keyword test and specify the above-mentioned RemoveXMLAttributes routine in the operation's parameters. Also, specify the XML.XmlCheckpoint1.Document value as the routine's parameter. When you execute this operation, the XmlCheckpoint1 element will contain the updated version of the baseline XML document (without the specified attributes).
e. Let's suppose that you have stored the actual XML document in a separate file (for example, C:\mydata.xml). Before comparing it with the baseline copy, load its contents to the XmlCheckpoint2 helper element. For this purpose, you can use the Run Code Snippet operation in your test and specify the following code in its parameters:
f. Add one more Run Script Routine operation to your keyword test, specify the RemoveXMLAttributes routine in its parameters and pass the following code to the routine:
As a result of this operation, the XmlCheckpoint2 element will contain the actual version of your XML document after removing the specified attributes.
g. Add one more Run Code Snippet operation to your test and specify the following code in its parameters:
This code performs the comparison of the XmlCheckpoint1 element (the baseline document) against the XmlCheckpoint2 element (the actual XML data). The specified variable attributes (containing date values in your case) have been already removed from both the documents, so, you can compare them.
I have attached a sample TestComplete project suite (see the XMLVerification.zip archive) that demonstrates how you can implement this approach. Note that the project suite was created with TestComplete ver. 8. The archive also contains a sample XML document named mydata.xml. Place it to the root of the C:\ disk to execute properly the test project. The RemoveXMLAttributes routine in the project removes all the version and name attributes from the sample document (note that its baseline copy stored in the XmlCheckpoint1 project element contains the attributes' values that differ from the values stored in the mydata.xml document).
I hope this approach helps in your case.