Compare Method

Applies to TestComplete 15.69, last modified on November 13, 2024

Description

Use the Compare method to compare data stored in the XMLCheckpoint project element, to which the XMLCheckpoint object corresponds, with the data of an XML document.

Using the Compare method you can also update the XML data stored in the XMLCheckpoint element. TestComplete does this if the Update XML data option is enabled. See Modifying Elements of the XML Collection.

Declaration

XMLCheckpointObj.Compare(xmlSource, ReportDifference, MessageType)

XMLCheckpointObj An expression, variable or parameter that specifies a reference to an XMLCheckpoint object
xmlSource [in]    Required    Variant    
ReportDifference [in]    Optional    Boolean Default value: True   
MessageType [in]    Optional    Variant Default value: lmWarning   
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

xmlSource

This parameter specifies the source of the compared XML data. It can be one of the following:

  • Fully qualified file name of the desired XML document.

  • URL of the desired XML document.

  • An XMLCheckpoint object.

  • An IXMLDOMDocument object (This is an object from Microsoft XML DOM. See the DOMDocument article in the MSDN library for more information)).

  • A WebService object.

ReportDifference

Specifies whether the method will build a detailed report on the found differences and post this report to the test log.

MessageType

The type of message to be posted to the test log in case the comparison fails and ReportDifference is True. Possible values:

Constant Value Description
lmNone 0 Do not post any message.
lmMessage 1 Post an informative message.
lmWarning 2 Post a warning message.
lmError 3 Post an error message.

Result Value

True, if the comparison is successful and False otherwise.

Remarks

The Compare method returns True or False indicating the comparison result.

If the verification passes, the method posts a success message to the test log. If the verification fails, the method, by default, logs a detailed report on all differences found. For more information on what the report contains, see XML Comparison Results Log Page. To disable logging the difference report, pass False to the ReportDifference parameter when calling the method.

The method compares XML data according to the comparison settings specified in the XMLCheckpoint project element. Before calling the method, you can modify these options as well as the stored data. For more information on changing the settings and data, see Creating XML Checkpoints.

You can also use the Check method for comparison. This method has fewer parameters than the Compare method does, so it is easier to use.

Example

The following example demonstrates how you can compare the stored XML document with the external XML document specified by its full name and path.

JavaScript, JScript

function XMLCheckpointCompare()
{

  var XMLCheckpoint, XMLFile;
  // Specifies the external XML file.
  XMLFile = "D:\\Work Folder\\SampleXMLFile.xml";
  // Obtains the XMLCheckpoint object from script.
   XMLCheckpoint = XML.MyXMLCheckpoint;

  // Compares the stored XML document with the external XML document.
  // Posts a detailed report if the comparison fails.
  if (! XMLCheckpoint.Compare(XMLFile, true , lmNone))
    Log.Error("" + XMLFile + " differs from the stored XML document.");

}

Python

def XMLCheckpointCompare():
  # Specifies the external XML file. 
  XMLFile = "D:\\Work Folder\\SampleXMLFile.xml"
  # Obtains the XMLCheckpoint object from script. 
  XMLCheckpoint = XML.MyXMLCheckpoint
  # Compares the stored XML document with the external XML document. 
  # Posts a detailed report if the comparison fails. 
  if not XMLCheckpoint.Compare(XMLFile, True , lmNone):
    Log.Error("" + XMLFile + " differs from the stored XML document.")

VBScript

Sub XMLCheckpointCompare
  Dim XMLCheckpoint, XMLFile

  ' Specifies the external XML file.
  XMLFile = "D:\Work Folder\SampleXMLFile.xml"
  ' Obtains the XMLCheckpoint object from script.
  Set XMLCheckpoint = XML.MyXMLCheckpoint

  ' Compares the stored XML document with the external XML document.
  ' Posts a detailed report if the comparison fails.
  If Not XMLCheckpoint.Compare(XMLFile, True , lmNone) Then
    Log.Error("" & XMLFile & " differs from the stored XML document.")
  End If

End Sub

DelphiScript

procedure XMLCheckpointCompare();
var XMLCheckpoint, XMLFile;
begin

  // Specifies the external XML file.
  XMLFile := 'D:\Work Folder\SampleXMLFile.xml';
  // Obtains the XMLCheckpoint object from script.
  XMLCheckpoint := XML.MyXMLCheckpoint;

  // Compares the stored XML document with the external XML document.
  // Posts a detailed report if the comparison fails.
  if not XMLCheckpoint.Compare(XMLFile, True , lmNone) then
    Log.Error('' + XMLFile + ' differs from the stored XML document.');

end;

C++Script, C#Script

function XMLCheckpointCompare()
{

  var XMLCheckpoint, XMLFile;
  // Specifies the external XML file.
  XMLFile = "D:\\Work Folder\\SampleXMLFile.xml";
  // Obtains the XMLCheckpoint object from script.
   XMLCheckpoint = XML["MyXMLCheckpoint"];

  // Compares the stored XML document with the external XML document.
  // Posts a detailed report if the comparison fails.
  if (! XMLCheckpoint["Compare"](XMLFile, true , lmNone))
    Log["Error"]("" + XMLFile + " differs from the stored XML document.");

}

The following example demonstrates how you can load XML data to the IXMLDOMDocument object and compare this data with the stored XML data.

JavaScript

