PC端自动化测试(一)

PC端自动化测试(一)

pywinauto:同时支持控件操作和图像操作,支持Win32 API和MS UI Automation API

A set of Python modules to automate the Microsoft Windows GUI

安装

  • python3环境(python3.5以上)
  • pip环境
1
$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pywinauto

pip安装

应用程序的可访问技术

支持控件的访问技术

Win32 API(backend=“win32”) 默认的backend

MFC,VB6,VCL简单的WinForms控件和大多数旧的应用程序

MS UI Automation API(backend=“uia”)

WinForms,WPF,Store apps,Qt5,浏览器

进程数量

单进程

Application作用范围是一个进程

跨进程

Desktop作用范围可以跨进程

GUI对象检查工具

Inspect.exe

spy++.exe

ViewWizard

打开应用程序

1
2
3
4
5
6
7
8
9
from pywinauto.application import Application

# 打开指定的应用程序

# 1. 打开windows自带的应用
app = Application(backend='uia').start("notepad.exe")

# 2. 打开任意一个应用程序
app = Application(backend='uia').start("exe路径")

连接已经打开的应用程序

  • 通过进程号
  • 通过窗口句柄
1
2
app = Application('uia').connect(process=4444)
app = Application('uia').connect(handle=1904040)

ViewWizard

选择指定的窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
# 方式一:app[类名/标题] :推荐使用该方式

# 1.通过窗口类型来选择
dlg = app["TNavicatMainForm"]

# 2.通过窗口标题来选择
dlg = app["Navicat for MySQL"]


# 方式二:app.类名
dlg =app.TNavicatMainForm

dlg.print_control_identifiers()

操作窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 窗口最大化
dlg.maximize()

# 窗口最小化
dlg.minimize()

# 窗口恢复正常大小
dlg.restore()

# 查找窗口显示状态,最大化为1,正常为0
status = dlg.get_show_state()

# 获取当前窗口的坐标位置
rect = dlg.rectangle()
print(rect)# (L548,T194,R1768,B1043)

# 关闭窗口
dlg.close()

选择控件

控件:窗口上的内容

1
2
3
4
5
6
7
8
9
# 选择控件

# menu = dlg.Menu
menu = dlg["Menu"]
print(menu.print_control_identifiers()) # 查看Menu上的控件


file = menu.child_window(title="文件", control_type="MenuItem")
file.print_control_identifiers()

控件的分类

  • 状态栏 StatusBar
  • 按钮 Button
  • 单选框 RadioButton
  • 组合框 ComboBox
  • 编辑栏 Edit
  • 列表框 ListBox
  • 弹出菜单 PopupMenu
  • 工具栏 Toolbar
  • 树状视图 Tree View
  • 菜单项 MenuItem
  • 静态内容 Static
  • 复选框 CheckBox
  • 组框 GroupBox
  • 对话框 Dialog
  • 头部内容 Header
  • 列表显示控件 ListView
  • 选项卡控件 TabControl
  • 工具提示 ToolTips
  • 菜单 Menu
  • 窗格 Pane
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!