When testing applications, you may need to verify different files. This is often done to make sure that the application under test generates correct output data (for example, a list of orders).
Quite often the generated files contain information that is modified from one copy to another, whereas the major information remains the same. For example, the current date or time, document’s ID, owner name and so on. The byte-by-byte comparison performed by the file checkpoints or by the Compare Files operation and the Files.Compare
method is ineffective, since the compared files are not identical.
As a solution, you can exclude those variable parts from verification. The general idea is to find the altering fragment and replace it with the unified fragment.
You can perform this task by writing special script code that will use regular expression for verification.
Note: | JavaScript, JScript, Python, VBScript, C#Script and C++Script have internal support for regular expressions. To learn how to use regular expressions in these scripting languages, read the Introduction to Regular Expressions (Scripting) article in the MSDN library. For DelphiScript, regular expressions are implemented via the RegExpr object, which is provided by the HISUtils plugin (installed and enabled by default). This object can also be used in JavaScript, JScript, Python, VBScript, C#Script and C++Script code. |
The code below demonstrates how to use regular expressions to compare text files ignoring the date/time stamps.
JavaScript
function CompareFiles(fileName1, fileName2)
{
var fso, file1, file2, regEx;
var fileText1, fileText2, newText1, newText2;
const ForReading = 1;
// Creates the FileSystemObject object
fso = getActiveXObject("Scripting.FileSystemObject");
// Reads the first text file
file1 = fso.OpenTextFile(fileName1, ForReading);
fileText1 = file1.ReadAll();
file1.Close();
// Reads the second text file
file2 = fso.OpenTextFile(fileName2, ForReading);
fileText2 = file2.ReadAll();
file2.Close();
// Specifies the regular expression pattern for the date/time mask
// MM/DD/YYYY HH:MM:SSLL (for example: 4/25/2006 10:51:35AM)
regEx = /\d{1,2}.\d{1,2}.\d{2,4}\s\d{1,2}:\d{2}:\d{2}\w{2}/gim;
// Replaces the text matching the specified date/time format with <ignore>
newText1 = fileText1.replace(regEx, "<ignore>");
newText2 = fileText2.replace(regEx, "<ignore>");
// Compares the text
return equal(newText1, newText2);
}
function Main()
{
var fileName1 = "d:\\text1.txt";
var fileName2 = "d:\\text2.txt";
if (CompareFiles(fileName1, fileName2))
Log.Message("The files are equal");
else
Log.Error("The files are different");
}
JScript
function CompareFiles(fileName1, fileName2)
{
var fso, file1, file2, regEx;
var fileText1, fileText2, newText1, newText2;
var ForReading = 1;
// Creates the FileSystemObject object
fso = new ActiveXObject("Scripting.FileSystemObject");
// Reads the first text file
file1 = fso.OpenTextFile(fileName1, ForReading);
fileText1 = file1.ReadAll();
file1.Close();
// Reads the second text file
file2 = fso.OpenTextFile(fileName2, ForReading);
fileText2 = file2.ReadAll();
file2.Close();
// Specifies the regular expression pattern for the date/time mask
// MM/DD/YYYY HH:MM:SSLL (for example: 4/25/2006 10:51:35AM)
regEx = /\d{1,2}.\d{1,2}.\d{2,4}\s\d{1,2}:\d{2}:\d{2}\w{2}/gim;
// Replaces the text matching the specified date/time format with <ignore>
newText1 = fileText1.replace(regEx, "<ignore>");
newText2 = fileText2.replace(regEx, "<ignore>");
// Compares the text
return (newText1 == newText2);
}
function Main()
{
var fileName1 = "d:\\text1.txt";
var fileName2 = "d:\\text2.txt";
if (CompareFiles(fileName1, fileName2))
Log.Message("The files are equal");
else
Log.Error("The files are different");
}
Python
def CompareFiles(fileName1, fileName2):
ForReading = 1
# Creates the FileSystemObject object
fso = Sys.OleObject["Scripting.FileSystemObject"]
# Reads the first text file
file1 = fso.OpenTextFile(fileName1, ForReading)
fileText1 = file1.ReadAll()
file1.Close()
# Reads the second text file
file2 = fso.OpenTextFile(fileName2, ForReading)
fileText2 = file2.ReadAll()
file2.Close()
# Specifies the regular expression pattern for the date/time mask
# MM/DD/YYYY HH:MM:SSLL (for example: 4/25/2006 10:51:35AM)
regEx = "/\d{1,2}.\d{1,2}.\d{2,4}\s\d{1,2}:\d{2}:\d{2}\w{2}/gim"
# Replaces the text matching the specified date/time format with <ignore>
newText1 = fileText1.replace(regEx, "<ignore>")
newText2 = fileText2.replace(regEx, "<ignore>")
# Compares the text
return (newText1 == newText2)
def Main():
fileName1 = "d:\\text1.txt"
fileName2 = "d:\\text2.txt"
if (CompareFiles(fileName1, fileName2)):
Log.Message("The files are equal")
else:
Log.Error("The files are different")
VBScript
Function CompareFiles(fileName1, fileName2)
Dim fso, file1, file2, regEx
Dim fileText1, fileText2, newText1, newText2
Const ForReading = 1
' Creates the FileSystemObject object
Set fso = CreateObject("Scripting.FileSystemObject")
' Reads the first text file
Set file1 = fso.OpenTextFile(fileName1, ForReading)
fileText1 = file1.ReadAll
file1.Close
' Reads the second text file
Set file2 = fso.OpenTextFile(fileName2, ForReading)
fileText2 = file2.ReadAll
file2.Close
' Creates the regular expression object
Set regEx = New RegExp
' Specifies the pattern for the date/time mask
' MM/DD/YYYY HH:MM:SSLL (for example: 4/25/2006 10:51:35AM)
regEx.Pattern = "\d{1,2}.\d{1,2}.\d{2,4}\s\d{1,2}:\d{2}:\d{2}\w{2}"
regEx.IgnoreCase = True
regEx.Global = True
' Replaces the text matching the specified date/time format with <ignore>
newText1 = regEx.Replace(fileText1, "<ignore>")
newText2 = regEx.Replace(fileText2, "<ignore>")
' Compares the text
CompareFiles = (newText1 = newText2)
End Function
Sub Main
Dim fileName1, fileName2
fileName1 = "d:\text1.txt"
fileName2 = "d:\text2.txt"
If CompareFiles(fileName1, fileName2) Then
Log.Message("The files are equal")
Else
Log.Error("The files are different")
End If
End Sub
DelphiScript
function CompareFiles(fileName1, fileName2);
const
ForReading = 1;
var
fso, regEx;
file1, file2;
fileText1, fileText2, newText1, newText2;
begin
// Creates the FileSystemObject object
fso := Sys.OleObject('Scripting.FileSystemObject');
// Reads the first text file
file1 := fso.OpenTextFile(fileName1, ForReading);
fileText1 := file1.ReadAll;
file1.Close;
// Reads the second text file
file2 := fso.OpenTextFile(fileName2, ForReading);
fileText2 := file2.ReadAll;
file2.Close;
// Creates the regular expression object
regEx := HISUtils.RegExpr;
// Specifies the pattern for the date/time mask
// MM/DD/YYYY HH:MM:SSLL (for example: 4/25/2006 10:51:35AM)
regEx.Expression := '(?i)\d{1,2}.\d{1,2}.\d{2,4}\s\d{1,2}:\d{2}:\d{2}\w{2}';
// Replaces the text matching the specified date/time format with <ignore>
newText1 := regEx.Replace(fileText1, '<ignore>');
newText2 := regEx.Replace(fileText2, '<ignore>');
// Compares the text
Result := (newText1 = newText2);
end;
procedure Main;
var
fileName1, fileName2;
begin
fileName1 := 'd:\text1.txt';
fileName2 := 'd:\text2.txt';
if CompareFiles(fileName1, fileName2) then
Log.Message('The files are equal')
else
Log.Error('The files are different');
end;
C++Script, C#Script
function CompareFiles(fileName1, fileName2)
{
var fso, file1, file2, regEx;
var fileText1, fileText2, newText1, newText2;
var ForReading = 1;
// Creates the FileSystemObject object
fso = new ActiveXObject("Scripting.FileSystemObject");
// Reads the first text file
file1 = fso["OpenTextFile"](fileName1, ForReading);
fileText1 = file1["ReadAll"]();
file1["Close"]();
// Reads the second text file
file2 = fso["OpenTextFile"](fileName2, ForReading);
fileText2 = file2["ReadAll"]();
file2["Close"]();
// Specifies the regular expression pattern for the date/time mask
// MM/DD/YYYY HH:MM:SSLL (for example: 4/25/2006 10:51:35AM)
regEx = /\d{1,2}.\d{1,2}.\d{2,4}\s\d{1,2}:\d{2}:\d{2}\w{2}/gim;
// Replaces the text matching the specified date/time format with <ignore>
newText1 = fileText1["replace"](regEx, "<ignore>");
newText2 = fileText2["replace"](regEx, "<ignore>");
// Compares the text
return (newText1 == newText2);
}
function Main()
{
var fileName1 = "d:\\text1.txt";
var fileName2 = "d:\\text2.txt";
if (CompareFiles(fileName1, fileName2))
Log["Message"]("The files are equal");
else
Log["Error"]("The files are different");
}
See Also
File Checkpoints
About File Checkpoints
Regular Expressions Syntax
Working With Files From Scripts