PC端自动化测试(三)

PC端自动化测试(三)

编辑类控件的基本操作

通过记事本来进行测试

1
2
3
4
5
6
import pywinauto

app = pywinauto.Application().start("notepad.exe")
# 选择主窗口
dlg = app["无标题 - 记事本"]
dlg.print_control_identifiers()

输入内容

选择编辑框并输入Hello World!

1
dlg["Edit"].type_keys("Hello World!")

替换

  1. 通过菜单选择替换
  2. 选择替换窗口app["替换"]
  3. 选择查找编辑框并输入World
  4. 选择替换为编辑框并输入Python
  5. 点击全部替换按钮
1
2
3
4
5
dlg.menu_select("编辑->替换(R)")
app["替换"].print_control_identifiers()
app["替换"]["Edit1"].type_keys("World")
app["替换"]["Edit2"].type_keys("Python")
app["替换"]["Button3"].click()

键盘操作

键盘操作模块:pywinauto.keyboard

send_keys方法

  • 按F5:send_key("{VK_F5}")
  • 按F5:send_key("{F5}")
  • 按回车:send_key("{VK_RETURN}")
  • 按字母A:send_key('A')

常用按键

  • ESC:VK_ESCAPE
  • TAB:VK_TAB
  • Shift:VK_SHIFT
  • Ctrl:VK_CONTROL
  • Alt:VK_MENU
  • 空格:VK_SPACK
  • 退格:VK_BACK
  • 左win:VK_LWIN
  • 右win:VK_RWIN

键盘修饰符

  • +:按Shift
  • ^:按Ctrl
  • %:按Alt

例如^s相当于输入ctrl+s

鼠标操作

鼠标操作模块:pywinauto.mouse

  • click:点击
  • double_click:双击
  • right_clik:右击
  • wheel_click:中间点击
  • press:按下
  • repleace:释放
  • move:移动
  • scrool:滚动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from pywinauto import mouse

# 鼠标单击(默认左键)
mouse.click(coords=(580,52))

# 鼠标右键
mouse.right_click(coords=(1000,500))

# 鼠标双击
mouse.double_click(button="left",coords=(155,50))

# 点击鼠标中键
mouse.wheel_click(coords=(1000,500))

# 按下鼠标
mouse.press(coords=(72,31))
# 释放鼠标
mouse.release(coords=(1000,500))

# 滑动鼠标滚轮
mouse.scroll(coords=(1000, 500), wheel_dist=1)

# 移动鼠标位置
mouse.move(coords=(0, 0))

for i in range(0, 1000, 50):
mouse.move(coords=(i, i))

# 部分无法直接点击的控件:获取控件中心点位置,双击点击
rect = db_name.rectangle().mid_point()
mouse.double_click(coords=(rect.x, rect.y))

查看系统提示

在时钟附近有表示正在运行的应用程序的图标,也就是「系统托盘」,也被称为「通知区域」。

可以通过启动Explorer.exe应用程序,在任务栏用户提示通知区域找到工具栏控件

1
2
3
4
from pywinauto import Application

app = Application("uia").connect(path="explorer")
icons = app['任务栏']['用户提示通知区域']

结合Selenium实现文件上传

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import pywinauto
from pywinauto.keyboard import send_keys
from selenium import webdriver

url= "https://www.layui.com/demo/upload.html"
browser = webdriver.Chrome()
browser.get(url=url)
browser.find_element_by_xpath("//button[@id='test1']").click()

# 使用pywinauto来选择文件
app = pywinauto.Desktop()
# 选择文件上传的窗口
dlg = app["打开"]
dlg.print_control_identifiers()

# 选择文件地址输入框
dlg["Toolbar3"].click()
send_keys("C:\images")
send_keys("{VK_RETURN}")

# 选中文件名输入框
dlg["文件名(&N):Edit"].type_keys("9.png")

# 点击打开
dlg["打开(&O)"].click()

连接Navicat并操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import pywinauto
from pywinauto import mouse
from pywinauto.keyboard import send_keys


class NavicatTest:

def __init__(self, path=None, precess=None):
# 初始化一个应用程序对象
if path:
self.app = pywinauto.Application(backend="uia").start(path)
else:
self.app = pywinauto.Application(backend="uia").connect(process=precess)
# 选择主窗口
self.dlg = self.app["Navicat for MySQL"]

