TestComplete作为一个软件自动化测试的IDE,留有少量接口供不同的人在不同的场景下运行项目,那么如何通过脚本去简化和更加智能的启动它并执行它的项目呢?

下面是TestComplete 提供的Command line:

TestComplete.exe [file_name [/run 
[
(/project:project_name|
 (/project:project_name /projectitem:item_name|
 (/project:project_name /test:test_name|
 (/project:project_name /unit:unit_name /routine:routine_name)] 
[/exit]]  [/SilentMode [/ErrorLog:File_name]]  [/ExportLog:File_name]
[Timeout:Time_in_seconds]  [/ForceConversion]  [/ns]

 

下面就根据上述的命令,写一个类似windows的计划任务功能的可指定执行时间和项目路径去执行TC项目的脚本……

方案一:思路是用dos命令实现,set /p命令定义一个可接收输入值的变量,然后将改变量值与当前时间对比,约定时间的格式,if判断相等就执行,否则就等待~

具体batch脚本如下:

 

@echo off
@echo /**************** begin *************/
::author Alan_Y
set /p executeTime=Please input the execution time(format:hhmm ,such as 1930):
set /p projectModel=Please input project model(1:TestItems , 2:Main):
set TCexePath=E:\software\TestComplete10\TestComplete.exe
if %projectModel% EQU 1 (
 set projectPath="E:\Learning\AutoTest\AutoTest.mds"
) else (
 set projectPath="E:\Learning\AutoTest\AutoTestSuit.pjs"
)
@echo.
@echo TestComplete.exe path: %TCexePath%
@echo.
@echo Project path: %projectPath%
@echo.
set /a Timer=1
set sign=:
:LOOP
rem get the current time
set currentTime=%time:~0,2%%time:~3,2%
if %Timer% EQU 1 (
 @echo the current Time: %currentTime:~0,2%%sign%%currentTime:~2,2%
 @echo the execute Time: %executeTime:~0,2%%sign%%executeTime:~2,2%
 @echo.
) else (
 rem wait for 60s
 ping -n 60 127.0.0.1>nul 2>nul 
 @echo the current Time: %time:~0,2%%sign%%time:~3,2%
 @echo the execute Time: %executeTime:~0,2%%sign%%executeTime:~2,2%
 @echo.
)
if %currentTime%==%executeTime% (
 rem kill TC process
 taskkill /F /IM "TestComplete*"
 rem run TC and execute project
 if %projectModel% EQU 1 (
  start %TCexePath% /r /e %projectPath%
 ) else ( 
  start %TCexePath% %projectPath% /r /p:AutoTest /t:"Script|fMain|main"
 )else (
 set /a Timer=%Timer%+1
 goto LOOP
)
@echo /***************** end **************/

 运行的效果如下:

/**************** begin *************/
Please input the execution time(format:hhmm ,such as 1930):1830

Please input the project model(1:TestItems , 2:Main):2

TestComplete.exe path: E:\software\TestComplete10\TestComplete.exe

Project path: "E:\Learning\AutoTest\AutoTest.mds"

the current Time: 15:35
the execute Time: 18:30

the current Time: 15:36

the execute Time: 18:30

the current Time: 15:37

the execute Time: 18:30

 

 ……直到执行

 

补充~~~~~~

方案二:通过VBScript 脚本实现运行项目(VBScript作为脚本语言,没有提供GUI控制界面,但可以通过内嵌html代码来实现界面操作):

dim projectPath,executeTime,executeHour,executeMinute,currentHour,currentMinute,interval
set ie=wscript.createobject("internetexplorer.application","event_") '创建ie对象'
ie.menubar=0 '取消菜单栏'
ie.addressbar=0 '取消地址栏'
ie.toolbar=0 '取消工具栏'
ie.statusbar=0 '取消状态栏'
ie.width=400 '宽400'
ie.height=400 '高400'
ie.resizable=0 '不允许用户改变窗口大小'
ie.navigate "about:blank" '打开空白页面'
ie.left=fix((ie.document.parentwindow.screen.availwidth-ie.width)/2) '水平居中'
ie.top=fix((ie.document.parentwindow.screen.availheight-ie.height)/2) '垂直居中'
ie.visible=1 '窗口可见'
with ie.document '以下调用document.write方法,'
.write "Project Scheduler" '写一段html到ie窗口中。'
.write "Project Scheduler"
.write "
" .write "" .write "Select the Project path that you need to run." .write "

Project Path:
" .write "" .write "Input the execution time(24H) that you expect to run." .write "

Expected Time:" .write "

" .write "        " .write "" .write "" end with 'author Alan_Y dim wmi '显式定义一个全局变量 set wnd=ie.document.parentwindow '设置wnd为窗口对象 set id=ie.document.all '设置id为document中全部对象的集合 id.confirm.onclick=getref("confirm") '设置点击"确定"按钮时的处理函数 id.cancel.onclick=getref("cancel") '设置点击"取消"按钮时的处理函数 do while true '由于ie对象支持事件,所以相应的 wscript.sleep 200 '脚本以无限循环来等待各种事件 loop sub event_onquit 'ie退出事件处理过程' wscript.quit '当ie退出时,脚本也退出' end sub sub cancel '"取消"事件处理过程'  ie.quit '调用ie的quit方法,关闭IE窗口,随后会触发event_onquit,于是脚本也退出了' end sub sub confirm '"确定"事件处理过程,这是关键'  with id  if .proPath.value="" then    .info.value="The Project Path can not be null."   exit sub  else   projectPath = .proPath.value   set fs =WScript.CreateObject("Scripting.FileSystemObject")   if fs.FileExists(projectPath) = true then     if .execTime.value <> "" and InStr(.execTime.value,":")>0 then     executeTime = .execTime.value     executeHour = CInt(split(executeTime,":")(0))     executeMinute = CInt(split(executeTime,":")(1))     currentHour = Hour(now)     currentMinute = Minute(now)          if((executeHour*60 + executeMinute) < (currentHour*60 + currentMinute)) then 'another day      interval = CInt((executeHour + 24 - currentHour)*60 + executeMinute - currentMinute) 'ms     else      interval = CInt((executeHour - currentHour)*60 + executeMinute - currentMinute) 'ms     end if     dim WshShell     set WshShell = WScript.CreateObject("WScript.Shell")     if interval>0 then       .confirm.disabled="disabled"      Do while interval>0       .info.value= "Need to wait " & interval & " minutes."       WshShell.sendkeys "{f5}" 'refresh       WScript.Sleep(60000)'sleep 60s       interval = interval -1      Loop      if interval=0 then        dim strCommand       strCommand = "start " & Chr(34) & "TestComplete.exe" & Chr(34) & " /r /e " & Chr(34) & projectPath & Chr(34)       .info.value = strCommand       WshShell.Run strCommand , 0 ,true      end if     end if         else     .info.value="The format of Execution Time is not correct, please input again."     exit sub    end if   else    .info.value="The Project Path is invalid, check it please."    exit sub   end if  end if    end with end sub

 界面效果:自定义脚本运行TestComplete项目_第1张图片 

 

方案三:JavaScript代码实现上述功能(目的很明确,就是要实现Confirm按钮的onclick()事件):


 
  
  Project Scheduler
 
 
  

Project Scheduler

  
  
     Select the Project path that you need to run.   

Project Path:
     Input the execution time(24H) that you expect to run.   

Expected Time:   

                       var btnCancel = document.getElementById("cancel");    btnCancel.onclick = function(){     window.opener=null;     window.close();    }     //author Alan_Y    var btnConfirm = document.getElementById("confirm");    var txtInfo = document.getElementById('info');    btnConfirm.onclick = function(){     var path = document.getElementById('proPath');     if(path.value !=""){      path.select();      var projectPath = document.selection.createRange().text;      var fs = new ActiveXObject("Scripting.FileSystemObject");      if(fs.FileExists(projectPath)){       var txtTime = document.getElementById('execTime');       if (txtTime.value != ""&& (txtTime.value).indexOf(":")!=-1) {        var executeTime = txtTime.value;        var executeHour = executeTime.split(":")[0];        var executeMinute = executeTime.split(":")[1];        var date = new Date();        var currentHour = date.getHours();        var currentMinute = date.getMinutes();        var interval = 0;        if((executeHour*60 + executeMinute) < (currentHour*60 + currentMinute)){ //another day;         interval = (executeHour + 24 - currentHour)*60 + executeMinute - currentMinute; //m        }else{         interval = (executeHour - currentHour)*60 + executeMinute - currentMinute ;//m        }        var WshShell = new ActiveXObject("WScript.Shell");        if(interval>0){         btnConfirm.disabled = true;         while(interval > 0){          txtInfo.value= "Need to wait " + interval + " minutes.";          WshShell.sendkeys("{f5}"); //refresh          WScript.Sleep(60000); //sleep 60s          interval--;         }        }        if(interval==0){         var strCommand = "start "+ "TestComplete.exe" + " /r /e \"" + projectPath + "\"";         txtInfo.value = strCommand;         alert(strCommand);         WshShell.Run(strCommand,true);               }               }else{        txtInfo.value = "The format of Execution Time is not correct, please input again.";       }             }else{       txtInfo.value = "The Project Path is invalid, check it please.";      }           }else{      txtInfo.value = "The Project Path can not be null.";     }    }      

 效果基本上跟vbs实现的界面效果……