Data-Driven Testing with TestComplete – Part 2
The DDT object is implemented as an extension (Using COM mechanism) in TestComplete. In order to use it we need to make sure it is installed in our TestComplete. This extension is installed by default. You can verify it in [ File -> Install Extension ].
The DDT object has separate drivers for data sources like Excel, CSV and ADO tables. The main advantage of having these drivers is we can use same code ie., access properties and methods of DDT object independent of data source. Based on the data source use the driver from DDT object.
For example,
function TestDDTDriver() { //Connect to ADO data source through windows authentication var objDB = DDT.ADODriver(“*ADO table connection string*”); ReadData(objDB); DDT.CloseDriver(objDB.Name); //Connect to CSV data source objDB = DDT.CSVDriver(“CSV File path”); ReadData(objDB); DDT.CloseDriver(objDB.Name); //Connect to excel data source objDB = DDT.ExcelDriver(“Excel File path”,”Sheet-Name”); ReadData(objDB); DDT.CloseDriver(objDB.Name); } function ReadData(objDB) { while(! objDB.EOF()) { var strRecord = "" for(var idx=0; idx <strong><</strong> objDB.ColumnCount; idx++) { strRecord += objDB.Value(idx) + " --- "; } Log.Message(strRecord); objDB.Next(); } }</pre> <pre>
I am using a general “ReadData” function to read various kind of data source just by connecting through different drivers.
Thus the complexity of manipulating different data sources is abstracted by DDT object and gives a clean and easy way to access them.
The key point behind this idea was, we can imagine the data source as rows and columns as similar to DB tables and neglect how Excel or CSV actually stores the data.
So how exactly this object gets the schema of each data source…
The assumptions are,
- The object assumes that each row in your CSV/EXCEL file contains data for one test run. Files that use multiple rows for one run are not supported.
- The column names are specified by the first line of the file.
- To retrieve data from the file, the object uses Microsoft Jet Engine.
- While connecting to Excel files we have option to switch to ACE drivers. (Supported for Microsoft Office 2007 and later).
Also we can override the default comma (“,”) as delimiter in CSV files. The format of the CSV file is determined by using a schema information file, named Schema.ini, and located in the same folder as the CSV file. Schema.ini can keep information about several files and for each file it provides data for the general format of the file, field names and field types, used character set, delimiter character, and a number of other data characteristics. For example:
; The contents of the Schema.ini [MyData.csv] Format=Delimited(#) CharacterSet=ANSI
Now the driver will read the CSV file “MyData.csv” with delimiter “#”.
Another best practice I would like to mention is, always close the opened connections with data sources. (Even though Jet Engine has a limitation of 64 connections per process).