AutoIt 随记1 ------ 为打开workbench[java jar启动的程序]做个自动打开工具
背景:
在使用http://chemistry.apache.org/java/developing/tools/dev-tools-workbench.html workbench 来登陆cmis的后台的时候每次步骤都比较繁琐
1、找到workbench的目录,双击workbench.bat
2、输入cmis的url→选择binding→输入username→选择authentication方式→点击load
做个小工具来实现这六个步骤的自动化。
思路1:让workbench启动完毕就有默认值,workbench的启动方式是java -jar方式启动的,如果想从代码层的话,需要修改源代码。放弃
思路2:用自动化测试的工具,实现自动执行这些步骤
一、工具选择
AutoIt
二、根据autoIt提供的example编写autoit代码
0、交互对话框获得用户是什么环境,什么用户 ,我用是是让用户按逗号隔开,然后再做分割处理
Local $iLoop = 1, $value = "" While $iLoop = 1 $value = InputBox("环境用户信息", "请按环境,用户名,密码输入,逗号分隔,正式环境用prod","vagrant,admin@fp.com,222222") If @error = 1 Then ;MsgBox($MB_SYSTEMMODAL, "Error", "You pressed 'Cancel' - try again!") Exit 1 Else ; They clicked OK, but did they type the right thing? If StringInStr($value, ",") = 0 Then ; This is not case-sensitive, therefore AutoIt and AuToIT are acceptable values as well MsgBox(80, "输入错误", "请以逗号分隔输入") Else $iLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too EndIf EndIf WEnd ; deal with the array ,to get the env ,email and password $array = StringSplit($value, ",") $env = $array[1] $email = $array[2] $pwd = $array[3]
1、实现打开workbench,打开cmd.exe 然后在cmd上执行workbench.bat,等[CLASS:SunAwtDialog]启动后,就关掉cmd窗口[CLASS:ConsoleWindowClass]
;Get the work dir then Run cmd.exe ,excute the workbench.bat,then close the cmd console $Var = StringSplit (@scriptdir,"\") ;msgbox(0,"",@scriptdir) ;msgbox(0,"",UBound($Var)) Run("cmd.exe") WinWaitActive("[CLASS:ConsoleWindowClass]") Send($Var[1] & "{ENTER}") ;Send("cd D:\chemistry-opencmis-workbench-0.13.0-full{ENTER}") Send(@scriptdir & "\workbench.bat{ENTER}") WinWaitActive("[CLASS:SunAwtDialog]") WinClose("[CLASS:ConsoleWindowClass]")
2、输入cmis的url
$cmisUrl = "http://cmis.vagrant.foxitcloud.com:8180/cmis/atom" $platformUrl = "http://platform.vagrant.foxitcloud.com:8003/rest/users/accessToken?email=" & $email & "&password=" & $pwd ;MsgBox(0, "platformUrl", $platformUrl) If $env = "prod" Then $cmisUrl = "xxxx/" $platformUrl = "xxxx" & $pwd EndIf WinActivate("[CLASS:SunAwtDialog]") $pos = WinGetPos("Login") ;MsgBox(0, “活动窗口状态 (X坐标,Y坐标,宽度,高度):”, $size[0] & “, ” & $size[1] & “, ” & $size[2] & “, ” & $size[3]) ;MsgBox(0, "", $pos[0]+150) MouseClick($MOUSE_CLICK_LEFT, $pos[0]+150, $pos[1]+90, 1) Send($cmisUrl)
3、选择binding
4、输入username,需要先通过请求url来获得token
Local $objJson = Jsmn_Decode($obj.ResponseText) $token = Jsmn_ObjGet($objJson, "data") ;MsgBox(0, "", $token) MouseClick($MOUSE_CLICK_LEFT, $pos[0]+170, $pos[1]+150, 1) Send($token)
5、选择authentication方式
6、点击load
完整的代码如下。
#include <JSMN.au3> #include <Constants.au3> ; ; AutoIt Version: 3.0 ; Language: English ; Platform: Win9x/NT ; Author: xiuju_lan<xiuju_lan@foxitsoftware.com> ; ; Script Function: ; 自动打开workbench,根据输入的用户名,密码, Local $iLoop = 1, $value = "" While $iLoop = 1 $value = InputBox("环境用户信息", "请按环境,用户名,密码输入,逗号分隔,正式环境用prod","vagrant,admin@fp.com,222222") If @error = 1 Then ;MsgBox($MB_SYSTEMMODAL, "Error", "You pressed 'Cancel' - try again!") Exit 1 Else ; They clicked OK, but did they type the right thing? If StringInStr($value, ",") = 0 Then ; This is not case-sensitive, therefore AutoIt and AuToIT are acceptable values as well MsgBox(80, "输入错误", "请以逗号分隔输入") Else $iLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too EndIf EndIf WEnd ;Get the work dir then Run cmd.exe ,excute the workbench.bat,then close the cmd console $Var = StringSplit (@scriptdir,"\") ;msgbox(0,"",@scriptdir) ;msgbox(0,"",UBound($Var)) Run("cmd.exe") WinWaitActive("[CLASS:ConsoleWindowClass]") Send($Var[1] & "{ENTER}") ;Send("cd D:\chemistry-opencmis-workbench-0.13.0-full{ENTER}") Send(@scriptdir & "\workbench.bat{ENTER}") WinWaitActive("[CLASS:SunAwtDialog]") WinClose("[CLASS:ConsoleWindowClass]") ; deal with the array ,to get the env ,email and password $array = StringSplit($value, ",") $env = $array[1] $email = $array[2] $pwd = $array[3] $cmisUrl = "http://cmis.vagrant.foxitcloud.com:8180/cmis/atom" $platformUrl = "http://platform.vagrant.foxitcloud.com:8003/rest/users/accessToken?email=" & $email & "&password=" & $pwd ;MsgBox(0, "platformUrl", $platformUrl) If $env = "prod" Then $cmisUrl = "xxxx/cmis/atom" $platformUrl = "xxxusers/accessToken?email=" & $email & "&password=" & $pwd EndIf WinActivate("[CLASS:SunAwtDialog]") $pos = WinGetPos("Login") ;MsgBox(0, “活动窗口状态 (X坐标,Y坐标,宽度,高度):”, $size[0] & “, ” & $size[1] & “, ” & $size[2] & “, ” & $size[3]) ;MsgBox(0, "", $pos[0]+150) MouseClick($MOUSE_CLICK_LEFT, $pos[0]+150, $pos[1]+90, 1) Send($cmisUrl) ; Get the token through a request Dim $obj = ObjCreate ("WinHttp.WinHttpRequest.5.1") $obj.Open("GET", $platformUrl, false) ;MsgBox(0, "", $obj) $obj.Send() ;QuickOutput("Response.html",$obj.ResponseText, 2) Local $objJson = Jsmn_Decode($obj.ResponseText) $token = Jsmn_ObjGet($objJson, "data") ;MsgBox(0, "", $token) MouseClick($MOUSE_CLICK_LEFT, $pos[0]+170, $pos[1]+150, 1) Send($token) MouseClick($MOUSE_CLICK_LEFT, $pos[0]+350, $pos[1]+210, 1) MouseClick($MOUSE_CLICK_LEFT, $pos[0]+330, $pos[1]+380, 1)
7.编辑成exe,双击哆啦A梦的exe,如下的效果,就是exe自动执行出来的
最后效果: