首页 > 资讯 > > 内容页

桌面应用自动化winappdriver

2023-03-29 15:53:39 来源:博客园 分享到:
桌面应用自动化winappdriver关于winappdriver介绍WinAppDriver全称是Windows Application Driver,它提供了一些API,使得用户可以像selenium操作web一样来操作windows的应用程序它支持的系统是Windows 10(Home and Pro) 和Windows Server 2016源码暂未开源WinAppDriver可以独立运行,也可以作为appium的一个插件来使用支持应用类型

UWP– Universal Windows Platform, also known as Universal Apps or Modern Apps, It"s Microsoft’s latest desktop application technology. It"s XAML based. Only runs on Windows 10 machines

WPF- also XAML based, much more mature, runs on any Windows version and has been around since 2006.


(资料图片仅供参考)

WinForms -one of the older technologies, now found mostly on legacy applications.

WPF和WinForms 是两套界面渲染方式。一个是对传统windows界面元素的封装,通过gdi绘制。另一个是全新的dx渲染绘制的界面,也脱离了对传统windows控件的依赖,没有历史包袱,理论上可以展现更炫酷的界面。

MFC/Classic Windows -MFC is a UI library normally paired with Win32 applications. This option is normally chosen when more efficiency is needed with low-level C++ handling or when supporting non-Microsoft platforms.

资源
素材地址说明
FlaUInspecthttps://github.com/FlaUI/FlaUInspect/releases定位工具
WinAppDriverhttps://github.com/microsoft/WinAppDriver/releases/tag/v1.2.1
UIRecorderhttps://github.com/microsoft/WinAppDriver/tree/master/Tools/UIRecorder定位工具
inspect微软官方工具集成于 Windows SDK定位工具
UIRecorder(下文不涉及,仅供参考与备忘)
Open WinAppDriverUIRecorder.slnin Visual StudioSelect Debug> Start Debuggingor simply Run
支持的定位方式
Client APILocator StrategyMatched Attribute in inspect.exeExample
FindElementByAccessibilityIdaccessibility idAutomationIdAppNameTitle
FindElementByClassNameclass nameClassNameTextBlock
FindElementByIdidRuntimeId (decimal)42.333896.3.1
FindElementByNamenameNameCalculator
FindElementByTagNametag nameLocalizedControlType (upper camel case)Text
FindElementByXPathxpathAny//Button[0]
配置开启windows的开发者模式你没看错,不是手机,windows也有第一步:搜开发者设置第二步:打开开发人员模式第三步:确认启用启动winappdriver

不开启开发人员模式的提示

C:\Program Files (x86)\Windows Application Driver>WinAppDriver.exeDeveloper mode is not enabled. Enable it through Settings and restart Windows Application DriverFailed to initialize: 0x80004005

开启后启动winappdriver

C:\Program Files (x86)\Windows Application Driver>WinAppDriver.exeWindows Application Driver listening for requests at: http://127.0.0.1:4723/Press ENTER to exit.

还可以这样启动

WinAppDriver.exe 4727WinAppDriver.exe 10.0.0.10 4725WinAppDriver.exe 10.0.0.10 4723/wd/hub   # 推荐
实例

appium-python-client 版本不要用2.0+,此处是1.2.0

记事本

比如记事本

from appium import webdriverdes_cap = {}des_cap["app"] = r"C:\Windows\System32\notepad.exe"driver = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub",                          desired_capabilities=des_cap)driver.implicitly_wait(5)driver.find_element_by_name("文件(F)").click()from time import sleepsleep(2)driver.find_element_by_name("保存(S)Ctrl+S").click()# driver.find_element_by_name("退出(X)").click()sleep(1)import pyautoguipyautogui.PAUSE = 0.5pyautogui.typewrite(r"D:\hello.txt")pyautogui.press("enter")

这里的难点是保存(S)Ctrl+S的获取

这里需要用到inspect.exe