def new_connect(self, title, host, port, user, password):
"""
新建连接
"""
# 选择菜单栏
menu = self.dlg["Menu"]
# 点击文件
menu.child_window(title="文件", control_type="MenuItem").click_input()
# 点击新建连接
menu.item_by_path("文件->新建连接...").click_input()
# 选择新建连接窗口
new_dlg = self.app["新建连接"]
# 连接名称输入框
new_dlg["常规"].Edit5.type_keys(title)
# ip输入框
new_dlg["常规"].Edit1.type_keys(host)
# 端口输入框
new_dlg["常规"].Edit4.type_keys(port)
# 用户名输入框
new_dlg["常规"].Edit3.type_keys(user)
# 密码输入框
new_dlg["常规"].Edit2.type_keys(password)
# 点击确定按钮
new_dlg["确定"].click()

def open_connect(self, title):
"""
打开连接或者数据库
:param title: 连接名或者数据库名
:return:
"""
# 获取控制坐标中心点,进行点击,打开连接
db_name = self.dlg["TTreeView"].child_window(title=title, control_type="TreeItem")
rect = db_name.rectangle().mid_point()
print(rect.x, rect.y)
mouse.double_click(coords=(rect.x, rect.y))

def del_connect(self, title):
"""删除连接"""
# 获取控件中心点位置
db_name = self.dlg["TTreeView"].child_window(title=title, control_type="TreeItem")
rect = db_name.rectangle().mid_point()
# 鼠标在控件中心点,右击
mouse.right_click(coords=(rect.x, rect.y))
# 删除连接
self.app["上下文"]["MenuItem5"].click_input()
# 选择删除窗口
self.app["确认删除"]["删除"].click()

def close_coonnect(self, title):
"""
关闭连接或者数据库
:param title: 连接名或者数据库名
:return:
"""
# 获取控件中心点位置
db_name = self.dlg["TTreeView"].child_window(title=title, control_type="TreeItem")
rect = db_name.rectangle().mid_point()
# 鼠标在控件中心点,右击
mouse.right_click(coords=(rect.x, rect.y))
# 删除连接
self.app["上下文"]["MenuItem2"].click_input()

def del_database(self, database):
db_name = self.dlg["TTreeView"].child_window(title=database, control_type="TreeItem")
rect = db_name.rectangle().mid_point()
mouse.right_click(coords=(rect.x, rect.y))
self.app["上下文"]["MenuItem4"].click_input()
# 选择删除窗口
self.app["确认删除"]["删除"].click()

def new_database(self, title, database, charset="utf8 -- UTF-8 Unicode"):
"""新建数据库"""
db_name = self.dlg["TTreeView"].child_window(title=title, control_type="TreeItem")
rect = db_name.rectangle().mid_point()
mouse.right_click(coords=(rect.x, rect.y))

dlg = self.app["上下文"]
# 点击新建连接
dlg.MenuItem8.click_input()
# 获取新建连接的窗口
# self.app["新建数据库"].print_control_identifiers()
# 数据数据库名
self.app["新建数据库"]["常规"].Edit3.type_keys(database)
self.app["新建数据库"]["常规"].Edit2.type_keys(charset)
# 键盘回车键,点击确定
send_keys("{VK_RETURN}")

def new_find_dlg(self):
"""新建查询窗口"""
find = self.dlg["TTreeView"].child_window(title="查询", control_type="TreeItem")
# 获取查询的左边
rect = find.rectangle().mid_point()
# 鼠标右击
mouse.right_click(coords=(rect.x, rect.y))
# 选择出现的小窗口
dlg = self.app["上下文"]
# 点击新建连接
dlg.MenuItem1.click_input()

def find_sql(self, database, title, sql):
"""查询sql"""
title = "无标题 @{} ({}) - 查询".format(database, title)
find_dlg = self.app[title]
find_dlg["TabControl"].print_control_identifiers()
# 获取编辑框
edit = find_dlg["TabControl"].Pane4
# 获取编辑窗口控件位置
rect = edit.rectangle().mid_point()
mouse.click(coords=(rect.x, rect.y))
sql = "^a{}".format(sql.replace(" ", "{VK_SPACE}")) + ";"
send_keys(sql)
# 使用运行快捷键
send_keys("^r")


if __name__ == '__main__':
nav = NavicatTest(precess=1320)

# 新建连接
nav.new_connect(title="python666", host="localhost", port=3306, user="root", password="mysql")

# 打开连接
nav.open_connect("win")

# 关闭连接
nav.close_coonnect("test")

# 打开数据库
nav.open_connect("test")

# 新建查询窗口
nav.new_find_dlg()

# 查询数据
sql = "SELECT * FROM students"
nav.find_sql("test", "win", sql)

# # 删除连接
nav.del_connect("localhost_3306")
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!