当前位置:   article > 正文

使用uiautomation模块来对基于windows系统的pc中的前端界面进行自动化测试(查找控件,点击控件等)_win uiautomation

win uiautomation

uiautomation模块

uiautomation模块是第三方模块需要先安装

python -m pip install uiautomation
  • 1

或者可以直接在github上面下载uiautomation,作者yinkaisheng在github上开源开发的uiautomation模块,直接下载也可以研究下他的代码

uiautomation模块github地址: https://github.com/yinkaisheng/Python-UIAutomation-for-Windows
uiautomation模块github地址

uiautomation模块的自动化测试需要配合UISPY.exe或者inspect.exe工具来进行调试大佬的工程里面也有这两个工具
参照博客

网上其他人的代码未深入研究

import os
import subprocess
import uiautomation
import time

#打开计算器进程
subprocess.Popen('calc.exe')
time.sleep(2)

# 四类标签格式
# 程序窗口:WindowControl()
# 按钮:ButtonControl()
# 文件显示:TextControl()
# 输入框:EditControl()

# 元素属性 ClassName、Name、ProcessId、AutomationId

#定位窗口
# # wc=uiautomation.WindowControl(searchDepth=1,Name='计算器')
wc=uiautomation.WindowControl(Name='计算器')

#将程序设置为顶层(页面放到最上面防止被别的程序盖住)
wc.SetTopmost(True)

# DoubleClick() 双击
# Click() 点击;
# RighClik() 右键点击;
# SendKeys() 发送字符;
# SetValue() 传值,一般对EditControl用;

wc.ButtonControl(Name='一').Click()
wc.ButtonControl(Name='二').Click()
wc.ButtonControl(Name='乘以').Click()
wc.ButtonControl(Name='三').Click()
wc.ButtonControl(Name='等于').Click()

# 获取元素某标签的具体标签内容
result=wc.TextControl(AutomationId='CalculatorResults')
print(result.Name)
print(result.ClassName)
print(result.ProcessId)
print(result.AutomationId)
print(result.Culture)
print(result.ControlType)

if result.Name=="显示为 36":
    print("测试成功")
else:
    print("测试失败")

#截图
# wc.CaptureToImage('1222222222222222222222222222222222222.png')

time.sleep(2)
wc.ButtonControl(Name='关闭 计算器').Click()
os.system("taskkill /F /IM calc.exe")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
显示为 36

14696
CalculatorResults
2052
50020
测试成功
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

从当前电脑已经打开的窗口中找寻某一个窗口,并对其操作,且可以再次查找便利这个窗口里面的所有子控件

查找时的参数介绍
searchFromControl = None:从哪个控件开始查找,如果为None,从根控件Desktop开始查找
searchDepth = 0xFFFFFFFF: 搜索深度 在指定的深度范围内(往下数几级)查找
searchInterval = SEARCH_INTERVAL:搜索间隔
foundIndex = 1 :搜索到的满足搜索条件的控件索引,索引从1开始
Name:控件名字
SubName :控件部分名字
RegexName:使用re.match匹配符合正则表达式的名字,Name,SubName,RegexName只能使用一个,不能同时使用
ClassName :类名字
AutomationId: 控件AutomationId
ControlType :控件类型
Depth:控件相对于searchFromControl的精确深度,只在指定的第几级查找
Compare:自定义比较函数function(control: Control, depth: int)->bool

    import uiautomation

    # 查找获取当前电脑上的某个窗口对象(不管当前窗口是最顶层还是被盖住或者最小化到任务栏,只要存在就能找到)
    # window = uiautomation.WindowControl(searchDepth=1, Name='管理员: 命令提示符', AutomationId='Console Window')
    window = uiautomation.WindowControl(searchDepth=1, Name='计算器')

    # 如果存在则打印这个窗口控件的基础属性
    if window.Exists():
        print(window.Name)
        print(window.AutomationId)
        print(window.ClassName)
        print(window.ControlTypeName)

        # 如果存在则枚举便利这个窗口对象中的所有子控件
        for one_control, one_depth in uiautomation.WalkControl(window, True, 99999999999999999):
            print(one_control.Name)
            print(one_control.AutomationId)
            print(one_control.ClassName)
            print(one_control.ControlTypeName)
            print("======================")

        # 窗口操作
        # 窗口置顶
        window.SetTopmost()
        # 窗口最大化
        window.Maximize()
        # 窗口最小化
        window.Minimize()
        # 窗口激活(置顶)
        window.SetActive()

        # # 窗口对象是cmd控制台的话这里可以打印控制台的文本显示内容
        # cmd_text = window.DocumentControl(searchDepth=1).GetTextPattern().DocumentRange.GetText()
        # print(cmd_text)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

自动化找寻当前最上层窗口的所有控件并锁定自己需要的控件并点击,(推荐)

    import uiautomation as auto
    import os
    import time
    import re

    def check_now_top_page_all_control_node():
        """
        查找当前最顶层页面的所有元素控件
        :return: 包含每个元素控件信息字典的列表[{}, {}]
        """
        now_page_all_node_info_dict_list = []
        control = auto.GetFocusedControl()
        controlList = []
        while control:
            controlList.insert(0, control)
            control = control.GetParentControl()
        if len(controlList) == 1:
            control = controlList[0]
        else:
            control = controlList[1]
        for one_control, one_depth in auto.WalkControl(control, True, 99999999999999999):
            now_page_all_node_info_dict_list.append({"ControlNodeName" : one_control.Name, "ControlNodeClassName" : one_control.ClassName, "ControlNodeAutomationId" : one_control.AutomationId,
                                            "ControlNodeControlType" : one_control.ControlTypeName,"ControlNodeHandle" : '0x{0:X}({0})'.format(one_control.NativeWindowHandle),
                                            "ControlNodeRect" : one_control.BoundingRectangle,})
        return now_page_all_node_info_dict_list


    def find_target_node_use_name(control_node_name, is_re=False, find_timeout_time=10):
        """
        在当前页面循环寻找目标控件
        :param control_node_name: 控件名字
        :param is_re: 是否正则寻找默认不正则寻找
        :param find_timeout_time: 查找超时时间,默认十秒,超时未找到返回None
        :return: 找到返回控件字典对象,超时未找到返回None
        """
        end_time = start_time = time.time()
        while end_time - start_time <= find_timeout_time:
            ret_list = check_now_top_page_all_control_node()
            for i in ret_list:
                if is_re:
                    if re.findall(control_node_name, i["ControlNodeName"]):
                        return i
                else:
                    if control_node_name == i["ControlNodeName"]:
                        return i
           	end_time = time.time()
    # 打开calc
    os.popen("calc")
    # 查找按钮   八
    ret = find_target_node_use_name("八")
    auto.Click(int(ret["ControlNodeRect"].xcenter()), int(ret["ControlNodeRect"].ycenter()))
    ret = find_target_node_use_name("一")
    auto.Click(int(ret["ControlNodeRect"].xcenter()), int(ret["ControlNodeRect"].ycenter()))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/476389
推荐阅读
相关标签
  

闽ICP备14008679号