要解决的问题

使用 tcl 自动化测试的时候,需要跟windows上的程序进行交互,才能结合起来完成测试任务。比如通过脚本自动化配置网络设备后, 需要windows上的网管程序同步该配置。人工测试的时候需要手动点击,自动化后需要程序进行点击。同时还有些输入工作也可以完成。

其实这种工具很多,但是因为我们使用的是 tcl ,所以找个tcl扩展那时最好的。

cwind

cwind 是一个tcl扩展,用来跟 windows 窗口打交道,比如移动鼠标,发动点击,发动按键组合,激活窗口,发送文字输入等。

cwind 安装

cwind 下载地址 http://www.interq.or.jp/japan/s-imai/tcltk/cwind.html

下载完成后,解压到本地磁盘,目录如下:

cwind1.4
    |----samples
    |----cwind.dll
    |----cwind.tcl
    |----pkgIndex.tcl
    |----install.tcl
    |----readme.txt

打开命令行,直接执行 install.tcl 即可完成安装。

f:\cwind1.4\install.tcl

然后弹出安装界面,选择安装路径,选择tcl安装路径即可比如 c:\Tcl

在 tcl 脚本中需要使用 cwind 模块的时候需要 package require cwind

cwind 使用说明

官方英文使用说明http://www.interq.or.jp/japan/s-imai/tcltk/cwind14.txt

以下为简单翻译。

使用的时候需要 “package require cwind”

cwind提供的命令

在 1.4 版本中新增函数

  • 模拟鼠标左键按下:

::cwind::lclick

  • 模拟鼠标右键按下:

::cwind::rclick

  • 模拟鼠标中间键按下:

::cwind::mclick

  • 设置鼠标位置:

::cwind::setpos {x y}

比如 ` ::cwind::setpos {100 100} ` 把鼠标移动到屏幕的x坐标100,y坐标100位置。这里的屏幕xy坐标系,左上角为 {0 0}

  • 获取鼠标的位置:

::cwind::getpos

  • 获取鼠标相对于当前前台应用窗口的相对坐标:

::cwind::getwpos

  • 设置对于当前前台应用窗口的鼠标相对坐标位置,这时候已当前前台窗口的左上角为原点坐标(标题栏不算,从内容区域算起) {0 0}

::cwind::setwpos {x y}

从1.3 版本支持函数

  • send a message 给当前前台窗口

::cwind::sendmessage <message name> ?<wparam>? ?<lparam>?

Message name is the windows’s message to be sent. Wparam and lparam are additional value.

  • post a message 给当前前台窗口.

::cwind::postmessage <message name> ?<wparam>? ?<lparam>?

Message name is the windows’s message to be posted. Wparam and lparam are additional value.

从1.2 版本支持的函数

主要是扩展的 MDI 一些函数.

  • 把一个打开的窗口从后台放到前台.

::cwind::show ?-exact? <window name>

该命令从已经打开的窗口name列表中找到name对应的窗口,然后放到前台。name支持正则表达式。

比如打开的word程序放到前台

::cwind::show {*Word*}

关于如何知道一个窗口的name是什么,参考后边讲解的 ::cwind::wlist 函数。

  • 获取当前窗口的状态 (Maximized, Minimized or Normal).

::cwind::state ?-exact? <window name>

  • 获取所有打开的窗口列表名字.

::cwind::wlist ?-exact? ?<window name>?

窗口名字可以省略,这样返回的是打开的所有窗口列表名。如果指定了窗口名字,哪儿返回的是她的 mdi 子窗口名。

从1.1 版本支持的函数

  • 把 ::cwind::list 改为了 ::cwind::wlist.

::cwind::wlist

  • 获取窗口状态(Maximized, Minimized or Normal).

::cwind::state ?-exact? <window name>

  • to restore the foreground window at the original size.

::cwind::restore

  • 最小化当前前台窗口

::cwind::minimize

  • 最大化当前前台窗口

::cwind::maximize

  • 最小化所有窗口,回到桌面

::cwind::minimizeAll

  • wait 一个指定的前台窗口(貌似是打开程序的时候需要,参考sample中的例子).

::cwind::waitwind <window name> ?<max wait>?

默认等待 5秒,可以指定最大 wait 秒。

从1.0 版本支持的函数

  • 发送文字到当前前台窗口.

::cwind::send <text to send> …

Send the arguments, separated by spaces. If you want send the spaces, enclose your text into { and }.

  • 把应用放到前台.

::cwind::show ?-exact? <window name>

  • 获取前台窗口的name

::cwind::gettext

  • 获取windows剪切板中的内容

::cwind::getcb

  • 设置windows 剪切板内容

::cwind::putcb

  • to set the sleep time.

::cwind::sleeptime ?<time>?

Time is to ask to the system to wait before each character sent. A sort of delay in milliseconds.

2. 特殊字符(按键?)

如果想发送特殊字符(按键?). For example ctrl+c, ctrl+o, PF4, …

为了跟普通的字符区分, 特殊字符必须放在 | 中间 (the character |).

比如向发送 ctrl+c, 如下 : “|CTRL+| c |CTRL-|”. 这是说要按下 “CTRL”, 保持住然后按下 “c”, 然后释放 “CTRL”.

3. 特殊按键列表:

"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