Testing the Win32 application with PYWINAUTO

Source: Internet
Author: User

Before doing Win32 application testing, used 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 orprocessID)

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

app.AboutNotepad

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.OKapp.AboutNotepad.Button1app.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.DoSaveapp.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:菜单的访问

Turn from:

Http://www.cnblogs.com/gannan/archive/2013/01/08/2851825.html

Testing the Win32 application with PYWINAUTO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.