On Python uiautomation, testing win10 calculator

Gong Jianbo 2022-01-15 03:56:36 阅读数:209

python uiautomation testing win10 win

Look for Python Client automation test library , Generally speaking, there are  pywinauto、pywin32、pyautogui、uiautomation. This article is only for uiautomation Do a simple understanding .

MS UI Automation yes MSAA An alternative to technology : That is to make controls and applications more accessible (accessible).MS UIA Two... Are clearly defined role:UIA Provider That is, the software itself , It is mainly that the software developers realize the relevant functions according to the corresponding mode interface, UIA Client That is, automated scripts and related assistive technology applications, From the tester's point of view , It mainly calls the corresponding API To implement automated test scripts .

and uiautomation According to the Microsoft manual , use Python and C++ Yes UI Automation Made a layer of encapsulation .

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

The author blog : Open source for your own use python Packaged one Windows GUI(UI Automation) Automation tools , Support MFC,Windows Forms,WPF,Metro,Qt - YinKaisheng - Blog Garden

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

Reference blog :Uiautomation stay Windows WPF and Qt Application on products _yaoliuwei1426 The blog of -CSDN Blog

Reference blog :MS UI Automation brief introduction _ffeiffei The column -CSDN Blog _uiautomation

According to the example provided by the author , I am right. Win10 The calculator did a simple calculation test , The code is as follows :

import subprocess
import uiautomation as uia
# Set global search timeout 15s
uia.uiautomation.SetGlobalSearchTimeout(15)
# test Win10 Calculator
def test_calc():
# Start the calculator process
subprocess.Popen(args='calc.exe')
# First, find the window of the program from the first layer of child controls on the desktop , Then find the child controls from this window
window = uia.WindowControl(
searchDepth=1, ClassName='ApplicationFrameWindow', Name=' Calculator ')
# Window top
window.SetTopmost(True)
# According to the document description , It is inefficient to find space directly from the top floor , Because every time you find it, you need to traverse
# So here can be based on inspect View the object tree to find
# Name/ClassName/AutomationId All are UIAutomation attribute , You can view... Through the tool , A selector similar to a web page
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=' The keypad ')
calc_group = button_group.Control(searchDepth=1, Name=' Standard operator ')
# Analog buttons
window.SendKeys('1')
#number_group.ButtonControl(Name=' One ').Click()
calc_group.ButtonControl(Name=' Add ').Click()
number_group.ButtonControl(Name=' Four ').Click()
calc_group.ButtonControl(Name=' be equal to ').Click()
result = button_group.Control(AutomationId='NormalOutput').Name
print(" test result :", result, ' bytes:', bytes(result, encoding='utf8').hex())
# There are invisible characters before and after the obtained text , Replace it here first
result = result.replace(b'\xe2\x80\xad'.decode('utf8'), '').replace(
b'\xe2\x80\xac'.decode('utf8'), '')
print(" End of test , expect =5, actual ={0},{1}".format(
result, " The test passed " if result == "5" else " Test to fail "))
# Get by copying and pasting , The resulting text has no other characters
window.SendKeys('{Ctrl}c', waitTime=1)
result = uia.GetClipboardText()
print(" test result :", result, ' bytes:', bytes(result, encoding='utf8').hex())
# Take a screenshot and save it to the specified path
window.CaptureToImage('C:/Users/1992/Pictures/1.png')
# Shut down the program
#window.ButtonControl(Name=' close ').Click()
window.GetWindowPattern().Close()
if __name__ == '__main__':
test_calc()

test result :

 

 

版权声明:本文为[Gong Jianbo]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2021/12/202112122340289555.html