TestComplete works with many formats to retrieve and write stored data. Some common formats are csv files, Excel spreadsheets, Access databases, SQL Server databases and XML files. Any of these formats can be used to work with your test data, the one you choose will depend on your organization and your project. XML files are useful for many projects, especially where a less rigid data format is needed.
In this post, I'll demonstrate how to use XML files to store and retrieve test data.
TestComplete can store and retrieve test data in XML files. For example, an XML file could define the name of a control where user actions will be simulated and it could store the value to be entered into the control.
Another way you could use XML files in testing is to store values between different test runs.
XML's structure is less rigid than a relational database so it's easier to store and retrieve data where the format isn't known up front.
To work with XML files in TestComplete, you can use TestComplete's
object or use special XML DOM objects.
Storages object contains methods and properties that let you write data to and read data from XML files. However, the disadvantage of this approach is that the object only works with XML files that have a specific format - the Storages object saves the XML files to this format.
To work with any XML file, you can use XML DOM objects. This is more powerful and flexible, but it requires more effort and knowledge of XML DOM.
First, here's a Storages example. The following code uses the
Storages object's methods to store values in an XML file and read data from this file:
The code above defines the sections and options. All work with XML nodes and attributes is done by the
Storages object and its child objects.
Note that an error will occur if the XML file format, whose name you pass to the
Storages.XML method, differs from the format of the file saved by the SaveAs method.
Now let's look at using XML DOM objects...
To work with XML files of any structure, you can use XML DOM objects. These objects provide a COM interface to every element and attribute of the XML document. The following TestComplete script demonstrates how to open an XML file and parse it.
TestXML procedure creates a new XML DOM Document object via COM and loads data from TestFile.xml.
When loading, the XML parser validates the document. We can check the result of the validation using the
parseError object. If the file was loaded successfully, our code obtains the root node and passes it to the
ProcessNode routine for processing.
ProcessNode routine posts the node's name and value to the TestComplete log. If the node does not contain a value, the value is not posted. Then the routine processes the node's attributes and child nodes.
When parsing XML files with DOM objects, you can also use XPath expressions. With XPath expressions you can easily obtain the list of desired nodes and avoid parsing the whole document.
The following code demonstrates how you can use XPath expressions to obtain a collection of control nodes and parse their child nodes.
The sample requires the Data.xml file. Download this file and copy it to the C: folder.
The code above is very similar to the previous sample. The only difference is that we call the selectNodes method and pass it an XPath expression. The selectNodes method returns a collection of the requested objects then we parse the collection.
You can find more information about XPath expressions, XML DOM objects and their methods and properties at MSDN.
Please feel free to post questions in the comments if you want to know more about using XML files in TestComplete.