TestComplete的Stores对象

发表于:2007-9-03 13:49

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:译者:陈能技    来源:陈能技的质量感悟

先保存后检查
        自动化测试并不是所有地方都可以使用,只有计算机能识别、方便检查和比较的地方才能有效发挥自动化测试的功效。
        自动化测试过程中经常需要先把对象的属性或状态暂时存起来,然后再回来找到它,与现在的对象属性或一个新的状态进行比较,从而判断测试是否通过。
        通常像文件、图像、对象属性等都需要保存下来进行后续比较。为此,Test Complete提供了一个叫Stores的编程对象。它包括Files、Regions、Objects集合对象。
        TC会在项目文件目录加一个叫Stores的目录,里面存储的就是这三类文件:
1、图像文件:支持BMP、JPEG、PNG、TIFF、GIF文件
2、以XML格式存储某个对象的属性集合
3、普通文本文件或数据文件
图像集合
        Stores项目Item包含一个可以在测试过程中访问的图像集合。集合名称叫Regions。它存储了对各种图像格式的引用。
        可以直接添加图像到Regions,也可以在录制过程中把截屏保存到Regions,还可以通过脚本编程的方式把图像加到Regions。例如,下面脚本把某个处于激活状态的窗体的一部分图像保存下来。
w := Sys.Desktop.ActiveWindow.Picture(20, 20, 50, 50);
// To save the entire window, use
// w := Sys.Desktop.ActiveWindow;
Regions.AddPicture(w, 'MyFile.bmp');
        把图像保存下来后,可以用下面的方式访问获取Regions里的图像文件:
var
PictureObject : OleVariant;
begin

PictureObject := Regions.GetPicture('MyImage');

end;
        获取到图像后,可以用它来进行比较和搜索操作。例如与一个激活状态的窗体比较看是否一致:
w2 := Sys.Desktop.ActiveWindow;
if not Regions.Compare('MyAppWnd', w2) then
Log.Error('The compared regions are not identical.', w2.Name);
还可以对Regions里面的两个图像进行比较,看是否一致:
if not Regions.Compare('Image1Name', 'Image2Name') then
Log.Error('The compared regions are not identical.');
        还可以通过Picture对象的Compare方法来对两个图像进行比较,如以下脚本:
procedure Test();
var
w1, w2, Pict1, Pict2;
begin
w1 := Sys.Process('MyApp', 1).Window('MyWndClass', 'MyWndCaption', 1);
w1.Activate();
Pict1 := w1.Picture(10, 10, 100, 100, false);
w2 := Sys.Process('MyApp', 2).Window('MyWndClass', 'MyWndCaption', 1);
w2.Activate();
Pict2 := w2.Picture(10, 10, 100, 100, false);
// Compare Pict2 with a fragmnet of application window
if not Pict1.Compare(Pict2) then
begin
Log.Picture(Pict1, 'Region 1');
Log.Picture(Pict2, 'Region 2');
Log.Error('The compared regions are not identical.');
end;
// Compare Pict2 with a stored image
Pict2 := Regions.GetPicture('StoredImage1');
if not Pict1.Compare(Pict2) then
begin
Log.Picture(Pict1, 'Region 1');
Log.Picture(Pict2, 'Region 2');
Log.Error('The compared regions are not identical.');
end;
end;
        通过Regions的FindRegion方法对图像集合中的所有图像进行查找,例如以下脚本在Regions中查找是否存在当前激活窗体的图像:
w := Sys.Desktop.ActiveWindow.Picture(20, 20, 50, 50);
// Region1 is part of the Regions collection
if Regions.FindRegion(w, 'Region1') = nil then
Log.Warning('Not found');
        通过Picture对象的Find方法在某个图像中查找一个图像区域,例如,以下脚本查找一个图像是否在鼠标所指的窗体的某个区域中。
// Obtains one of images
Pict1 := Sys.Desktop.PictureUnderMouse(20, 20, False);
// Obtains another image
w := Sys.Process('MyApp', 1).Window('MyWndClass', 'MyWndCaption', 1);
Pict2 := w.Picture(10, 10, 200, 150);
if ( Pict2.Find(Pict1) = nil ) then
Log.Warning('Not found');
文件集合
        在测试一个程序的过程中,你可能需要确保程序产生的输出数据与上一次测试的相同。TC在Stores中提供Files集合对象用于支持这种做法。
        可以直接往Files添加文件,也可以通过脚本添加,例如:
if not Files.Add('c:\MyApplication\OrdersList.txt', 'OrdersList') then
Log.Message(Files.LastError);
添加完文件后,可以通过Files的NameByFileNam方法来按文件名访问文件:
Name := Files.NameByFileName('C:\Work\OrdersList_old.txt');
通过Files的Compare方法来对比文件:
if not Files.Compare(Name, 'C:\Work\OrdersList.txt') then
Log.Warning('List of orders was changed');
上面的方法是一个个字节地对比文件,任何不一致都会返回False。但是通常,产生的文件包含信息会与保存文件的略有不同。例如,日期、ID等可能会不大一样。解决办法是用正则表达式,下面的脚本通过正则表达式忽略日期时间戳比较文本文件:
function CompareFiles(fileName1, fileName2: string);
const
ForReading = 1;
ForWriting = 2;
ForAppending = 8;
var
fso, regEx: OleVariant;
file1, file2: String;
fileText1, fileText2, newText1, newText2: String;
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
if (newText1 = newText2) then
result := True
else
result := False;
end;

procedure Main;
var
fileName1, fileName2: String;
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;
对象集合
Stores对象包含Objects集合。Objects保存对象(进程、窗体、控件等)的各种属性值,通常用于后续的比较。
可以通过Object Browser来添加对象属性到Objects,也可以通过选择Options | Tools的Store Object Properties 来存储对象属性,也可以在录制脚本过程中保存对象属性。还可以通过脚本编程的方式添加,例如:
w := Sys.Desktop.ActiveWindow();
// Prepares the property list
PropertyNames := 'Top Left Height Width';
// Sets the name of the property collection
CollectionName := 'ActiveWindowProps';
// Saves values of properties specified in PropertyNames.
Objects.Save(w, CollectionName, PropertyNames);
在测试过程中,你可能想知道某个对象的属性是否发生了变化,Objects允许保存对象的各种属性值,然后与后面测试过程中的对象值进行比较。使用Objects的Compare方法来进行比较,例如:
var
w, PropertyNames, CollectionName : OleVariant;
begin
w := Sys.Desktop.ActiveWindow();
// Prepares the property list
PropertyNames := 'Top Left Height Width';
// Sets the name of the property collection
CollectionName := 'ActiveWindowProps';
// Saves values of properties specified in PropertyNames.
Objects.Save(w, CollectionName, PropertyNames);
...
// Compares property values
if not Objects.Compare(w, CollectionName) then
Log.Message('Properties have been changed.');
end;
文件、图像、对象属性是测试过程中需要经常关注的东西,对于他们的保存、访问、查找、比较是自动化测试的强项,应该在自动化测试的策划阶段充分识别出这些对象集合并在测试过程中利用TC提供的Stores对象机制来达到测试自动化的目的。
《2023软件测试行业现状调查报告》独家发布~

精彩评论

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号