test.compareXMLFiles

Boolean test.compareXMLFiles(expectedFilePath, actualFilePath)

Boolean test.compareXMLFiles(expectedFilePath, actualFilePath, options)

This function compares the expected XML file specified by expectedFilePath with the actual XML file specified by actualFilePath. If they are equal, a test result of type PASS is added to the test log and this function returns a true value. Otherwise a test result of type FAIL is added to the test log and a false value is returned. If the files are different the result message details will show in which line and for what reason the comparison failed.

The optional parameter options can be specified as a map or dictionary. So far the following options can be set:

  • ignoreNodeOrder : If this is set to true the function will ignore the order of elements on the same level, i.e., with the same parent element. The default is false.
  • ignoreAttributeOrder : If this is set to true the function will ignore the order of attributes inside an element. The default is true.
  • ignoreComments : If this is set to true the function will ignore all comments. If it is set to false all comments are compared. The default is true.
  • whitespaceInTextHandling : This option can be used to control how whitespaces inside of text elements are processed. This can be set to either preserve, normalize or trim. If this is set to preserve all whitespaces are kept, if this is set to normalize all whitespaces will be normalized and if this is set to trim whitespaces at the start and end of the text elements are removed. The default is normalize.
  • whitespaceInValuesHandling : This option can be used to control how whitespaces inside of attribute values are processed. This can be set to either preserve, normalize or trim. If this is set to preserve all whitespaces are kept, if this is set to normalize all whitespaces will be normalized and if this is set to trim whitespaces at the start and end of the attribute values are removed. The default is preserve.
  • ignoreCDATA : If this is set to true only the text inside the CDATA tags will be compared. The tags itself will be ignored. If this is set to false the tags will also be compared. The default is true.
  • matchSpecialTags : If this is set to true special tags like the declaration, processing instructions and doctype tags will also be compared by value. The default is false.
  • filteredElements : This option accepts a list/array of strings representing tag names. Tags that have a name contained inside the filtered elements list will be ignored when comparing the two documents.
  • filteredAttributes : This option accepts a list/array of strings representing attribute names. Attributes that have a name contained inside the filtered attributes list will be ignored when comparing the two documents.

You can also compare XML files with the Boolean test.compareTextFiles(expectedFilePath, actualFilePath) function but this function has the advantage that it does not consider the file formatting. Instead it looks directly at the structure of the files and compares nodes and attributes.

//example 1: no options
test.compareXMLFiles("expected.xml", "actual.xml");

//example 2: you only need to specify the options you need
test.compareXMLFiles("expected.xml", "actual.xml", {'ignoreNodeOrder': false});

//example 3: with filtered attributes and elements
filteredAttributes = ["version", "xmlns"];
filteredElements = ["uri"];
options = {'ignoreNodeOrder': false,
           'ignoreAttributeOrder': true,
           'filteredAttributes': filteredAttributes,
           'filteredElements': filteredElements,
           'whitespaceInTextHandling':'trim'};
test.compareXMLFiles("expected.xml", "actual.xml", options);
#example 1: no options
test::compareXMLFiles("expected.xml", "actual.xml");

#example 2: you only need to specify the options you need
test::compareXMLFiles("expected.xml", "actual.xml", {"ignoreNodeOrder" => 0});

#example 3: with filtered attributes and elements
my $filteredAttributes = ["version", "xmlns"];
my $filteredElements = ["uri"];
my $options = {"ignoreNodeOrder" => 0,
               "ignoreAttributeOrder" => 1,
               "filteredAttributes" => $filteredAttributes,
               "filteredElements" => $filteredElements,
               "whitespaceInTextHandling" => "trim"};
test::compareXMLFiles("expected.xml", "actual.xml", $options);
#example 1: no options
test.compareXMLFiles("expected.xml", "actual.xml")

#example 2: you only need to specify the options you need
test.compareXMLFiles("expected.xml", "actual.xml", {'ignoreNodeOrder': False})

#example 3: with filtered attributes and elements
filteredAttributes = ["version", "xmlns"]
filteredElements = ["uri"]
options = {'ignoreNodeOrder': False,
           'ignoreAttributeOrder': True,
           'filteredAttributes': filteredAttributes,
           'filteredElements': filteredElements,
           'whitespaceInTextHandling':'trim'}
test.compareXMLFiles("expected.xml", "actual.xml", options)
#example 1: no options
Test.compareXMLFiles("expected.xml", "actual.xml")

#example 2: you only need to specify the options you need
Test.compareXMLFiles("expected.xml", "actual.xml", {'ignoreNodeOrder' => false})

#example 3: with filtered attributes and elements
filteredAttributes = ["version", "xmlns"]
filteredElements = ["uri"]
options = {'ignoreNodeOrder' => false,
           'ignoreAttributeOrder' => true,
           'filteredAttributes' => filteredAttributes,
           'filteredElements' => filteredElements,
           'whitespaceInTextHandling' => 'trim'}
Test.compareXMLFiles("expected.xml", "actual.xml", options)
#example 1: no options
test compareXMLFiles "expected.xml" "actual.xml"

#example 2: you only need to specify the options you need
test compareXMLFiles "expected.xml" "actual.xml" [dict create ignoreNodeOrder 0]

#example 3: with filtered attributes and elements
set filteredAttributes [list version xmlns]
set filteredElements [list uri]
set options [dict create ignoreNodeOrder 0 \
                         ignoreAttributeOrder 1 \
                         filteredAttributes $filteredAttributes \
                         filteredElements $filteredElements \
                         whitespaceInTextHandling trim]
test compareXMLFiles "expected.xml" "actual.xml" $options

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.
The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation.
Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

Search Results