Setting up Appium with python for iOS/Android

What are we going to do?

Install Appium as a node module, run the Appium server and get the python client library for Appium.

In this post we are going to cover the basics of setting up Appium with python on MacOS, I found that there is a lack of consistent information on how to get it right from A-Z, so hopefully this will help you:

Pre-requisites:

Steps:

  1. Create a virtual environment
    • mkvirtualenv --python=python3 appium-tests
  2. Install Appium server as a node module via NPM
    • npm install appium
  3. (Optional) To debug system requirements you can install and run appium doctor:
    • npm install appium-doctor
    • appium-doctor --ios andĀ appium-doctor --android
    • The command above should output system requirements and if not met, it will suggest you how to install/fix them
  4. Install Appium Python client:
    • pip installĀ Appium-Python-Client
  5. Starting the Appium server
    • node_modules/.bin/appium --address 127.0.0.1 --port 4723

Now that we have the server running, lets code our first test.

Appium is based on the WebDriver protocol so we will need to instantiate a web driver to interact with our applications.

Web driver takes a dictionary of desired capabilities to define the properties and configuration, we will analyze Android first:

'ANDROID': {
'platformName': 'Android',
'platformVersion': os.environ.get('ANDROID_PLATFORM_VERSION', '5.1'),
'deviceName': os.environ.get('ANDROID_DEVICE_NAME', 'Nexus_5_API_22'),
'app': os.environ.get('ANDROID_APP_PATH', 'apps/AppiumTest.apk'),
'appActivity': 'com.test.appium.MainActivity',
'appWaitActivity': 'com.test.appium.RegistrationActivity'
}

If you are planning to run your tests in a CI environment it is good practice to move your configuration values to environment variables.

platformVersion defines the Android OS version and the API level.

deviceName Sets the Identifier for the device, physical or emulator.

appActivity and appWaitActivity These two are helpful to identify when the application has launched and ready to be interacted with. appActivity is will be the main activity of your application while appWaitActivity can be another activity that starts once the app has launched. A good example of this would be an application that has a registration screen that starts after the application has started.

For iOS:

'IOS': {
'automationName': 'XCUITest',
'platformName': 'iOS',
'platformVersion': os.environ.get('IOS_PLATFORM_VERSION', '9.3'),
'deviceName': os.environ.get('IOS_DEVICE_NAME', 'iPhone 6s'),
'app': os.environ.get('IOS_APP_PATH', 'apps/AppiumTest.app')
}

Same approach but we are also explicitly telling Appium to use XCUITest for the automation.

A good practice is to move all configuration to a separate file ‘settings.py’

APPIUM_SERVER = {
'url': os.environ.get('APPIUM_SERVER_URL', 'http://0.0.0.0:4723/wd/hub'),
}

When creating the web driver instance just send the desired capabilities:

from appium import webdriver

driver = webdriver.Remote(settings.APPIUM_SERVER['url'],
settings.DRIVER_CAPABILITIES['IOS'])

From here you can use the driver to start interacting with your application, for example to tap on a button:

driver.find_element_by_id('login_button').click()

In the next posts I will talk about how to reliably query for elements in your app.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s