桌面应用自动化UIAutomation

/ 测试开发 / 0 条评论 / 160浏览
import os
import sys
import subprocess
import uiautomation
from uiautomation import Logger,Bitmap
import time
import json
import ast

'''
UIAutomation的工作原理是:
IRawElementProviderSimple就是UI Automation Provider,包含了控件的各种信息,如Name,ClassName,ContorlType,坐标...
UIAutomation根据程序返回的IRawElementProviderSimple,就能遍历程序的控件,得到控件各种属性,进行自动化操作。
所以如果你发现UIAutomation不能识别一些程序内的控件或部分不支持,这并不是UIAutomation的问题,
是程序作者没有处理WM_GETOBJECT或没有实现UIAutomation Provider,或者故意不想支持UIAutomation。

#######
程序窗口:WindowControl()
按钮:ButtonControl()
文件显示:TextControl()
输入框:EditControl()
MenuItemControl
ComboBoxControl
windowFont = window.WindowControl(Name='字体')
combo = windowFont.ComboBoxControl(AutomationId='1140')

一般定位的属性有:ClassName、Name、ProcessId、AutomationId
操作:
Click()     点击
RighClik()  右键点击
SendKeys()  发送字符
SetValue()  传值,一般对EditControl用
subprocess.Popen('Name')   用进程打开程序
window.Close()         关闭窗口
window.SetActive()         使用
window.SetTopMost()       设置为顶层
window.ShowWindow(uiautomation.ShowWindow.Maximize)  窗口最大化
window.CaptureToImage('Notepad.png')   截图
dir(obj)
checkbox.GetTogglePattern().ToggleState
edit.GetValuePattern().Value

uiautomation - 全局方法
ShowDesktop()
WaitForExist(window, 3)
WaitForDisappear(window, 3)
GetClipboardText()
SetClipboardText(text)
DragDrop
WheelDown
WheelUp
Show
Hide
MoveWindow
Logger.WriteLine("Notepad does not exist after 3 seconds", automation.ConsoleColor.Yellow)
#查找方法
buttonNotSave = automation.FindControl(window, lambda control, depth: control.ControlType == automation.ControlType.ButtonControl and u'不保存' in control.Name)

import comtypes.client
a = comtypes.client.GetModule(r'C:\\Windows\\System32\\UIAutomationCore.dll')
b = comtypes.client.CreateObject('{ff48dba4-60ef-4201-aa87-54103eef594e}', interface=a.IUIAutomation)
d = b.RawViewWalker
print(d)
'''
'''
#打开计算器进程
subprocess.Popen('calc.exe')
time.sleep(2)
#定位窗口
wc=uiautomation.WindowControl(searchDepth=1,RegexName='计算器')
print("ProcessId:"+str(wc.ProcessId))
#设置为顶层
wc.SetTopmost(True)
wc.ButtonControl(Name='七').Click()
wc.ButtonControl(Name='加').Click()
wc.ButtonControl(Name='五').Click()
wc.ButtonControl(Name='等于').Click()
result=wc.TextControl(AutomationId='CalculatorResults')
print(result.Name)
if result.Name=="显示为 12":
    print("测试成功")
else:
    print("测试失败")
#截图
wc.CaptureToImage('1.png')
time.sleep(2)
wc.ButtonControl(AutomationId='Close').Click()
#uiautomation.Logger.ColorfullyWriteLine("Notepad does not exist after 3 seconds", uiautomation.ConsoleColor.Red, logFile="a.txt")
'''
    
'''
subprocess.Popen('notepad.exe')
time.sleep(2)
notepad=uiautomation.WindowControl(searchDepth=1,Name='无标题 - 记事本')
notepad.SetTopmost(True)
notepad.MenuItemControl(Name='格式(O)').Click()
notepad.MenuItemControl(Name='字体(F)...').Click()
windowFont = notepad.WindowControl(Name='字体')
font_data = {"字体(F):": "微软雅黑", "字形(Y):": "常规", "大小(S):": "小四"}
for k, v in font_data.items():
    listItem = windowFont.ListControl(
        searchDepth=2, AutomationId='1000', Name=k).ListItemControl(Name=v)
    if listItem.Exists(1):
        listItem.GetScrollItemPattern().ScrollIntoView()
        listItem.Click()
windowFont.SetActive()
combo = windowFont.ComboBoxControl(AutomationId='1140')
combo.Select('中文 GB2312')
windowFont.ButtonControl(Name='确定').Click()

edit=notepad.EditControl(AutomationId='15')
edit.SendKeys("测试文本\n测试文本换行")
print(edit.GetValuePattern().Value)
notepad.ButtonControl(Name='关闭').Click()
notepad.ButtonControl(Name='不保存(N)').Click()
'''
#"{'symbol':'kaka'}"
#print(sys.argv[1])
#obj = ast.literal_eval(sys.argv[1])
#print(obj["symbol"])
#print(isinstance (sys.argv[1],str))
#obj = sys.argv[1]
def func(variables):
    #subprocess.Popen(r'C:\Users\DELL\AppData\Local\Programs\Microsoft VS Code\Code.exe')
    #vs=uiautomation.PaneControl(searchDepth=1,RegexName='.* - Visual Studio Code')
    #uiautomation.SetGlobalSearchTimeout(600)
    #uiautomation.EnumAndLogControl(vs, 7)
    Logger.SetLogFile(os.path.abspath(".")+"\\"+time.strftime("%Y%m%d", time.localtime())+'.txt')
    #Logger.Write("abcde")
    flag = True
    while flag:
        time.sleep(3)
        obj = uiautomation.ControlFromCursor()
        rect = obj.BoundingRectangle
        print(variables['a'])
        #rect.left, rect.top, rect.right, rect.bottom, rect.width(), rect.height(), rect.xcenter(), rect.ycenter()
        #print(rect.width())
        #print(rect.height())
        #Bitmap().Clear(color=0x7fdd2378,x=rect.left, y=rect.top, width=rect.width(), height=rect.height())
        #uiautomation.MessageBox("获取元素完成", "提示")
        uiautomation.LogControl(obj)
        stript = input("是否继续[0,1]:")
        flag = bool(int(stript))


func({"a":"test"})