Automate Windows Application Testing with Windows Application Driver-Python

Synopsis so far

Looking at @ ootaken's Automating Windows application testing with Windows Application Driver, it seemed quite interesting, so I decided to try it with Python as well. It was. That's boring, so I'll try a keyword-driven test case using Applum Library from Robot Framework. I came up with it.

What is Robot Framework? For those who say, please refer to Previously written article. In the previous article (more than 2 years ago), I used the Selenium2 keyword library, but this time I will use the library for Appium.

Preparation

First, download and install the Windows Application Driver from the following URL. https://github.com/Microsoft/WinAppDriver/releases

Also, download the Python sample file.  https://github.com/Microsoft/WinAppDriver/blob/master/Samples/Python/calculatortest.py

Next, install the relevant Python libraries. If you install robotframework-appiumlibrary, you can enter the whole set.

> pip install robotframework-appiumlibrary

By the way, the confirmation environment is Windows10 Home Version 1703 64bit version + Python 3.6.0. The version of the library is as follows.

>pip freeze                   
apipkg==1.4                                         
Appium-Python-Client==0.24                          
colorama==0.3.7                                     
coverage==4.3.4                                     
decorator==4.0.11                                   
docutils==0.13.1                                    
execnet==1.4.1                                      
mock==1.0.1                                         
py==1.4.33                                          
pytest==3.0.7                                       
pytest-cov==2.4.0                                   
pytest-pythonpath==0.7.1                            
pytest-xdist==1.15.0                                
robotframework==3.0.2                               
robotframework-appiumlibrary==1.4.3                 
sauceclient==0.2.1                                  
selenium==3.3.3                                     ```

#Run the sample I'll try running the original Python sample,Automate Windows application testing with the Windows Application DriverAs with the Java sample in, it will fail in the Japanese environment, so change the Locator specification method and Assert judgment method. I used assertIn because there is no assertThat in the python unittest. Note that the order of the arguments is reversed.

calculator.py


"""
//******************************************************************************
//
// Copyright (c) 2016 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************
"""

import unittest
from appium import webdriver

class SimpleCalculatorTests(unittest.TestCase):

	def setUp(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)

	def tearDown(self):
		self.driver.quit()

	def test_initialize(self):
		self.driver.find_element_by_accessibility_id("clearButton").click()
		self.driver.find_element_by_accessibility_id("num7Button").click()

		result = self.driver.find_element_by_accessibility_id("CalculatorResults")
		self.assertIn(" 7 ", str(result.text))

	def test_addition(self):
		self.driver.find_element_by_accessibility_id("num1Button").click()
		self.driver.find_element_by_accessibility_id("plusButton").click()
		self.driver.find_element_by_accessibility_id("num7Button").click()
		self.driver.find_element_by_accessibility_id("equalButton").click()

		result = self.driver.find_element_by_accessibility_id("CalculatorResults")
		self.assertIn( " 8 ", str(result.text))

	def test_combination(self):
		self.driver.find_element_by_accessibility_id("num7Button").click()
		self.driver.find_element_by_accessibility_id("multiplyButton").click()
		self.driver.find_element_by_accessibility_id("num9Button").click()
		self.driver.find_element_by_accessibility_id("plusButton").click()
		self.driver.find_element_by_accessibility_id("num1Button").click()
		self.driver.find_element_by_accessibility_id("equalButton").click()
		self.driver.find_element_by_accessibility_id("divideButton").click()
		self.driver.find_element_by_accessibility_id("num8Button").click()
		self.driver.find_element_by_accessibility_id("equalButton").click()

		result = self.driver.find_element_by_accessibility_id("CalculatorResults")
		self.assertIn( " 8 ", str(result.text))

	def test_division(self):
		self.driver.find_element_by_accessibility_id("num8Button").click()
		self.driver.find_element_by_accessibility_id("num8Button").click()
		self.driver.find_element_by_accessibility_id("divideButton").click()
		self.driver.find_element_by_accessibility_id("num1Button").click()
		self.driver.find_element_by_accessibility_id("num1Button").click()
		self.driver.find_element_by_accessibility_id("equalButton").click()
		
		result = self.driver.find_element_by_accessibility_id("CalculatorResults")
		self.assertIn( " 8 ", str(result.text))

	def test_multiplication(self):
		self.driver.find_element_by_accessibility_id("num9Button").click()
		self.driver.find_element_by_accessibility_id("multiplyButton").click()
		self.driver.find_element_by_accessibility_id("num9Button").click()
		self.driver.find_element_by_accessibility_id("equalButton").click()

		result = self.driver.find_element_by_accessibility_id("CalculatorResults")
		self.assertIn( " 81 ", str(result.text))

	def test_subtraction(self):
		self.driver.find_element_by_accessibility_id("num9Button").click()
		self.driver.find_element_by_accessibility_id("minusButton").click()
		self.driver.find_element_by_accessibility_id("num1Button").click()
		self.driver.find_element_by_accessibility_id("equalButton").click()

		result = self.driver.find_element_by_accessibility_id("CalculatorResults")
		self.assertIn( " 8 ", str(result.text))



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

Launch the Window Application Driver and then try running the test.

>python calculatortest.py
test_addition (__main__.SimpleCalculatorTests) ... ok
test_combination (__main__.SimpleCalculatorTests) ... ok
test_division (__main__.SimpleCalculatorTests) ... ok
test_initialize (__main__.SimpleCalculatorTests) ... ok
test_multiplication (__main__.SimpleCalculatorTests) ... ok
test_subtraction (__main__.SimpleCalculatorTests) ... ok

----------------------------------------------------------------------
Ran 6 tests in 11.818s

OK

I passed everything.

#Try keyword-driven test Next, let's write and run the same test in Robot Framework. You can use the keywords provided in the Appium Library as they are. Click here for documents on keywords that can be used.  http://serhatbolsu.github.io/robotframework-appiumlibrary/AppiumLibrary.html

I think you don't need to explain much about each test case. Numbers${SPACE}The area surrounded by is not so good, but apart from that, I think it's very intuitive to write. ~~If platformName is not specified in the argument of Open Application of Test Setup, an error will occur in Click Element, so it is specified. Is there a point to note?~~

(2017/04/22 correction) http://appium.io/slate/en/master/?python#create-test-project Regarding the desired capabilities of Windows App Automation Session, there was the following description, so I modified the code.

STARTING A SESSION

Note that you should additionally use these capabilities to ensure you are getting a Windows App automation session:

platformName: Windows deviceName: WindowsPC

I don't follow the official sample though. .. ..

calculator.robot


*** Settings ***
Library  AppiumLibrary
Test Setup  Open Application  http://127.0.0.1:4723  app=Microsoft.WindowsCalculator_8wekyb3d8bbwe!App  platformName=Windows  deviceName=WindocwPC
Test Teardown  Close Application

*** Test Cases ***
Test Initialization
    Click Element  accessibility_id=clearButton
    Click Element  accessibility_id=num7Button
    Element Should Contain Text  accessibility_id=CalculatorResults  ${SPACE}7${SPACE}

Test Addition
    Click Element  accessibility_id=num1Button
    Click Element  accessibility_id=plusButton
    Click Element  accessibility_id=num7Button
    Click Element  accessibility_id=equalButton
    Element Should Contain Text  accessibility_id=CalculatorResults  ${SPACE}8${SPACE}

Test Combination
    Click Element  accessibility_id=num7Button
    Click Element  accessibility_id=multiplyButton
    Click Element  accessibility_id=num9Button
    Click Element  accessibility_id=plusButton
    Click Element  accessibility_id=num1Button
    Click Element  accessibility_id=equalButton
    Click Element  accessibility_id=divideButton
    Click Element  accessibility_id=num8Button
    Click Element  accessibility_id=equalButton
    Element Should Contain Text  accessibility_id=CalculatorResults  ${SPACE}8${SPACE}

Test Division
    Click Element  accessibility_id=num8Button
    Click Element  accessibility_id=num8Button
    Click Element  accessibility_id=divideButton
    Click Element  accessibility_id=num1Button
    Click Element  accessibility_id=num1Button
    Click Element  accessibility_id=equalButton
    Element Should Contain Text  accessibility_id=CalculatorResults  ${SPACE}8${SPACE}

Test Multiplication
    Click Element  accessibility_id=num9Button
    Click Element  accessibility_id=multiplyButton
    Click Element  accessibility_id=num9Button
    Click Element  accessibility_id=equalButton
    Element Should Contain Text  accessibility_id=CalculatorResults  ${SPACE}81${SPACE}

Test Substraction
    Click Element  accessibility_id=num9Button
    Click Element  accessibility_id=minusButton
    Click Element  accessibility_id=num1Button
    Click Element  accessibility_id=equalButton
    Element Should Contain Text  accessibility_id=CalculatorResults  ${SPACE}8${SPACE}

Let's run it. All are Pass.

>robot -d result calculatortest.robot
==============================================================================
Calculatortest
==============================================================================
 Test Initialization                                                   | PASS |
------------------------------------------------------------------------------
 Test Addition                                                         | PASS |
------------------------------------------------------------------------------
 Test Combination                                                      | PASS |
------------------------------------------------------------------------------
 Test Division                                                         | PASS |
------------------------------------------------------------------------------
 Test Multiplication                                                   | PASS |
------------------------------------------------------------------------------
 Test Substraction                                                     | PASS |
------------------------------------------------------------------------------
 Calculatortest                                                        | PASS |
6 critical tests, 6 passed, 0 failed
6 tests total, 6 passed, 0 failed
==============================================================================
Output:  D:\Dev\rf_home\result\output.xml
Log:     D:\Dev\rf_home\result\log.html
Report:  D:\Dev\rf_home\result\report.html

#Summary *Install WIndows Application Driver and Python Appium related libraries *Execution of (modified) Python sample test case on the original site *Rewrite and execute sample test cases into keyword-driven test cases using Robot Framework's Appium Library

As a result of trying, I found that the Windows Application Driver makes test automation of Windows desktop applications fairly easy (for example, a simple button operation of a well-identified application).

No, I'm really looking forward to the future of the Windows Application Driver!

Recommended Posts

Automate Windows Application Testing with Windows Application Driver-Python
Automate python testing with CircleCI
Automate smartphone app testing with Appium-Python
Automate Facebook App Testing with Facebook Test Users
Run (legacy) Windows apps with Windows Application Driver
Automate UI testing with Selenium API | Crawling websites with python
Python starting with Windows 7
Testing Elasticsearch with python-tcptest
Python with VS Code (Windows 10)
Run python with PyCharm (Windows)
venv environment with windows powershell
Web application development with Flask
WebSocket application with Flask-Socket IO
Use Windows 10 fonts with WSL
Web application creation with Django
Pre-try local testing with Travis
Automate sushi making with Python
Testing HTTP requests with ESP-WROOM-32
Time synchronization (Windows) with Python
Blogging with Pelican on Windows