Written in Python under Windows automated testing software

https://www.jianshu.com/p/be3c46c7a905

uiautomation module study notes


Some time ago, because of personal needs, the Internet to find some information on the Windows platform, automated testing, and finally found a pretty good Python module: uiautomation, direct pip install uiautomation can easily access this module. While studying this module, write some notes to facilitate their access to the future, but also facilitate everyone to read.

0x00 Overview

This library is open source on Github, address: https://github.com/yinkaisheng/Python-UIAutomation-for-Windows
and the author is a Chinese, or Nanjing, makes me feel very warm.
This library can be said to be very fit the actual, in the Windows platform, the vast majority of Windows software must follow the norms, that window ah, handle, ah, ah controls these things are common, Windows provides out these interfaces. Thanks in advance about yinkaisheng, his job is to further integrate these interfaces do, Python developers to provide the interface. This greatly facilitates the people who want to develop in Python automated test procedures.

0x01 author's readme file

  • This module is a Windows-based technology UIA, the minimum required system WindowsXP SP3, support MFC, WinForm, WPF, Metro UI (from the beginning of the Windows system Windows8 developed a new window), Qt and Firefox, that is based on. Net technology all Windows Forms applications support, and other software, such as DirectUI based development, this module may be difficult to obtain the information.
  • The authors of this open source module, Apache2.0 based on this that the source code can be freely copied, modified, re-use and publish, praise!
  • Installation is simple, and Python run on existing computers pypi environment pip install uiautomationcan be installed, and will be automatically copied to the main program automation.py Python's Scripts folder.
  • The author also provides a C ++ dll source code, but do not go here involved. It can be seen is the name of the developer of the very dedicated!
  • You can simply run the automation.py, the effect is run directly after 3 seconds to traverse all the controls currently active window. The effect of running QQ:


     
     

     
     

    Visible effect is very obvious, controls all information in the window displayed, including the location (in pixels), handles, control tree depth control type, name of the control, control. At the same time, all the information will be saved to the current @ AutomationLog.txt file in the directory for easy access.

  • The author provides a Demo, to control the Notepad program:
import subprocess
import uiautomation as automation

print(automation.GetRootControl())
subprocess.Popen('notepad.exe')
notepadWindow = automation.WindowControl(searchDepth = 1, ClassName = 'Notepad') print(notepadWindow.Name) notepadWindow.SetTopmost(True) edit = notepadWindow.EditControl() edit.SetValue('Hello') edit.SendKeys('{Ctrl}{End}{Enter}World') 

GetRootControl () method returns the root of control, that is, the entire Windows desktop visualization (so understanding right bar)
WindowControl (searchDepth = 1, ClassName = 'Notepad') method creates a WindowsControl objects, the role is to refine the parameters of how to find we want to control, the available parameters searchFromControl = None, searchDepth = 0xFFFFFFF, searchWaitTime = sEARCH_INTERVAL, foundIndex = 1, Name, SubName, ClassName, Depth and so on, initialization method of the control class can go to the source code under the init look at these parameters how to use.

  • Next, the author describes a small tool Inspect.exe, this tool is provided by Microsoft, can be used to detect the contents of the UI, this program automatically integrated in the Microsoft Windows SDK, I just installed on the computer Visual Studio and Windows SDK, You should be able to find this program. Sure enough focus to find this program in the tool:


     
     

     
     

    We can see the effect is very good, clear structure of the window!

  • 最后作者放了一些自己使用该模块的截图,可以看到,功能十分强大:


     

     

     

     

     

0x02 简单了解实现原理

