Python uiautomation初探,测试Win10计算器

在网上找 Python 客户端自动化测试的库,一般有 pywinauto、pywin32、pyautogui、uiautomation。本文仅对 uiautomation 做简单的了解。

MS UI Automation 是 MSAA 技术的一个替代品:即让控件和应用程序具有更好的可达性(accessible)。MS UIA明确定义了两个role:UIA Provider 即软件本身,主要是软件的开发人员依据相应的模式去实现相关的 interface, UIA Client 即自动化脚本和相关的 assistive technology applications,从测试人员的角度出发,主要是调用相应的API去实现自动化测试脚本。

而 uiautomation 的作者根据微软手册,用 Python 和 C++ 对 UI Automation 做了一层封装。

MS UI Automation概述:https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview

作者博客:开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qt - YinKaisheng - 博客园

Github地址:https://github.com/yinkaisheng/Python-UIAutomation-for-Windows

参考博客:Uiautomation 在Windows WPF和Qt 产品上的应用_yaoliuwei1426的博客-CSDN博客

参考博客:MS UI Automation简介_ffeiffei的专栏-CSDN博客_uiautomation

根据作者提供的示例,我对 Win10 计算器做了简单的计算测试,代码如下:

import subprocess
import uiautomation as uia

# 设置全局搜索超时 15s
uia.uiautomation.SetGlobalSearchTimeout(15)

# 测试Win10计算器
def test_calc():
    # 启动计算器进程
    subprocess.Popen(args='calc.exe')
    # 首先从桌面的第一层子控件中找到程序的窗口,再从这个窗口查找子控件
    window = uia.WindowControl(
        searchDepth=1, ClassName='ApplicationFrameWindow', Name='计算器')
    # 窗口置顶
    window.SetTopmost(True)
    # 根据文档描述,直接从顶层找空间的效率较低,因为每次找都需要去遍历
    # 所以这里可以根据inspect查看到的对象树来逐层找
    # Name/ClassName/AutomationId都是UIAutomation属性,可以通过工具查看到,类似于网页的选择器
    window_main = window.Control(
        searchDepth=1, ClassName='Windows.UI.Core.CoreWindow')
    button_group = window_main.Control(
        searchDepth=1, ClassName='LandmarkTarget')
    number_group = button_group.Control(searchDepth=1, Name='数字键盘')
    calc_group = button_group.Control(searchDepth=1, Name='标准运算符')

    # 模拟按键
    window.SendKeys('1')
    #number_group.ButtonControl(Name='一').Click()
    calc_group.ButtonControl(Name='加').Click()
    number_group.ButtonControl(Name='四').Click()
    calc_group.ButtonControl(Name='等于').Click()
    result = button_group.Control(AutomationId='NormalOutput').Name
    print("测试结果:", result, ' bytes:', bytes(result, encoding='utf8').hex())
    # 获取到的文本前后有不可见字符,这里先替换掉
    result = result.replace(b'\xe2\x80\xad'.decode('utf8'), '').replace(
        b'\xe2\x80\xac'.decode('utf8'), '')
    print("测试结束,预期=5,实际={0},{1}".format(
        result, "测试通过" if result == "5" else "测试失败"))
    # 通过复制粘贴获取,这样得到的文本没有其他字符
    window.SendKeys('{Ctrl}c', waitTime=1)
    result = uia.GetClipboardText()
    print("测试结果:", result, ' bytes:', bytes(result, encoding='utf8').hex())
    # 截图并保存到指定路径
    window.CaptureToImage('C:/Users/1992/Pictures/1.png')
    # 关闭程序
    #window.ButtonControl(Name='关闭').Click()
    window.GetWindowPattern().Close()

if __name__ == '__main__':
    test_calc()

测试结果:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龚建波

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值