function XMLCheckpointCompare()
{

  var XMLDoc, XMLCheckpointObject;
  // Creates an object implementing the IXMLDOMDocument interface
  // If you have MSXML 4:
  XMLDoc = getActiveXObject("Msxml2.DOMDocument.4.0");
  // If you have MSXML 6:
  XMLDoc = getActiveXObject("Msxml2.DOMDocument.6.0");

  XMLDoc.async = false;
  // Loads data from the XML document to the IXMLDOMDocument object.
  XMLDoc.load("D:\\Work Folder\\SampleXMLFile.xml")

  // Obtains the XMLCheckpoint object from script.
  XMLCheckpointObject = XML.MyXMLCheckpoint;

  // Compares MyXMLCheckpoint with the IXMLDOMDocument object.
  // Posts an error message and a detailed report to the log if the comparison fails.
  XMLCheckpointObject.Compare(XMLDoc, true, lmError);

}

JScript

function XMLCheckpointCompare()
{

  var XMLDoc, XMLCheckpointObject;
  // Creates an object implementing the IXMLDOMDocument interface
  // If you have MSXML 4:
  XMLDoc = Sys.OleObject("Msxml2.DOMDocument.4.0");
  // If you have MSXML 6:
  XMLDoc = Sys.OleObject("Msxml2.DOMDocument.6.0");

  XMLDoc.async = false;
  // Loads data from the XML document to the IXMLDOMDocument object.
  XMLDoc.load("D:\\Work Folder\\SampleXMLFile.xml")

  // Obtains the XMLCheckpoint object from script.
  XMLCheckpointObject = XML.MyXMLCheckpoint;

  // Compares MyXMLCheckpoint with the IXMLDOMDocument object.
  // Posts an error message and a detailed report to the log if the comparison fails.
  XMLCheckpointObject.Compare(XMLDoc, true, lmError);

}

Python

def XMLCheckpointCompare2():
  # Creates an object implementing the IXMLDOMDocument interface. 
  # If you have MSXML 4: 
  XMLDoc = Sys.OleObject["Msxml2.DOMDocument.4.0"]
  # If you have MSXML 6:  
  XMLDoc = Sys.OleObject["Msxml2.DOMDocument.6.0"]
  XMLDoc.async = False
  # Loads data from the XML document to the IXMLDOMDocument object. 
  XMLDoc.load("D:\\Work Folder\\SampleXMLFile.xml")
  # Obtains the XMLCheckpoint object from script. 
  XMLCheckpointObject = XML.MyXMLCheckpoint
  # Compares MyXMLCheckpoint with the IXMLDOMDocument object. 
  # Posts an error message and a detailed report to the log if the comparison fails. 
  XMLCheckpointObject.Compare(XMLDoc, True, lmError)

VBScript

Sub XMLCheckpointCompare
  Dim XMLDoc, XMLCheckpointObject

  ' Creates an object implementing the IXMLDOMDocument interface.

  ' If you have MSXML 4:
  Set XMLDoc = Sys.OleObject("Msxml2.DOMDocument.4.0")
  ' If you have MSXML 6:
  Set XMLDoc = Sys.OleObject("Msxml2.DOMDocument.6.0")

  XMLDoc.async = False
  ' Loads data from the XML document to the IXMLDOMDocument object.
  Call XMLDoc.load("D:\Work Folder\SampleXMLFile.xml")

  ' Obtains the XMLCheckpoint object from script.
  Set XMLCheckpointObject = XML.MyXMLCheckpoint

  ' Compares MyXMLCheckpoint with the IXMLDOMDocument object.
  ' Posts an error message and a detailed report to the log if the comparison fails.
  Call XMLCheckpointObject.Compare(XMLDoc, true, lmError)

End Sub

DelphiScript

procedure XMLCheckpointCompare();
var XMLDoc, XMLCheckpointObject;
begin

  // Creates an object implementing the IXMLDOMDocument interface.
  // If you have MSXML 4:
  XMLDoc := Sys.OleObject('Msxml2.DOMDocument.4.0');
  // If you have MSXML 6:
  XMLDoc := Sys.OleObject('Msxml2.DOMDocument.6.0');

  XMLDoc.async := false;
  // Loads data from the XML document to the IXMLDOMDocument object.
  XMLDoc.load('D:\Work Folder\SampleXMLFile.xml');

  // Obtains the XMLCheckpoint object from script.
  XMLCheckpointObject := XML.MyXMLCheckpoint;

  // Compares MyXMLCheckpoint with the IXMLDOMDocument object.
  // Posts an error message and a detailed report to the log if the comparison fails.
  XMLCheckpointObject.Compare(XMLDoc, true, lmError);

end;

C++Script, C#Script

function XMLCheckpointCompare()
{

  var XMLDoc, XMLCheckpointObject;
  // Creates an object implementing the IXMLDOMDocument interface.
  // If you have MSXML 4:
  XMLDoc = Sys["OleObject"]("Msxml2.DOMDocument.4.0");
  // If you have MSXML 6:
  XMLDoc = Sys["OleObject"]("Msxml2.DOMDocument.6.0");

  XMLDoc["async"] = false;
  // Loads data from the XML document to the IXMLDOMDocument object.
  XMLDoc["load"]("D:\\Work Folder\\SampleXMLFile.xml")

  // Obtains the XMLCheckpoint object from script.
  XMLCheckpointObject = XML["MyXMLCheckpoint"];

  // Compares MyXMLCheckpoint with the IXMLDOMDocument object.
  // Posts an error message and a detailed report to the log if the comparison fails.
  XMLCheckpointObject["Compare"](XMLDoc, true, lmError);

}

See Also

About XML Checkpoints
Creating XML Checkpoints
Options Property
Document Property
Modifying Elements of the XML Collection

Highlight search results