作者在readme文档中外链了一篇博文,简单介绍了实现原理,为了能更好地掌握这个库的使用,这篇原理我也简单的学习一下。(原文链接

在最早的Windows开发中是没有自动化测试的概念的。1997年微软在操作系统中集成了MASS(Microsoft Active Accessibility)组件,但是微软开发MASS组件的初衷并不是为了自动化测试,而是提供了一套接口,让开发者们可以方便的开发残疾人士辅助软件,比如读屏软件等。伴随着自动化测试的应用越来越广泛,微软正视了自动化测试的需求,在MASS的基础上,对其重新封装设计并实现了UIAutomation的类库(.NET)。从Win7系统开始的后续Windows操作系统都整合了Windows Automation API的所有功能。作者在阅读了MSDN上的《UI Automation Client Programmer's Guide》和CodeMagazine上的《Creating UI Automation Client Applications》两篇文章后,用Python和C++对UIAutomation做了一层简单的封装,方便了想要用Python开发自动化测试应用而对.Net平台又不太熟悉的人,比如我。

0x03 API学习摘要

之前也已经提到,这个模块是作者对UIA用C++和Python简单的做了一层封装,只要能理解面向对象编程,学习难度也不是很大。正好,我在作者的CSDN博客里面找到了作者使用此模块的实例(原文链接),就以此为学习的入口,代码如下:

#!python3
# -*- coding: utf-8 -*-
"""
本脚本可以获取QQ2017(v8.9.4)群所有成员详细资料,请根据提示做对应的操作
作者:[email protected]
"""
import time
import uiautomation as automation def GetPersonDetail(): detailWindow = automation.WindowControl(searchDepth= 1, ClassName = 'TXGuiFoundation', SubName = '的资料') details = '' for control, depth in automation.WalkControl(detailWindow): if isinstance(control, automation.EditControl): details += control.Name + control.CurrentValue() + '\n' details += '\n' * 2 detailWindow.Click(-10, 10) return details def main(): automation.Logger.WriteLine('请把鼠标放在QQ群聊天窗口中的一个成员上面,3秒后获取\n') time.sleep(3) listItem = automation.ControlFromCursor() if listItem.ControlType != automation.ControlType.ListItemControl: automation.Logger.WriteLine('没有放在群成员上面,程序退出!') return consoleWindow = automation.GetConsoleWindow() if consoleWindow: consoleWindow.SetActive() qqWindow = listItem.GetTopWindow() list = listItem.GetParentControl() allListItems = list.GetChildren() for listItem in allListItems: automation.Logger.WriteLine(listItem.Name) answer = input('是否获取详细信息?按y和Enter继续\n') if answer.lower() == 'y': automation.Logger.WriteLine('\n3秒后开始获取QQ群成员详细资料,您可以一直按住F10键暂停脚本') time.sleep(3) qqWindow.SetActive() #确保群里第一个成员可见在最上面 left, top, right, bottom = list.BoundingRectangle while allListItems[0].BoundingRectangle[1] < top: automation.Win32API.MouseClick(right - 5, top + 20) for listItem in allListItems: if listItem.ControlType == automation.ControlType.ListItemControl: if automation.Win32API.IsKeyPressed(automation.Keys.VK_F10): if consoleWindow: consoleWindow.SetActive() input('\n您暂停了脚本,按Enter继续\n') qqWindow.SetActive() listItem.RightClick() menu = automation.MenuControl(searchDepth= 1, ClassName = 'TXGuiFoundation') menuItems = menu.GetChildren() for menuItem in menuItems: if menuItem.Name == '查看资料': menuItem.Click() break automation.Logger.WriteLine(listItem.Name, automation.ConsoleColor.Green) automation.Logger.WriteLine(GetPersonDetail()) listItem.Click() automation.SendKeys('{Down}') if __name__ == '__main__': main() input('press Enter to exit') 

我用的环境是Pycharm2017.2社区版和Python3.6,这个模块的内容主要集中在uiautomation.py文件中,学习方法就是去这个文件里看相关的代码。先简单看一下这个代码,看看里面有哪些看不懂的,看不懂的地方就是要学习的地方。

首先是automation.WindowControl,这看起来是个对象,但是还不知道这个对象有哪些方法和属性,下面的WalkControl和EditControl应该也是对象,从命名上看可能直接代表了窗体中的不同控件。control.CurrentValue应该是属性,下面的detailWindow.Click()应该是模拟鼠标点击的方法。再看main()函数,Logger.WriteLine()方法应该是跟日志有关,ControlFromCursor()可能是从鼠标获取控件的方法......

这样看一遍大概就知道应该去源码里面找哪些定义了,我们利用PyCharm的跳转定义功能可以很容易地找到定义这些类和方法的代码,都在uiautomation.py这个文件中。源码就不贴了,通过跳转功能可以很容易地理解其中的方法与类之间的逻辑关系。这个文件中主要定义了“控件”这个类,就是两千多行处的Class Control(.............):这一大段代码,里面包含了所有对控件的方法和基本的属性。方法有获取控件名称啊,内容啊等,属性有位置啊,是否Active啊这些。然后由Control类派生出各个子类,代表具体的各种控件,比如WindowControl类表示窗口,EditControl类表示输入框,ButtonControl类代表按钮等等,几乎涵盖所有Win窗体程序的所有控件。并且作者在很多类下面做了详细的注释,阅读起来应该没有什么困难。理解的难点应该是这里面有大量关于WinForm开发的知识,需要一点基础。

So when we define these methods and classes to figure out, we can understand the function of this instance. By using a combination of various spatial properties and methods to achieve the information obtained from the group QQ QQ group window. Including information group number, group membership information, chat records, and saved to a text file.

0x04 Postscript

  • Sure enough, look, write more fundamental way is to enhance the strength of the materials look a hundred times better to find their own way again!
  • Life is short,use Python!


Author: 8f7aac77586a
link: https: //www.jianshu.com/p/be3c46c7a905
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/dhcn/p/11128251.html