Uiautomation 在Windows WPF和Qt 产品上的应用

前面使用Pywinauto 对公司自研的Windows WPF和Qt进行了应用。

最近用Uiautomation 进行了试验,发现Uiautomation 比Pywinauto更加易用,更好上手。

如果是做Windows程序的自动化测试,推荐使用Uiautomation

Uiautomation相关文章:

Windows GUI自动化测试技术的比较和展望:https://blog.csdn.net/vagabond1/article/details/5648902

开源自己用python封装的一个Windows GUI(UI Automation)自动化工具,支持MFC,Windows Forms,WPF,Metro,Qthttp://www.cnblogs.com/Yinkaisheng/p/3444132.html

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

下载地址:https://pypi.org/project/uiautomation/#history

简单介绍下用法,详细的可以参考作者的原博客

1、程序启动:

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

2、窗口定位

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

3、控件定位

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

4、鼠标动作

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

5、窗口的等待

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

项目代码如下:

import os, sys, time
import subprocess
import uiautomation as automation
from pywinauto import application
automation.ShowDesktop()
# 打开cas
subprocess.Popen(args='D:\Release\Higemi.exe',cwd=r"D:\Release")
# # 查找cas
cas = automation.WindowControl(searchDepth=1, AutomationId='myMainWindow', Name='Login')
# 可以判断window是否存在,如果不判断,找不到window的话会抛出异常
if automation.WaitForExist(cas, 3):
    automation.Logger.WriteLine("CAS exists now",logFile="cas.txt")
else:
    automation.Logger.WriteLine("CAS does not exist after 3 seconds",logFile="cas.txt")
cas.SetActive()
# # 登录
login=cas.ButtonControl(AutomationId ="btn_login")
login.Click()
# 查找病例
patientManager = automation.WindowControl(Name = 'PatientManager',AutomationId='myMainWindow')
patientManager.SetActive()
patient = patientManager.TextControl(Name = 'LI QIANG',ClassName="TextBlock",foundIndex=1)
patient.Click()
# # 进入三维重建
_3dRebuidBtn = patientManager.ButtonControl(AutomationId="_3dRebuidBtn")
_3dRebuidBtn.Click()
time.sleep(10)
lung = patientManager.ImageControl(AutomationId="lung")
lung.Click()
# 设置窗宽窗位
WL = patientManager.EditControl(AutomationId="WL")
WL.SetValue("600")
WB = patientManager.EditControl(AutomationId="WB")
WB.SetValue("1400")
w_OK = patientManager.ButtonControl(AutomationId = "w_OK")
w_OK.Click()
wwwl_preset = patientManager.ComboBoxControl(AutomationId="wwwl_preset")
wwwl_preset.Select("CT Lung")
# 选择种子点并执行
segmentation_TakePixel = patientManager.ButtonControl(AutomationId="segmentation_TakePixel")
segmentation_TakePixel.Click()
automation.Win32API.MouseClick(600,250)
automation.Win32API.MouseWheelDown(2)
automation.Win32API.MouseClick(600,350)
segmentation_Show = patientManager.ButtonControl(AutomationId = "segmentation_Show")
segmentation_Show.Click()
BarWindow = automation.WindowControl(Name= "MetroProBarWindow",AutomationId ="myMainWindow",ClassName="Window")
while automation.WaitForExist(BarWindow,3):
    pass
print("快速分割完成")
# 保存
save = patientManager.ButtonControl(AutomationId = "save")
save.Click()
umessageBox = automation.WindowControl(Name="UMessageBox",AutomationId ="myMainWindow",ClassName="Window")
if automation.WaitForExist(umessageBox,3):
    yes = umessageBox.TextControl(AutomationId ="tb_YES")
    yes.Click()
    print("保存成功")
 
 

 

 

 

 

 

你可能感兴趣的:(自动化测试,python)