今天在做回归测试的时候发现一条用例是查询数据库数据,于是思考如何使用工具自动化的方式解决该问题
首先想到的是jmeter的jdbc request配置大概如下
因为听说airtest也可以进行windows操作,随后开始研究如何使用airtest执行windows操作
打开airtest官网发现有三种方式可以连接windows应用窗口分别对应三个按钮
1-选择游戏画面:通过选择对应的应用窗口将程序嵌入airtestIDE进行操作
选取后会嵌入到IDE中
2-搜索窗口
点击对应应用窗口的名称进行连接即可
3.点击图标使用windows桌面直接操作断开windows应用窗口的方式也非常简单,点击下图图标选择断开连接即可
整个操作过程中共碰到2个问题
1.airtest操作windwos应用时,对于应用弹出的窗口识别度不高
这种图标虽小,但是色差比较明显的图标可以清晰识别,但是对于弹框或者悬浮菜单无法可靠识别
为了解决问题,同时考虑到windows窗口对于鼠标键盘的可替代性,于是选择使用模拟键盘的方式解决上述问题
这里引出第二个问题,键盘操作底层是基于pywinauto开发的,所以按键略有不同,具体可参考
{SCROLLLOCK}, {VK_SPACE}, {VK_LSHIFT}, {VK_PAUSE}, {VK_MODECHANGE},
{BACK}, {VK_HOME}, {F23}, {F22}, {F21}, {F20}, {VK_HANGEUL}, {VK_KANJI},
{VK_RIGHT}, {BS}, {HOME}, {VK_F4}, {VK_ACCEPT}, {VK_F18}, {VK_SNAPSHOT},
{VK_PA1}, {VK_NONAME}, {VK_LCONTROL}, {ZOOM}, {VK_ATTN}, {VK_F10}, {VK_F22},
{VK_F23}, {VK_F20}, {VK_F21}, {VK_SCROLL}, {TAB}, {VK_F11}, {VK_END},
{LEFT}, {VK_UP}, {NUMLOCK}, {VK_APPS}, {PGUP}, {VK_F8}, {VK_CONTROL},
{VK_LEFT}, {PRTSC}, {VK_NUMPAD4}, {CAPSLOCK}, {VK_CONVERT}, {VK_PROCESSKEY},
{ENTER}, {VK_SEPARATOR}, {VK_RWIN}, {VK_LMENU}, {VK_NEXT}, {F1}, {F2},
{F3}, {F4}, {F5}, {F6}, {F7}, {F8}, {F9}, {VK_ADD}, {VK_RCONTROL},
{VK_RETURN}, {BREAK}, {VK_NUMPAD9}, {VK_NUMPAD8}, {RWIN}, {VK_KANA},
{PGDN}, {VK_NUMPAD3}, {DEL}, {VK_NUMPAD1}, {VK_NUMPAD0}, {VK_NUMPAD7},
{VK_NUMPAD6}, {VK_NUMPAD5}, {DELETE}, {VK_PRIOR}, {VK_SUBTRACT}, {HELP},
{VK_PRINT}, {VK_BACK}, {CAP}, {VK_RBUTTON}, {VK_RSHIFT}, {VK_LWIN}, {DOWN},
{VK_HELP}, {VK_NONCONVERT}, {BACKSPACE}, {VK_SELECT}, {VK_TAB}, {VK_HANJA},
{VK_NUMPAD2}, {INSERT}, {VK_F9}, {VK_DECIMAL}, {VK_FINAL}, {VK_EXSEL},
{RMENU}, {VK_F3}, {VK_F2}, {VK_F1}, {VK_F7}, {VK_F6}, {VK_F5}, {VK_CRSEL},
{VK_SHIFT}, {VK_EREOF}, {VK_CANCEL}, {VK_DELETE}, {VK_HANGUL}, {VK_MBUTTON},
{VK_NUMLOCK}, {VK_CLEAR}, {END}, {VK_MENU}, {SPACE}, {BKSP}, {VK_INSERT},
{F18}, {F19}, {ESC}, {VK_MULTIPLY}, {F12}, {F13}, {F10}, {F11}, {F16},
{F17}, {F14}, {F15}, {F24}, {RIGHT}, {VK_F24}, {VK_CAPITAL}, {VK_LBUTTON},
{VK_OEM_CLEAR}, {VK_ESCAPE}, {UP}, {VK_DIVIDE}, {INS}, {VK_JUNJA},
{VK_F19}, {VK_EXECUTE}, {VK_PLAY}, {VK_RMENU}, {VK_F13}, {VK_F12}, {LWIN},
{VK_DOWN}, {VK_F17}, {VK_F16}, {VK_F15}, {VK_F14}
https://www.kancloud.cn/gnefnuy/pywinauto_doc/1193047
同时对于修饰符的使用会在airtest的text方法中被自动过滤,如下语句
select count(CAID) as a from hk where LOID in (‘0070235002350’,‘0070161401614’,‘0070161801618’)
在使用text方法后会被输入成 selectcountCAIDasafromhkwhereLOIDin’0070235002350’,‘0070161401614’,‘0070161801618’
仔细观察发现空格和()不见了
此处如需要输入正确语句,需要将所有空格替换为{SPACE},换行替换为{ENTER},而将()通过加{}的方式解决,{}在这里有点类似Java中的\转义符
替换后select{SPACE}count{(}CAID{)}{SPACE}as{SPACE}a{ENTER}from{SPACE}hk{ENTER}where{SPACE}LOID{SPACE}in{SPACE}{(}‘0070235002350’,‘0070161401614’,‘0070161801618’{)}
于是,问题解决了,现在可以通过airtest做windows应用的测试了
整个过程中总结为以下三点:
1.airtest对于windows截图的解析还不够稳定,时常出现截图可以识别,再次运行就无法识别的情况
2.airtest对于windows的应用操作存在一定局限性,建议使用第一或第二种方式更为方便
3.airtest对于windows应用的自动打开和使用没有实现(这里没有深入研究,个人猜测是可以通过命令行实现的)
最后附上大致图片,可以看到大致操作方式