使用Tcl擴充套件包cwind進行介面自動化測試

jearmy發表於2012-02-16
cwind是一個控制介面操作的Tcl擴充套件包,它可以模擬鍵盤、滑鼠的操作,捕獲介面資訊,控制介面視窗等,類似於QARUN等軟體,透過cwind庫可以利用tcl指令碼方便的實現QARUN的功能,因此可以用於各種GUI測試。
[@more@]

cwind擴充套件包命令介紹:

to simulate a left click on the mouse
::cwind::lclick

to simulate a right click on the mouse
::cwind::rclick

to simulate a middle click on the mouse
::cwind::mclick

to set the mouse position
::cwind::setpos
where the starting position (0 0) is the upper left corner

to get the mouse position
::cwind::getpos

to get the cursor postion based on the position of the foreground window
::cwind::getwpos

to set the cursor postion based on the position of the foreground window
::cwind::setwpos

In version 1.3

to send a message to the foreground window.
::cwind::sendmessage ?? ??
Message name is the windows's message to be sent.
Wparam and lparam are additional value.

to post a message to the foreground window.
::cwind::postmessage ?? ??
Message name is the windows's message to be posted.
Wparam and lparam are additional value.

In version 1.2

I've expanded the specification of some commands for MDI.

to put in active a window.
::cwind::show ?-exact?
Search into the list of active window's name and set in foreground this window.
Window's name is treated as a glob-style pattern.
Window's name is specified in list form,
The first element is a parent window name and the optional second element is a MDI child window name.

to get the status of a window (Maximized, Minimized or Normal).
::cwind::state ?-exact?
Window's name is treated as a glob-style pattern.
Window's name is specified in list form,
The first element is a parent window name and the optional second element is a MDI child window name.

to have a list of all opened windows.
::cwind::wlist ?-exact? ??
If Window's name is omitted, Will show you a Tcl list with all the window's name.
If Window's name is specified, Will show you a Tcl list with all the MDI child window's name.
Window's name is treated as a glob-style pattern.

In version 1.1

Due to confilcts with the Tcl command "list",
I've renamed the command ::cwind::list to ::cwind::wlist.
::cwind::wlist

to get the status of a window (Maximized, Minimized or Normal).
::cwind::state ?-exact?
Window's name is treated as a glob-style pattern.

to restore the foreground window at the original size.
::cwind::restore

to minimize the foreground window.
::cwind::minimize

to maximize the foreground window.
::cwind::maximize

to minimize all the windows.
::cwind::minimizeAll

to wait a specific window on the foreground.
::cwind::waitwind ??
Max wait is in seconds and the default value is 5.
Window's name is treated as a glob-style pattern.

In version 1.0

to send a text to the foreground window.
::cwind::send ...
Send the arguments, separated by spaces.
If you want send the spaces, enclose your text into { and }.

to put in active a window.
::cwind::show ?-exact?
Search into the list of active window's name and set in foreground this window.
Window's name is treated as a glob-style pattern.

to get the active window name.
::cwind::gettext
Get the test of the foreground window.

to get the content of the clipboard.
::cwind::getcb

to put strings into the clipboard.
::cwind::putcb

to have a list of all opened windows.
::cwind::list
Will show you a Tcl list with all the window's name.

to set the sleep time.
::cwind::sleeptime ?

一些特殊鍵的用法:

如果要使用像ctrl + c這樣的特殊鍵,必須用下面的字串序列來表示

"|CTRL+| c |CTRL-|"

特殊鍵的名字對應關係如下:

"ALT+"	press ALT "ALT-"	release ALT "ALTL+"	press left ALT key "ALTL-"	release left ALT key "ALTR+" press right ALT key "ALTR-"	release right ALT key "CTRL+"	press CTRL "CTRL-"	release CTRL "CTRLL+" press left CTRL key "CTRLL-"	release left CTRL key "CTRLR+"	press right CTRL key "CTRLR-"	release right CTRL key "SHIFT+"	press SHIFT "SHIFT-"	release SHIFT "SHIFTL+"	press left SHIFT key "SHIFTL-" release left SHIFT key "SHIFTR+"	press right SHIFT key "SHIFTR-"	release right SHIFT key "TAB" press the tabulation key "RET"	press the return key "ESC"	press the escape key "BACK"	press the backward key "DEL"	press the delete key "INS"	press the insert key "HELP"	press the help key "LEFT" send the cursor to the left "RIGHT"	send the cursor to the right "UP"	send the cursor to up "DOWN" send the cursor to down "PGUP"	press the page up key "PGDN"	press the page down key "HOME"	press the home key "END"	press the end key "F1"	press the function key F1 "F2"	press the function key F2 "F3"	press the function key F3 "F4"	press the function key F4 "F5"	press the function key F5 "F6" press the function key F6 "F7"	press the function key F7 "F8"	press the function key F8 "F9"	press the function key F9 "F10"	press the function key F10 "F11"	press the function key F11 "F12"	press the function key F12 "NUM0"	press the 0 on the key pad "NUM1"	press the 1 on the key pad "NUM2" press the 2 on the key pad "NUM3"	press the 3 on the key pad "NUM4"	press the 4 on the key pad "NUM5"	press the 5 on the key pad "NUM6"	press the 6 on the key pad "NUM7"	press the 7 on the key pad "NUM8"	press the 8 on the key pad "NUM9"	press the 9 on the key pad "NUM*"	press the * on the key pad "NUM+"	press the + on the key pad "NUM-"	press the - on the key pad "NUM,"	press the , on the key pad "NUM/"	press the / on the key pad "SNAP"	press the print key "APPS"	press the application key "KANJI"	press the kanji key "CONV"	press the convert key "NCONV"	press the nonconvert key

下面是一段演示程式,演示了開啟一個記事本,輸入hello world,開啟about視窗,然後複製視窗到剪貼簿,開啟畫筆,把介面抓圖複製到畫圖程式。下面程式中使用的視窗名是按照英文作業系統的,對於中文作業系統,需要修改其中的視窗名,例如*WordPad修改為記事本的視窗名,*Paint修改為畫筆的中文視窗名。

package require cwind exec $env(COMSPEC) /c start wordpad.exe & ::cwind::waitwind {*WordPad} 10::cwind::send {Hello World!} |RET| after 1000 ::cwind::send |CTRL+| a |CTRL-| after 1000::cwind::send |CTRL+| c |CTRL-| after 1000 set clip [::cwind::getcb] after 1000 ::cwind::send |END| |CTRL+| v |CTRL-| after 1000 ::cwind::send |ALT+| h |ALT-| a after 1000 ::cwind::send |ALT+| |SNAP| |ALT-| exec $env(COMSPEC) /c start mspaint.exe & ::cwind::waitwind {*Paint} 10 ::cwind::send |ALT+| e |ALT-| p exit

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/459749/viewspace-1057378/,如需轉載,請註明出處,否則將追究法律責任。

相關文章