WinAppDriver - Windows桌面软件自动化

这是写的第一篇文章,用来记录学习过程,如有不对的地方请指正

WinAppDriver

由microsoft团队开发的Windows桌面软件驱动,支持Win10及以上的系统。类似于用selenium做web自动化时一样,运行Chrome需要有chromedriver驱动。所以,当我们需要运行windows软件,就需要一个驱动,这个东西就是WinAppDriver。
github项目地址 - WinAppDriver

支持基于下述框架开发的APP

  1. Universal Windows Platform (UWP)
  2. Windows Forms (WinForms),
  3. Windows Presentation Foundation (WPF)
  4. Classic Windows (Win32)

决定使用WinAppDriver做自动化前最好先用inspect看一下要测试的APP是否可以提取元素
之前公司用的前端框架是kivy,是个混血框架,用inspect抓取元素的时候发现根本就获取不到,所以就只能放弃转用pyautogui来做自动化

kivy开发的APP(DOM)

环境准备

  1. 安装WinAppDriver - https://github.com/Microsoft/WinAppDriver/releases
    默认安装就好,默认安装后会保存在C:\Program Files (x86)\Windows Application Driver
  2. 打开windows开发者模式


    image.png
  3. 安装inspect
    https://docs.microsoft.com/zh-cn/windows/win32/winauto/inspect-objects?redirectedfrom=MSDN
  4. 运行WinAppDriver.exe
    如需要指定IP地址则可以用winappdriver.exe+IP地址,如winappdriver.exe 127.0.0.1 4725
    指定端口号

工作原理

运行WinAppDriver后,会在被测机上(运行WinAppDriver的机器)开启监听,用于接收JsonWireProtocol
通讯协议的请求,根据请求的不同来执行不同的操作,如:打开软件,点击按钮,输入字符串等
API列表详见 - https://github.com/microsoft/WinAppDriver/blob/master/Docs/SupportedAPIs.md

如何向WinAppDriver发起请求

Selenium

建议使用selenium3.5.0版本,新的版本会存在获取的element是一个字典,导致不能调用click等一系列的问题。
如果是用selenium的话只能通过name来查找元素,如果想通过AutomationID来查找元素的话还是得用Appium。

因为WinAppDriver是通过JSON wire protocol协议进行通信,而Selenium封装好了该协议,所以直接用就好了,不需要再去费劲儿阅读协议文档来组装报文。

最新的selenium已经弃用JSON wire protocol改用W3C WebDriver协议,两个协议比较相似,所以暂且不用担心调用WinAppDriver的接口会出现问题

我用的是python,所以先pip install Selenium

import unittest
# from appium import webdriver
from selenium import webdriver

class WindowsCalculatorTest(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        # set up appium
        desired_caps = {
            "app": "D:\\progam files\\XMind\\XMind.exe",
            'platformName': 'Windows',
            'automationName': 'Windows'
        }
        self.driver = webdriver.Remote('http://localhost:4723', desired_capabilities=desired_caps)
        # self.driver.
        print(self.driver)

    # @classmethod
    # def tearDownClass(self):
    #     self.driver.quit()

    def test_addition(self):
        pass


if __name__ == '__main__':
    unittest.main()

运行后就会打开Xmind,更多的示例可查看官方示例1官方示例2

Appium

Appium其实是封装好了Selenium,所以相当于是在用Selenium,建议使用2.2.0版本,否则会出现像selenium一样的问题。使用前先pip Appium-Python-Client,示例

还有一种使用Appium的方法是通过Appium server转发请求,大概的工作原理就是Appium server接收到请求,再根据请求的不同转发给不同的驱动,驱动收到请求后就可以完成相关操作。使用该方法时不用开启WinAppDriver

在被测机上安装好Appium server后运行Appium server,选择合适的地址,如果此时WinAppDriver是开启状态且地址为默认地址,port为4723就会冲突,需要选择其他端口再开启服务。

Appium首页

开启服务后就会开启监听,此时只需要向Appium server发起请求,其就会根据请求的类型转发给相应的驱动。
如:当platformNameWindows时,Appium server就会启动WinAppDriver并向其发送请求
Appium 1.x server的地址末尾需加上/wd/hub
Appium 2.0及以上版本的地址为/

desired_caps = {
            "app": "D:\\progam files\\XMind\\XMind.exe",
            'platformName': 'Windows'
        }
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_capabilities=desired_caps)

但是,Appium会先发起/status请求,但经过验证(/status接口测试)该接口不通,导致请求失败。我的WinAppDriver版本为1.2.1,Appium是1.22.2。

所以想用这个方法的话可能需要降版本,具体我还没试过,等后头我试过的话再来更新
更新:WinAppDriver1.2.2009版本可用

Appium - response of /status

test - response of /status

HTTP请求

可以用postman或者jmeter等接口测试工具向驱动发起请求,如打开APP
接口报文是在用selenium测试的时候从WinAppDriver上拷贝过来的,具体的接口报文须查阅JsonWireProtocol
URL为服务地址(在未指定地址的情况下为127.0.0.1:4723)+ path

postman调用WinAppDriver接口

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容