Getting Started with Desktop Application Automation WinAppDriver

1. About WinAppDriver

1 Introduction

  • The full name of WinAppDriver is Windows Application Driver. It provides some APIs so that users can operate windows applications just like selenium operates the web.

  • The systems it supports are Windows 10 (Home and Pro) and Windows Server 2016

  • The source code is not yet open source

  • WinAppDriver can run independently or be used as a plug-in for appium.

2.Support application types

  • 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 and WinForms are two sets of interface rendering methods. One is an encapsulation of traditional Windows interface elements, drawn through GDI. The other is a brand-new dx rendering interface, which also breaks away from the dependence on traditional windows controls. It has no historical baggage and can theoretically show a cooler interface.

  • 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.

3. Resources

  • WinAppDriver
    https://github.com/microsoft/WinAppDriver/releases/tag/v1.2.1

  • Positioning tool FlaUInspect
    https://github.com/FlaUI/FlaUInspect/releases
    WinAppDriver

  • Positioning tool UIRcorder https://github.com/microsoft/WinAppDriver/tree/master/Tools/UIRecorder

  • The positioning tool inspect
    Microsoft official tool is integrated into the Windows SDK

UIRecorder (not covered below, only for reference and memo)

  1. Open WinAppDriverUIRecorder.sln in Visual Studio

  2. Select Debug > Start Debugging or simply Run

4. Supported positioning methods

picture

2. Configuration

1. Turn on the developer mode of Windows

You read that right, it’s not a mobile phone, it’s also available in windows

Step 1: Search developer settings

Step 2: Turn on developer mode

picture

Step 3: Confirm activation

picture

2. Start WinAppDriver

  • Tips for not turning on developer mode

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

  • Start winappdriver after turning it on

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

  • You can also start it like this

WinAppDriver.exe 4727
WinAppDriver.exe 10.0.0.10 4725
WinAppDriver.exe 10.0.0.10 4723/wd/hub # Recommended

3. Examples

Do not use appium-python-client version 2.0+, here is 1.2.0

notebook

Such as notepad

from appium import webdriver
des_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 sleep
sleep(2)
driver.find_element_by_name('保存(S)    Ctrl+S').click()
# driver.find_element_by_name('退出(X)').click()
sleep(1)
import pyautogui
pyautogui.PAUSE = 0.5
pyautogui.typewrite(r'D:\hello.txt')
pyautogui.press('enter')

The difficulty here is the acquisition of Save(S) Ctrl+S

You need to use inspect.exe here

picture

calculator

You might write code like this

from appium import webdriver
des_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)

But it will report an error

Traceback (most recent call last):
  File "D:/demo_calc.py", line 5, in <module>
    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

Open the calculator and execute the following command in powershell

Get-StartApps |Select-String "计算器"
# 输出
@{Name=计算器; AppID=Microsoft.WindowsCalculator_8wekyb3d8bbwe!App} # 你要的是这里的AppID # 同理,记事本你也可以得到其appid,并自动化

code

from appium import webdriver
des_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() # 这些定位你仍然需要inspect
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()

That’s all. You can try it and encapsulate it based on your application. I'll bring it here.

Calculator test (official website)

I didn’t run it, it’s just for reference, you can think it’s to increase the length.

# https://raw.githubusercontent.com/microsoft/WinAppDriver/master/Samples/Python/calculatortest.py
import unittest
from appium import webdriver

class 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)

Finally: The complete software testing video tutorial below has been compiled and uploaded. Friends who need it can get it by themselves [guaranteed 100% free]

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

Guess you like

Origin blog.csdn.net/wx17343624830/article/details/132977458