计算器你可能会写这样的代码
from appium import webdriverdes_cap = {}des_cap["app"] = r"C:\Windows\System32\calc.exe"driver = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub",                          desired_capabilities=des_cap)driver.implicitly_wait(5)
但会报错
Traceback (most recent call last):  File "D:/demo_calc.py", line 5, in     desired_capabilities=des_cap)  File "D:\Python37\lib\site-packages\appium\webdriver\webdriver.py", line 157, in __init__    AppiumConnection(command_executor, keep_alive=keep_alive), desired_capabilities, browser_profile, proxy  File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__    self.start_session(capabilities, browser_profile)  File "D:\Python37\lib\site-packages\appium\webdriver\webdriver.py", line 226, in start_session    response = self.execute(RemoteCommand.NEW_SESSION, parameters)  File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute    self.error_handler.check_response(response)  File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response    raise exception_class(message, screen, stacktrace)selenium.common.exceptions.WebDriverException: Message: Failed to locate opened application window with appId: C:\Windows\System32\calc.exe, and processId: 4472进程已结束,退出代码为 1
打开计算器,然后在powershell中执行如下命令
Get-StartApps |Select-String "计算器"# 输出@{Name=计算器; AppID=Microsoft.WindowsCalculator_8wekyb3d8bbwe!App} # 你要的是这里的AppID
代码
from appium import webdriverdes_cap = {}des_cap["app"] = r"Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"driver = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub",                          desired_capabilities=des_cap)driver.implicitly_wait(5)driver.find_element_by_name("一").click()driver.find_element_by_name("二").click()driver.find_element_by_name("加").click()driver.find_element_by_name("三").click()driver.find_element_by_name("四").click()driver.find_element_by_name("等于").click()# 通过inspect 获取 automationID print(driver.find_element_by_accessibility_id("CalculatorResults").text) # 得到的是   ·显示为 46·  你仍然要处理才能做测试driver.quit()
计算器测试(官网)

我没跑,仅供参考,你可以认为是为了增加篇幅

# https://raw.githubusercontent.com/microsoft/WinAppDriver/master/Samples/Python/calculatortest.pyimport unittestfrom appium import webdriverclass SimpleCalculatorTests(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):        displaytext = self.driver.find_element_by_accessibility_id("CalculatorResults").text        displaytext = displaytext.strip("Display is " )        displaytext = displaytext.rstrip(" ")        displaytext = displaytext.lstrip(" ")        return displaytext    def test_initialize(self):        self.driver.find_element_by_name("Clear").click()        self.driver.find_element_by_name("Seven").click()        self.assertEqual(self.getresults(),"7")        self.driver.find_element_by_name("Clear").click()    def test_addition(self):        self.driver.find_element_by_name("One").click()        self.driver.find_element_by_name("Plus").click()        self.driver.find_element_by_name("Seven").click()        self.driver.find_element_by_name("Equals").click()        self.assertEqual(self.getresults(),"8")    def test_combination(self):        self.driver.find_element_by_name("Seven").click()        self.driver.find_element_by_name("Multiply by").click()        self.driver.find_element_by_name("Nine").click()        self.driver.find_element_by_name("Plus").click()        self.driver.find_element_by_name("One").click()        self.driver.find_element_by_name("Equals").click()        self.driver.find_element_by_name("Divide by").click()        self.driver.find_element_by_name("Eight").click()        self.driver.find_element_by_name("Equals").click()        self.assertEqual(self.getresults(),"8")    def test_division(self):        self.driver.find_element_by_name("Eight").click()        self.driver.find_element_by_name("Eight").click()        self.driver.find_element_by_name("Divide by").click()        self.driver.find_element_by_name("One").click()        self.driver.find_element_by_name("One").click()        self.driver.find_element_by_name("Equals").click()        self.assertEqual(self.getresults(),"8")    def test_multiplication(self):        self.driver.find_element_by_name("Nine").click()        self.driver.find_element_by_name("Multiply by").click()        self.driver.find_element_by_name("Nine").click()        self.driver.find_element_by_name("Equals").click()        self.assertEqual(self.getresults(),"81")     def test_subtraction(self):        self.driver.find_element_by_name("Nine").click()        self.driver.find_element_by_name("Minus").click()        self.driver.find_element_by_name("One").click()        self.driver.find_element_by_name("Equals").click()        self.assertEqual(self.getresults(),"8")if __name__ == "__main__":    suite = unittest.TestLoader().loadTestsFromTestCase(SimpleCalculatorTests)    unittest.TextTestRunner(verbosity=2).run(suite)
标签:
x 广告
x 广告

Copyright ©  2015-2022 欧洲科普网版权所有  备案号:沪ICP备2022005074号-23   联系邮箱: 58 55 97 3@qq.com