Appium自动化Windows APP

博客首发
前提条件
环境搭建
  • 打开Windows PC的开发者模式

  • 下载Windows SDK并默认安装

  • 下载Windows driver并默认安装

  • 运行WinAppDriver.exe(记得要用admin权限运行), 默认路径 (C:\Program Files (x86)\Windows Application Driver)
    可以自定义地址或端口:

    WinAppDriver.exe 4727
    WinAppDriver.exe 127.0.0.1 4725
    WinAppDriver.exe 127.0.0.1 4723/wd/hub
    

    如下图:
    在这里插入图片描述

Windows 自动化脚本

运行脚本前要打开 WinAppDriver.exe
对于Windows App来说,只需要传一个app capabilities 即可。
对于UWP的App,app对应的值为Application Id(App ID)。关于如何获取APP ID,可以使用powershell命令get-StartApps来获取,打开powershell终端运行:get-StartApps | select-string "计算器"即可获取值(运行命令之前先打开计算器)。以下是java样例代码:

 DesiredCapabilities cap = new DesiredCapabilities();
 cap.setCapability("app", "LexisNexisAPAC.LexisRed_wsek3cqrhvvz2!App");
 driver = new WindowsDriver(new URL("http://127.0.0.1:4723"), cap);
 driver.findElementByAccessibilityId("CalculatorResults");

对于经典的Windows App,app对应的值为可执行的.exe文件路径。以下是java样例代码:

// Launch Notepad
DesiredCapabilities cap= new DesiredCapabilities();
cap.SetCapability("app", "C:\\Windows\\System32\\notepad.exe");
cap.SetCapability("appArguments", "MyTestFile.txt");
cap.SetCapability("appWorkingDir", "C:\\MyTestFolder");
driver= new WindowsDriver(new URL("http://127.0.0.1:4723"), cap);
// Use the session to control the app
driver.FindElementByClassName("Edit").SendKeys("This is some text");
Windows定位元素

使用Windows SDK提供的工具inspect.exeC:\Program Files (x86)\Windows Kits\10\bin\x86或者C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64根据系统查看)来定位,详情查看inspect,或者使用AccExplorer32UISpy定位。
支持的定位方式:

API 定位方法 对应inspect.exe的属性 例子
FindElementByAccessibilityId accessibility id AutomationId AppNameTitle
FindElementByClassName class name ClassName TextBlock
FindElementById id RuntimeId (decimal) 42.333896.3.1
FindElementByName name Name Calculator
FindElementByTagName tag name LocalizedControlType (upper camel case) Text
FindElementByXPath xpath Any //Button[0]
计算器的例子

Python(GitHub):

import unittest
from appium import webdriver


class WindowsCalculatorTest(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        # set up appium
        desired_caps = {
   }
        desired_caps["app"] = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"
        self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities=desired_caps)

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

    def getresults(self)
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值