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 "<h3 align=center>Project Scheduler</h3>" .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'> " .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
界面效果:
方案三:JavaScript代码实现上述功能(目的很明确,就是要实现Confirm按钮的onclick()事件):
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Project Scheduler</title> </head> <body> <h2>Project Scheduler</h2> <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"> <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实现的界面效果……
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。