Automate smartphone app testing with Appium-Python

1.First of all

Last time, I wrote in "Automating smartphone app testing with Appium-Overview" that you can automate smartphone app testing with Appium.

This time, I would like to write about how to create test code for smartphone apps using Appium and Python.

(Supplement) At the moment (2013/7/20), it is only for iOS apps. The Android app will be described later.

2. Prerequisites

The environment used this time is as follows.

software version
OS MacOS X version 10.8.4
Python 2.7.4
pip 1.2.1
XCode 4.5.3

For the installation of Appium, please refer to the previous article: "Automating the testing of smartphone apps using Appium-Overview".

3. Run the sample code

First of all, let's check the operation using the sample code. The operations from here are performed on the terminal.

Get the complete Appium source from GitHub.

git clone https://github.com/appium/appium.git

Once you have the complete Appium source, go to the TestApp folder.

cd appium/sample-code/apps/TestApp/

Build your iOS app using the xcodebuild command. The iOS SDK used for the build should be iPhone Simulator.

xcodebuild -sdk iphonesimulator

Navigate to the folder that contains the Python sample code.

cd ../../examples/python/

Before running the sample code, make sure you have the Python selenium module included. If not, use pip to install the selenium module.

#Check if the Python selenium module is included
pip freeze

#If you don't have the selenium module, run the following command
sudo pip install selenium

Now that we're ready, let's move the sample code.

appium &
python simple.py

4. Learn how to write test code from sample code

I searched a lot, but I couldn't find a tutorial on how to write test code in Python, so I would like to summarize how to write test code based on the sample code. → If you find something like a tutorial, rewrite the content of this article.

The storage location of the sample code is "appium / sample-code / examples / python /".

If you read the sample code, you can see that the iOS app is running via the WebDriver API of the selenuim module.

(1) Create a test class that inherits the unittest.TestCase class

import unittest


class AppiumSampleTest(unittest.TestCase):

	def setUp(self):
		pass

	def tearDown(self):
		pass

(2) Register the iOS application to be started by the setUp method of the test class in Appium.

def setUp(self):
	#Get the path where the iOS app is located
	# (Import the os module)
	app = '(The path where the iOS app is located)'
	
	#Make Appium recognize iOS apps using selenium's web driver
	# (Import the webdriver from the selenium module)
	# command_The domain part and port part specified by the executor are
	#It depends on the environment you are using
	self.driver = webdriver.Remote(
				command_executor = 'http://127.0.0.1:4723/wd/hub',
				desired_capabilities = {
					'browserName' : 'iOS',
					'platform' : 'Mac',
					'version' : '6.0',	#iOS version
					'app' : app		#The path where the iOS app to run is located
				})

(3) Exit the iOS app with the tearDown method of the test class.

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

(4) Add a test method

def test_ui_operation(self):
	# 1.Slider operation
	# 1-1.Get the slider
	sliders = self.driver.find_elements_by_tag_name('slider')
	# 1-2.Evaluate the initial value of the slider
	self.assertEqual(sliders[0].get_attribute('value'), '50%')
	# 1-3.Get a touch event
	drag = webdriver.common.touch_actions.TouchActions(self.driver)
	# 1-4.Set the slider-50%To
	drag.flick_element(sliders[0], -0.5, 0, 0)
	# 1-5.Move the slider
	drag.perform()
	# 1-6.Slider setting value is 0%Evaluate that
	self.assertEqual(sliders[0].get_attribute('value'), '0%')

	# 2.Manipulating text fields
	# 2-1.Get a text field
	textFields = self.driver.find_elements_by_tag_name('textField')
	# 2-2.Enter 1 in the first text field
	textFields[0].send_keys(1)
	# 2-3.Enter 2 in the second text field
	textFields[1].send_keys(2)
	# 2-4.Evaluate that the sum of the values in the first and second text fields is 3.
	self.assertEqual(int(textFields[0].get_attribute('value'))
                         + int(textFields[1].get_attribute('value')), 3)

	# 3.Button operation
	# 3-1.Get the button
	buttons = self.driver.find_elements_by_tag_name('button')
	# 3-2.Click the button
	buttons[0].click()

	# 4.Label manipulation
	# 4-1.Get the label
	texts = self.driver.find_elements_by_tag_name('staticText')
	# 4-2.Evaluate that the content of the label is 3
	self.assertEqual(int(texts[0].get_attribute('value')), 3)

To summarize the handling of screen parts of iOS applications in the selenium module

How to get screen parts

Screen parts Acquisition method
UITextField self.driver.find_elements_by_tag_name('textField')
UIButton self.driver.find_elements_by_tag_name('button')
UILabel self.driver.find_elements_by_tag_name('staticText')
UISlider self.driver.find_elements_by_tag_name('slider')

How to operate screen parts

Screen parts Operation details Method of operation
UITextField Enter a value send_Use the keys method
UITextField Get the value get_Use attribute method
UIButton Click the button use the click method
UILabel Get the label display content Refer to the text property
UISlider Change the setting value Touch Actions flick_Set the value with the element method. After setting, move the slider with the perform method of TouchActions.

It will be.

(Supplement) The above table will be updated from time to time.

The overall picture of the source code is as follows.

# coding: utf-8

import unittest
import os
from selenium import webdriver


class AppiumSampleTest(unittest.TestCase):

    def setUp(self):
        #Get the path where the iOS app is located
        app = os.path.join('/tmp/appium',
                    'sample-code/apps/TestApp/build/Release-iphonesimulator',
                    'TestApp.app')
        app = os.path.abspath(app)

        #Make Appium recognize iOS apps using selenium's web driver
        # command_The domain part and port part specified by the executor are
        #It depends on the environment you are using
        self.driver = webdriver.Remote(
            command_executor='http://localhost:4723/wd/hub',
            desired_capabilities={
                'browserName': 'iOS',
                'platform': 'Mac',
                'version': '6.0',
                'app': app
            })

    def test_ui_operation(self):
        #Slider operation
        sliders = self.driver.find_elements_by_tag_name('slider')
        self.assertEqual(sliders[0].get_attribute('value'), '50%')
        drag = webdriver.common.touch_actions.TouchActions(self.driver)
        drag.flick_element(sliders[0], -0.5, 0, 0)
        drag.perform()
        self.assertEqual(sliders[0].get_attribute('value'), '0%')

        #Manipulating text fields
        textFields = self.driver.find_elements_by_tag_name('textField')
        textFields[0].send_keys(1)
        textFields[1].send_keys(2)
        self.assertEqual(int(textFields[0].get_attribute('value'))
                         + int(textFields[1].get_attribute('value')), 3)

        #Button operation
        buttons = self.driver.find_elements_by_tag_name('button')
        buttons[0].click()

        #Label manipulation
        texts = self.driver.find_elements_by_tag_name('staticText')
        self.assertEqual(int(texts[0].get_attribute('value')), 3)

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


if __name__ == '__main__':
    unittest.main()

5. Reference

Recommended Posts

Automate smartphone app testing with Appium-Python
Automate Facebook App Testing with Facebook Test Users
Automate python testing with CircleCI
Automate Windows Application Testing with Windows Application Driver-Python
Automate UI testing with Selenium API | Crawling websites with python
linux with termux app
Testing Elasticsearch with python-tcptest
[Blender] Automate Blender add-on testing with GitHub and Travis CI
Pre-try local testing with Travis
Automate sushi making with Python
Testing HTTP requests with ESP-WROOM-32