●文件系统服务:内部服务,利用此服务,可以对文件系统进行操作,比如复制、删除、查看等操作。本框架中利用STAF文件系统服务,将测试用例部署到所有的测试平台,并在执行结束后,将日志文件返还特定机器,以便查阅。例如在 windows 平台下的调用语法为:
STAF local FS COPY DIRECTORY $fromDir TODIRECTORY $toDir TOMACHINE $hostname EXT $ext |
其中 $fromDir 指本地文件夹,$toDir 指远程文件夹,$hostname 指远程测试平台,$ext 指将要进行复制的文件扩展名,此处为 “pl”。
配置文件
脚本的执行命令和环境变量在各个平台是不同的,具有很大的差异性。为避免将这种差异硬编码到自动化脚本中,并使自动化脚本具有很好的透明性,我们采用基于 XML的配置文件覆盖多种平台的差异性。例如本框架中的配置文件如下所示。
代码1 : 配置文件内容示例
<Monitor name="hostname" exeCmd="b:\Scripts\executeCommand.pl" \ log="b:\Scripts\ logfile\ " configure="b:\Scripts\configure.xml"> <host name="turfgrass" os="win" dir="systemDirve\automation"> <testcase value="testcase0001"/> <testcase value="testcase0002"/> <testcase value="testcase0003"/> <testcase value="testcase0004"/> <testcase value="testcase0005"/> </host> <host name="eagles" os="aix" dir="home/automation"> <testcase value="testcase0001"/> <testcase value="testcase0002"/> <testcase value="testcase0003"/> <testcase value="testcase0004"/> <testcase value="testcase0005"/> </host> <Monitor/> |
在配置文件中,指定了要执行的脚本,日志文件的存放位置,配置文件的存放位置,测试机器的操作系统类型,以及脚本要部署的具体路径。脚本在执行时首先读取 XML 文件中的重要信息,例如测试机器的平台,以便采用相对应的执行命令和查询必须的环境量。示例代码如下。
代码2 : 读取XML代码示
use XML::Parser; $file="configure.xml"; |
遍历所有测试平台,查询相关信息,如果平台为 Windows 平台,还将查询 STAF 变量,获取 System Drive 的值,并构造测试平台下的路径,此路径为所要执行测试用例的存放位置。示例代码如下。
代码3 : 遍历所有测试平台代码示例
foreach $host($monitor->children('host')){ my $exeCmd; my $hostname = $host->attribute('name'); my $os = $host->attribute('os'); my $dir = $host->attribute('dir'); chomp($os); chomp($hostname); chomp($dir); if($os =~ /win/i){ my @systemDrive = `staf $hostname VAR GET var STAF/Env/SystemDrive`; chomp($systemDrive[2]); $dir = "$systemDrive[2]\\automation\\"; } } |