聊聊 PC 端自動化最佳方案 - WinAppDriver

1. 前言

大家好,我是安果!

一提到自動化,可能大家想到的是 App 端的 Appium、Airtest、AutoJS,亦或是 Selenium、Puppeteer、Cypress 等 Web 端的自動化框架

本篇文章,我將和大家聊聊 PC 端的自動化工具 - WinAppDriver

​2. 準備

WinAppDriver,全稱爲 Windows Application Driver,它是 Windows 上一個類似 Selenium 的 UI 自動化驅動服務框架

它支持 Appium,可以使用 Appium-Python-Client 依賴庫完成對 Windows 桌面程序的自動化操作

項目地址:https://github.com/Microsoft/WinAppDriver

需要注意的是,要使用 WinAppDriver 服務框架完成 Windows 的自動化,需要滿足 Windows10 或 Windows Server 2016 以上系統

另外,它支持的應用程序包含:

  • UWP  -  Universal Windows Platform

  • WinForms  -  Windows Forms

  • WPF  -  Windows Presentation Foundation

  • Win32  -  Classic Windows

在實現之前,我們需要做好以下準備工作

2-1  開啓「 開發者模式 」

關鍵字搜索「 開發者設置 」,選擇開啓「 開發者模式 」

image

2-2  安裝窗口組件元素識別工具

常用的 2 種窗口元素識別工具爲:inspect.exe、FlaUInspect

其中

作爲官方的組件元素識別工具,inspect.exe 集成於 Windows SDK

如果本地不存在該文件,可以通過下面鏈接進行安裝

https://download.microsoft.com/download/4/d/2/4d2b7011-606a-467e-99b4-99550bf24ffc/windowssdk/winsdksetup.exe

相比 inspect.exe,FlaUInspect 界面更簡潔,功能更易用( 推薦 )

項目地址:https://github.com/FlaUI/FlaUInspect

2-3  安裝 WinAppDriver

通過下面鏈接下載 WinAppDriver 應用程序,並在本地運行起來

https://github.com/Microsoft/WinAppDriver/releases

2-4  搭建 Appium 環境

這部分內容涉及 NodeJS 安裝及 Appium-Server 環境的搭建

可以參考:https://www.cnblogs.com/amoyshmily/p/10500687.html

2-5  安裝依賴

最後安裝 Python 依賴庫 Appium-Python-Client

# 安裝依賴 Appium-Python-Client
pip3 install Appium-Python-Client

3. 實戰一下

我們以操作 PC 端的微信爲例,聊聊自動化的常見步驟

首先,我們在本機打開 WinAppDriver 服務,讓它在後臺運行

然後,我們使用 Python 編寫自動化腳本

通過 ip 地址、端口號及 PC 版微信的絕對路徑,使用 Appium 打開微信

import time, os
from appium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from time import sleep

class Auto():

    def open_weixin(self, host='localhost', port=4723):
        # 打開WinAppDriver服務
        # 注意:如果手動開啓,則可以註釋掉
        # os.system(r'start "" /d "C:\Program Files\Windows Application Driver\"  "WinAppDriver.exe"')

        # 配置信息
        # 包含:平臺名、系統、應用程序絕對路徑
        desired_caps = {'platformName': 'Windows', 'deviceName': 'WindowsPC',
                        'app': r"D:\Program Files (x86)\Tencent\WeChat\WeChat.exe"}

        try:
            # 連接WinAppDriver服務,打開目標軟件
            self.driver = webdriver.Remote('http://{}:{}'.format(host, port), desired_caps)
        except Exception as e:
            raise AssertionError(e)

接着,通過「 組件元素識別工具 」拿到界面元素的屬性值,執行常見的點擊、移動、滑動等操作

比如:點擊「 文件傳輸助手 」,發送一條信息

# 給文件傳輸助手發送一條信息
def send_msg(self, element_name, msg):
    """
​    :param element_name:元素name值
    :param msg:
    :return:
    """
    # 通過name屬性,找到目標元素
    chat_element = self.weixin_driver.find_element_by_name(target_name)

    # 點擊元素,進入聊天界面
    chat_element.click()

    # 找到輸入框,並輸入
    self.weixin_driver.find_element_by_name("輸入").send_keys(msg)

    # 點擊右下角的發送,發送消息出去
    self.weixin_driver.find_element_by_name("發送(S)").click()

需要注意的是,如果涉及界面的滑動,可以使用「 ActionChains 」移動鼠標,然後使用 win32api 和 win32con 模擬屏幕滑動即可

import win32api
​import win32con
from appium import webdriver
from selenium.webdriver import ActionChains

# 模擬屏幕滑動
# 1、移動到某個元素區域
ActionChains(self.weixin_driver).move_to_element(
     self.weixin_driver.find_element_by_name("element_name")).perform()

# 2、滑動界面
# 比如,向上滾動,模擬滑動
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -500)

完成自動化操作後,就可以主動釋放資源、關閉 WinAppDriver 服務

# 釋放資源及關閉服務
def tearDownFunc(self):
​    print("準備退出")
    sleep(2)

    # 1、釋放資源
    self.weixin_driver.quit()

    # 2、關閉WinAppDriver應用程序
    os.system(' @taskkill /f /im WinAppDriver.exe')

4. 最後

在實際使用過程中,可能會遇到複雜的桌面應用程序,這時我們可以通過打印驅動對象的「 page_source」元素控制樹值,以此來幫助我們進行快速定位元素,進而完善自動化腳本

如果你覺得文章還不錯,請大家 點贊、分享、留言 下,因爲這將是我持續輸出更多優質文章的最強動力!

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