自定義腳本運行TestComplete項目

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 "<html><title>Project Scheduler</title><body bgcolor=#ffffff scroll=no>" '寫一段html到ie窗口中。'
.write "<h2 align=center>Project Scheduler</h2>"
.write "<input id=info type=text color=red size=400 style='color:red;background:white;border:1px;border-bottom-style:none;border-top-style:none;border-left-style:none;border-right-style:none;'><br>"
.write "<p align=left>"
.write "<font color=blue size=2>Select the Project path that you need to run.</font>"
.write "<p>Project Path:<input id=proPath type=file size=19><br>"
.write "<p align=left>"
.write "<font color=blue size=2>Input the execution time(24H) that you expect to run.</font>"
.write "<p>Expected Time:<input id=execTime type=text size=30 onfocus='this.select();'value='19:30'>"
.write "<p align=center><br><br>"
.write "<input id=confirm type=button value='Confirm'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
.write "<input id=cancel type=button value='Cancel'>"
.write "</body></html>"
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

 界面效果:wKioL1W4TbCx5pFfAADsFJF060Q089.jpg 

 

方案三:JavaScript代碼實現上述功能(目的很明確,就是要實現Confirm按鈕的onclick()事件):

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>Project Scheduler</title>
 </head>
 <body>
  <h1>Project Scheduler</h1>
  <hr>
  <input id=info type=text color=red size=400 style='color:red;background:none;border:1px;border-bottom-style:none;border-top-style:none;border-left-style:none;border-right-style:none;'><br>
  <p align=left>
  <font color=blue size=4>Select the Project path that you need to run.</font>
  <p>Project Path:<input id=proPath type=file size=19><br>
  <p align=left>
  <font color=blue size=4>Input the execution time(24H) that you expect to run.</font>
  <p>Expected Time:<input id=execTime type=text size=30 onfocus='this.select();'value='such as: 19:30'>
  <br><br>
  <input id="confirm" type="button" value="Confirm">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input id="cancel" type="button" value="Cancel">
  
  <script language="javascript">
   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.";
    }
   } 
  </script>
 </body>
</html>

 效果基本上跟vbs實現的界面效果……

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章