pywinauto处理UI自动化
pywinauto处理UI自动化
Python的pywinauto模块来处理Windows控件的UI操作。
1. 首先搭建pywinauto环境。
1). 可以参考vncdotool - A command line VNC client 安装python2.7.5, Twisted 和PIL。
2). 安装elementtree(elementtree-1.2.7-20070827-preview.win32.exe) 和ctypes(ctypes-1.0.2.win32-py2.5-AMD64.msi)。
3). 安装已经下载好的pywinauto(pywinauto-0.4.2)
> cd to xxx:\pywinauto-0.4.2
> python setup.py install
目前最新的版本已经是0.5.1了。 可以参考:https://pypi.python.org/pypi/pywinauto
4). 测试下是否安装成功
> from pywinauto import application
> app = application.Application().start_("notepad")
> app.notepad.TypeKeys("%FX")
这段代码可以打开notepad‘并将notepad通过使用用快捷键关闭。
2. 首先还需要一个工具: Swapy-ob 来获取控件属性
Swapy-ob 是一个可以获得控件原生的 Python 代码的工具。
3. 一些控件的基本操作
TreeView
Click()
Select()
ListView
Check()
UnCheck()
Select()
Note: ListView sometimes supports not good, especially do the action from lead to test machine.
Button
Click()
PressButton()
Edit
SetEditText()
SetText()
Window
Wait()
WaitNot()
Listbox / Combobox
Select()
Checkbox / Radio button
Check()
UnCheck()
MenuItem
MenuItem.Select()
MenuItem.Click()
Others
Close()
DoubleClick()
For more info: http://pywinauto.googlecode.com/hg/pywinauto/docs/controls_overview.html#all-controls
4.关联一个instance
start_() – The application is not running and need to start it
connect_() – The application is already running.
For example:
app = Application()
app.start_(“c:\windows\system32\notepad.exe”)
app.connect_(title_re = ".*Notepad", class_name ="Notepad")
5. 调用一个窗口
a_window = app.window_(title = "Page Setup", class_name = "#32770")
也可以用handle方式来处理:
w_handle = pywinauto.findwindows.find_windows(title=u‘Active Directory Users and Computers‘, class_name=‘MMCMainFrame‘)[0]
aduc_window = pwa_app.window_(handle=w_handle)
aduc_window.print_control_identifiers() //打印当前窗口的所有控件和属性。
6. TypeKeys()快捷键
SHIFT +
CTRL ^
ALT %
空格键是SPACE
BACKSPACE {BACKSPACE}、{BS} or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER} or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
+ {ADD}
- {SUBTRACT}
* {MULTIPLY}
/ {DIVIDE}
7. Example for create AD user
pwa_aduc.window_(title=u‘New Object - User‘, class_name=‘#32770‘).Wait(‘exists‘, timeout=30, retry_interval=1)
u_window = pwa_aduc.window_(title=u‘New Object - User‘, class_name=‘#32770‘)
u_window[‘First name:Edit‘].Wait(‘enable‘, timeout=30, retry_interval=1)
u_window[‘First name:Edit‘].TypeKeys(User)
u_window[‘User logon name:Edit0‘].TypeKeys(User)
u_window[‘Next‘].Wait(‘enable‘, timeout=30, retry_interval=1)
u_window[‘Next‘].Click(button=‘left‘, double=False)
u_window[‘Password:Edit‘].Wait(‘exists‘, timeout=30, retry_interval=1)
u_window[‘Password:Edit‘].TypeKeys(Password)
u_window[‘Confirm password:Edit‘].TypeKeys(Password)
u_window[‘User must change password at next logon‘].UnCheck()
u_window[‘Next‘].Wait(‘enable‘, timeout=30, retry_interval=1)
u_window[‘Next‘].Click(button=‘left‘, double=False)
u_window[‘Finish‘].Click(button=‘left‘, double=False)
8. 当然因为Selenium 支持 Python, 所以可以用python+pywinauto+selenium组合的方式实现Windows和Web UI的处理。
Helpful link:
http://pywinauto.github.io/
http://pywinauto.googlecode.com/hg/pywinauto/docs/contents.html
https://code.google.com/p/swapy/
pywinauto处理UI自动化