在GUI测试过程中,通常会碰到对象无法被测试工具识别的问题,这时候往往需要特殊的解决办法,其中一种方法就是通过发送键盘按键的方式来操纵控件。
下面以QQ2009正式版为例,介绍使用Type和Mercury.DeviceReplay对象处理控件的方法。
以下是录制的脚本:
Window("QQ2009 正式版").Activate
Window("QQ2009 正式版").WinObject("123456").Click 46,11
Window("QQ2009 正式版").WinObject("123456").Type "123456"
Window("QQ安全中心").Activate
Window("QQ安全中心").Click 369,302
可以看到密码输入的步骤没有录制下来,按下登录按钮的步骤也没有录制下来。在对象库手工添加对象后再录制,仍然没有把密码输入的步骤录制下来,因此需要手工添加这一步的脚本(另外在对象库中也不能手工地把登录按钮对象添加进去)。调整脚本如下:
Window("QQ2009 正式版").Activate
Window("QQ2009 正式版").WinObject("123456").Click 46,11
Window("QQ2009 正式版").WinObject("123456").Type "123456"
Window("QQ2009 正式版").WinEdit("Edit").Type "123456"
' 按5次TAB键后聚焦在“登录”按钮上面
For I=1 to 5
Window("QQ2009 正式版").Type micTab
Next
Window("QQ2009 正式版").Type micReturn ' 按回车键模拟用户点击“登录”按钮
在这里使用了Window测试对象的Type方法来模拟按键,Type方法接受一个按键常量作为输入参数。
登录如果失败会出现“QQ安全中心”界面,在这个界面中“取消按钮”也没有被识别出来,因此需要采用相同的办法来处理:
If Window("QQ安全中心").Exist(10) Then ' 密码输入不正确
Window("QQ安全中心").Activate
Window("QQ安全中心").Type micTab
Window("QQ安全中心").Type micReturn
End If
当然,这里也可以使用DeviceReplay对象来模拟按键。
附Type方法的按键常量:
Constant | Action |
micCtrlDwn | Presses the Ctrl key. |
micCtrlUp | Releases the Ctrl key. |
micLCtrlDwn | Presses the left Ctrl key. |
micLCtrlUp | Releases the left Ctrl key. |
micRCtrlDwn | Presses the right Ctrl key. |
micRCtrlUp | Releases the right Ctrl key. |
micAltDwn | Presses the Alt key. |
micAltUp | Releases the Alt key. |
micLAltDwn | Presses the left Alt key. |
micLAltUp | Releases the left Alt key. |
micRAltDwn | Presses the right Alt key. |
micRAltUp | Releases the right Alt key. |
micShiftDwn | Presses the Shift key. |
micShiftUp | Releases the Shift key. |
micLShiftDwn | Presses the left Shift key. |
micLShiftUp | Releases the left Shift key. |
micRShiftDwn | Presses the right Shift key. |
micRShiftUp | Releases the right Shift key. |
micIns | Presses the Insert key. |
micDel | Presses the Delete key. |
micHome | Presses the Home key. |
micEnd | Presses the End key. |
micPgUp | Presses the Page Up key. |
micPgDwn | Presses the Page Down key. |
micUp | Presses the Up arrow key. |
micDwn | Presses the Down arrow key. |
micLeft | Presses the Left arrow key. |
micRight | Presses the Right arrow key. |
micEsc | Presses the Esc key. |
micBack | Presses the Backspace key. |
micReturn | Presses the Return key. |
micTab | Presses the Tab key. |
micBreak | Presses the Break key. |
micPause | Presses the Pause key. |
micPrintScr | Presses the Print Screen key. |
micWinLogoDwn | Presses the Windows Logo key. |
micWinLogoUp | Releases the Windows Logo key. |
micLWinLogoDwn | Presses the left Windows Logo key. |
micLWinLogoUp | Releases the left Windows Logo key. |
micRWinLogoDwn | Presses the right Windows Logo key. |
micRWinLogoUp | Releases the right Windows Logo key. |
micAppKey | Presses the Application key. |
micF1 | Presses the F1 key. |
micF2 | Presses the F2 key. |
micF3 | Presses the F3 key. |
micF4 | Presses the F4 key. |
micF5 | Presses the F5 key. |
micF6 | Presses the F6 key. |
micF7 | Presses the F7 key. |
micF8 | Presses the F8 key. |
micF9 | Presses the F9 key. |
micF10 | Presses the F10 key. |
micF11 | Presses the F11 key. |
micF12 | Presses the F12 key. |
micNumLockOn | Turns on the Num Lock. |
micCapsLockOn | Turns on the Caps Lock. |
micScrollOn | Turns on the Scroll Lock. |
micNumLockOff | Turns off the Num Lock. |
micCapsLockOff | Turns off the Caps Lock. |
micScrollOff | Turns off the Scroll Lock. |