Http://www.cnblogs.com/gannan/archive/2013/01/08/2851825.html
Today to organize their own blog, found that it has not been updated for almost a year, Khan.
Today is Pywinauto, it is written in Python language open-source testing tools, designed to test the Win32 application, its official website is: http://pypi.python.org/pypi/pywinauto/0.4.0
I used to do Win32 application testing, with a lot of people familiar with the tool, before touching the pywinauto, not much understanding of it, however, with the increase in understanding of it, the use of Python dynamic object capabilities, so that the code even in the book, it is easy to read, and the function is very powerful, The most important thing is that it operates in a more elegant way on Win32 forms, controls, and no longer uses previous recordings-the way you modify code.
For example, the following code is tested on the English operating system:
?
12345 |
from pywinauto import application app = application. Application.start ( "notepad.exe" Code class= "Python plain" >app. Notepad.menuselect ( app. AboutNotepad.OK.Click () app. Notepad.menuselect ( "File->exit" |
In the example above, there are only 5 lines of code that can perform the actions in the comments. Its biggest highlight comes from the third line, its variable notepad is not defined and declared, it can be used, again, the fourth line of Aboutnotepad and the OK behind it is called.
If you run the above code under the English operating system, you can clearly see the whole process of software work.
See here, people will ask, the above mentioned variables are how to come? Why can it be called?
This is the Pywinauto feature, it first through the app to get the handle of an application, and then use "fuzzy matching" way to find the corresponding window and control, in the above example, because the title of the WordPad is Untitled–notepad, so you can use part of the title, That is Notepad to visit it, similarly, with untitled can also.
At the same time, in the "about" window of WordPad, the title of the Close button is "OK", so you can call it with OK.
Speaking so much, now specifically describes the installation and use of Pywinauto
1 installation
1.1 Installing Python
The installation of Pywinauto has two requirements:
The 1:python version must be no higher than 2.6
2: Using Python's 32-bit version
Therefore, it is recommended that you go to the Python website to download its 32-bit python2.6 and install it.
1.2 Installing dependent libraries
Pywinauto has two dependent libraries, i.e.
Sendkeys-ctypes
SendKeys
It can be downloaded, installed, or downloaded from the attachment in this article, and will be added later.
Description, the above two libraries below Pywinauto all have setup.py program, the user can run the Python setup.py install under DOS command, install, these libraries will appear in Python's installation directory after successful installation lib\ Under the site-packages.
1.3 Installing Pywinauto
As long as Python meets the installation requirements (32 bits earlier, less than 2.7), and the installation depends on the library, the installation of Pywinauto is very easy and is skipped here.
2 Python's development environment (optional)
I use the Eclipse+pydev plugin for Python development.
The specific configuration method is
1: Download Pydev
2: Unzip, the extracted two folders features, plugins copy to eclipse Dropins directory
3: Launch Eclipse, click Menu Windows->preferences, the left side of the popup window should appear pydev node
4: Click Interpreter-python under Pydev, click the New button, find the Python.exe installation path, and then click the OK button two times.
5: Now you can create a new Python project, module, you can develop and debug python in Eclipse.
3 Pywinauto Use
3.1 relate to an application in the following ways:
?
start_(path) connect_(handle or processID) |
Here is the sample code
App = Application () App.start_ ("notepad.exe") app.connect_ (Path = r "C:\windows\system32\notepad.exe") app.connect_ ( Process = 2341) app.connect_ (handle = 0x010f0c)
3.2 Calling a form
When the app is initialized, a form in the app can be manipulated in a fuzzy match, and the variable name can be part or all of the form's title
Dlg = App.window_ (Title_re = ". *part of title.*") Dlg = App. Partoftitle
For example, for a window in a WordPad app, in the English operating system, the title is "Untitled-notepad"
You can call the form in the following two ways
App. Untitledapp.notepad
For the About window, its title is "About Notepad"
The form can be called with the following name
?
3.3 Calling a control
Pywinauto Use the following order to locate a control
1: Caption of the control, that is, title
2: The class name of the control, that is, friendly class
3: Title plus class name for the control, i.e. title + friendly class
For the on button in the About window, you can call it in any of the following ways
?
app.AboutNotepad.OK app.AboutNotepad.Button1 app.AboutNotepad.OKButton |
If you enter text in WordPad, the query window pops up when you exit, and for the "Do not save" button, the English title is ' t ' save, according to the rule above, you can call the button using any of the following methods
?
app.Notepad.DontSave app.Notepad.DoSave app.Notepad.DotSave |
Both Dontsave, Dosave, and Dotsave are part of the title Dont ' save, and Pywinauto can find the button in a fuzzy matching way.
3.4 Basic operations for controls
As follows:
?
Control.Click() #点击 Control.MenuSelect() #菜单选择 Control.PressMouse / MoveMouse / ReleaseMouse() #按、移动、释放鼠标 Control.TypeKeys() #键入字符 |
where Typekeys () supports key combinations, such as
Ctrl: ^
ALT:%
Shift: +
The following is a read of its common properties:
?
ControlID() FriendlyClassName() WindowText() Rectangle() IsEnabled() IsVisible() |
If you want to know more, you can view the official documentation for Pywinauto, whose link is
Http://pywinauto.googlecode.com/hg/pywinauto/docs/controls_overview.html
At the top of the page is the basic operation and property reading of all the controls, followed by the actions unique to each control, which, in actual programming, can be queried by a document if you want to know an action for a control.
4 other Instructions
4.1 and Pyunit Integration
As long as the code is written to meet the requirements of pyunit, it is easy to integrate the Win32 application and PYUNIT tested by Pywinauto.
4.2 and Selenium Integrated
Selenium supports the Python language, so it's easy to get pywinauto and selenium.
In some test scenarios, when you run out of a Win32 app, you'll see the results in the Web application right away-that is, a test that faces both Win32 and web two applications can be implemented in a python+pywinauto+selenium combination.
4.3 Code structure
Interested netizens, can also read its source code, the main several programs are
?
Application.py:与app相关 Findwindows.py:窗体的查找 Timings.py:各种操作的时间间隔设置 win32_controls.py:普通控件的访问 common_controls.py:TreeView / ListView...等控件的访问 Menuwrapper.py:菜单的访问 |
Go to Pywinauto for Win32 application testing