I am going to do UI automation tests for a desktop application using appium. My preference is using appium python client. However there are no python test samples for a desktop application and all the examples are dedicated for android or ios apps. And I don't really know how to start with my project without a sample code. Can anyone tell me, if it is possible to do UI automation tests for desktop apps in python. I would be greatful, if you would send me also a example link.
yes you can!
You will need to download and install WinAppDriver from here: https://github.com/Microsoft/WinAppDriver/releases and Developer mode will have to be enabled on your PC, this will tell you how: https://github.com/microsoft/WinAppDriver
You will also need to ensure that you are using Selenium at v3.141.0 and appium-python-client at v1.3.0
pip install selenium==3.141.0
pip install appium-python-client==1.3.0
You can run tests with or without Appium.
To run without make sure Appium is not running and try the following example (modified from https://github.com/microsoft/WinAppDriver/tree/master/Samples/Python):
import unittest
from appium import webdriver
import os
class SimpleCalculatorTests(unittest.TestCase):
@classmethod
def setUpClass(self):
# WinAppDriver can be run independently instead if required
os.startfile('C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe')
#set up appium
desired_caps = {}
desired_caps["app"] = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"
desired_caps['platformName'] = "Windows"
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")
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(SimpleCalculatorTests)
unittest.TextTestRunner(verbosity=2).run(suite)
To run using Appium try the following example instead:
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"
desired_caps['platformName'] = "Windows"
#self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723/wd/hub', 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")
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(SimpleCalculatorTests)
unittest.TextTestRunner(verbosity=2).run(suite)