[PYTHON] Run (legacy) Windows apps with Windows Application Driver

I had the opportunity to write a simple automated test for a legacy Windows application (Win32) using Windows Application Driver, so I summarized the environment construction and touch. I will. [^ 1]

environment

Environment

  1. Change Windows to ** Developer Mode **. In the search box, search for ** Developer Settings ** and select: devmode.png
  2. Install Python. Don't forget to pass the path: slight_smile:
  3. Install the Windows Application Driver (https://github.com/microsoft/WinAppDriver). Just download and run the MSI and you're good to go.

Try running something with the Windows Application Driver

First, before executing the script, start the Windows Application Driver from the command prompt. When it starts, it goes into a standby state.

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.

For the time being, I tried to move it and searched for a sample, but it only works with an English UI: frowning2: For this reason, I wrote the flow of starting and closing Notepad and saving it.

wda_test.py


import unittest
import os
import time
from appium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common import exceptions

save_file = "c:\\temp\\test.txt"

def remove_save_file():
    if os.path.exists(save_file):
        os.remove(save_file)

class WindowsApplicationDriverTests(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        remove_save_file()
        desired_caps = {}
        desired_caps["app"] = r"C:\Windows\System32\notepad.exe"
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4723',
            desired_capabilities= desired_caps)

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

    def test_edit_and_close_with_save(self):
        #Character input
        self.driver.find_element_by_class_name("Notepad").send_keys("abcdefg")
        #Notepad exit instructions
        self.driver.find_element_by_class_name("Notepad").send_keys(Keys.ALT, Keys.F4)
        #Wait for the save confirmation message to be displayed and press the save button
        time.sleep(1)
        self.driver.find_element_by_name("save(S)").send_keys(Keys.ENTER)
        #Waiting for the Save As dialog to appear
        time.sleep(3)
        #Enter the file name
        self.driver.find_element_by_name("save as").send_keys(save_file)
        #Press the save button
        self.driver.find_element_by_name("Save(S)").send_keys(Keys.ENTER)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(WindowsApplicationDriverTests)
    unittest.TextTestRunner(verbosity=2).run(suite)

Please note that if you do not wait for the message or dialog to be displayed, the operation may not be performed as expected. You can see that Notepad runs automatically when you run the script as shown below.

python wad_test.py

Impressions

reference

[^ 1]: As a result of writing, it seems that it ended at the reference site: severe:

Recommended Posts

Run (legacy) Windows apps with Windows Application Driver
Run python with PyCharm (Windows)
Run SwitchBot on Windows 10 with Bleak
Automate Windows Application Testing with Windows Application Driver-Python
Run servo with Python on ESP32 (Windows)
Develop Windows apps with Python 3 + Tkinter (Preparation)
Run a Python web application with Docker
GUI automation with Python x Windows App Driver
Develop Windows apps with Python 3 + Tkinter (exe file)
Run Kali Linux on Windows with GUI (without VirtualBox)
Setting to run application in subdirectory with nginx + uwsgi
Run Python with VBA
Run prepDE.py with python3
Run Blender with python
Python starting with Windows 7
Run iperf with python