自动化工具 Uiautomation 在 Windows WPF 和 Qt 产品上的应用

tudou · 2019年05月08日 · 最后由 卡里啦 回复于 2020年01月03日 · 3316 次阅读

公司产品主要是 Windows App,最近用 PywinAuto 和 Uiautomation 进行了自动化测试的尝试。
从易用性和易使用 上,个人觉得 Uiautomation 更加好用。

并且自带控件查看工具,比 Windows 自带的 Inspector.exe 更加好用:
运行 CMD,输入 automation.py -t3 回车,然后 3 秒内切换到应用界面。

automation.py –t3, 3 秒后遍历当前窗口所有控件
automation.py –d2 –t3, 3 秒后遍历当前窗口前三层控件
automation.py –r –d1 –t0 -n, 0 秒后遍历 root 的第一层子控件,并显示控件完整名称
automation.py –c –t3, 3 秒后遍历鼠标光标下面的控件信息
automation.py –c –t3 -d-2, 3 秒后遍历鼠标光标下面的控件向上两层的父控件

安装

pip install uiautomation

程序启动

subprocess.Popen(args='D:\Release\Higemi.exe',cwd=r"D:\Release")

窗口定位

cas = automation.WindowControl(searchDepth=1, AutomationId='myMainWindow', Name='Login')

控件定位

login=cas.ButtonControl(AutomationId ="btn_login")

鼠标动作

automation.Win32API.MouseClick(600,250)
automation.Win32API.MouseWheelDown(2)

窗口或控件的等待

#在超时时间内寻找指定的窗口或控件,找不到则抛出异常
automation.WaitForExist(cas, 3) 

Uiautomation 相关文章:

Windows GUI 自动化测试技术的比较和展望:https://blog.csdn.net/vagabond1/article/details/5648902
开源自己用 python 封装的一个 Windows GUI(UI Automation) 自动化工具,支持 MFC,Windows Forms,WPF,Metro,Qt :http://www.cnblogs.com/Yinkaisheng/p/3444132.html
Github 地址:https://github.com/yinkaisheng/Python-UIAutomation-for-Windows
下载地址:https://pypi.org/project/uiautomation/#history

共收到 7 条回复 时间 点赞

大佬,PywinAuto,Uiautomation 这两个有啥区别?平时都用 pywinauto 和 pyautogui

WPF 不是自带一套 uiautomation 的吗?

港真,我都不知道发生了什么,为什么大家都不待见 autoit 这种神器呢😂

槽神 回复

我们公司的产品,我用 autoit 定位不到控件。。。

kukaka 回复

基于微软的 Uiautomation,大佬用 python 封装了下

卡里啦 回复

我用了下 pywinauto,觉得元素的定位和操作没有 uiautomation 简单。这个只是个人见解。用熟悉了,哪个都一样。

buttonNotSave = uiautomation.ButtonControl(SubName = '修改'),运行这代码后,鼠标移动的速度能调吗,这速度没有用坐标点击的快啊,还有 searchDepth=1 这参数有啥用?求大佬回复

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册