PC端自動化測試(一)

PC端自動化測試(一)

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

A set of Python modules to automate the Microsoft Windows GUI

安裝

  • python3環境(python3.5以上)

  • pip環境

$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pywinauto

應用程序的可訪問技術

支持控件的訪問技術

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

打開應用程序

from pywinauto.application import Application


# 打開指定的應用程序


# 1. 打開windows自帶的應用
app = Application(backend='uia').start("notepad.exe")


# 2. 打開任意一個應用程序
app = Application(backend='uia').start("exe路徑")

連接已經打開的應用程序

  • 通過進程號

  • 通過窗口句柄

app = Application('uia').connect(process=4444)
app = Application('uia').connect(handle=1904040)

選擇指定的窗口

# 方式一:app[類名/標題] :推薦使用該方式


# 1.通過窗口類型來選擇
dlg = app["TNavicatMainForm"]


# 2.通過窗口標題來選擇
dlg = app["Navicat for MySQL"]




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


dlg.print_control_identifiers()

操作窗口

# 窗口最大化
dlg.maximize()


# 窗口最小化
dlg.minimize()


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


# 查找窗口顯示狀態,最大化爲1,正常爲0
status = dlg.get_show_state()


# 獲取當前窗口的座標位置
rect = dlg.rectangle()
print(rect)# (L548,T194,R1768,B1043)


# 關閉窗口
dlg.close()

選擇控件

控件:窗口上的內容

# 選擇控件


# 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章