Last Updated Apr 12, 2020 — Continuous Testing Expert

This guide focuses on Appium testing using Python as a programming language. Appium and Python is a good combination for Mobile Automation and App Testing. Python being an Interpreted, high-level programming language offers a faster development time.

Table of Contents:

Overview

Appium is an Open Source, Cross-platform utility for testing Native, Web and Hybrid applications on iOS, and Android Mobile Operating System platforms. Appium Testing is flexible, you can use the same code for iOS that you have written for Android.

Cross-platform here means that it allows the same API which works for different mobile operating systems. Hence it is an ideal choice and in fact the most widely used tool in Mobile Automation.

Appium is based on Client-Server Architecture, where Appium testing Clients send automation commands to the Appium Server which translates it to platform-specific commands and executes on the devices.

Appium Clients are basically libraries exposed by the Appium framework for Mobile Automation which can be used by test engineers. It supports various languages such as Java, Python, and Ruby.

This guide focuses on Appium testing using Python as a programming language. Appium and Python is a good combination for Mobile automation. Python being an Interpreted, high-level programming language offers a faster development time. For more specifically on Appium Testing, visit our Appium Studio.

Installation

Appium Server

Appium Server can be installed using two ways explained below.

Using NPM

  1. Download Node
  2. Double click the downloaded file and follow the steps in the wizard to finish the installation of Node.
  3. Once NodeJS is installed execute, npm install -g appium
  4. To run Appium Server execute, appium

Using Appium Desktop

  1. Download the Appium Desktop
  2. Launch the Installer of the platform of your choice and follow the setup wizard.
  3. Run the Appium Desktop installed in the machine, this also launches the Appium Server.

For detailed instructions on how to install refer to the official page of Appium.

Python

There are many different Python distributions since its open source. But this blog will stick to the official distribution of Python on the Windows platform.

Python Installation

  1. Download the latest version of Python. You can click on the latest version and navigate to the Files section on the page and download it.

    Appium Testing using Python

  2. Launch the executable installer and Click Install Now. Check the ‘Add Python to Path’ checkbox if you prefer the Python to be added to your environment path.

    Appium Testing using Python

  3. Follow the setup wizard to complete the installation.

Python will get installed in the users directory, example c:Users\AppDataLocalProgramsPython in Windows.

Double-check the installation by executing python in the command prompt as shown below.

Appium Testing using Python

PyCharm Installation

As Engineers, it is always preferred to use Integrated Development Environment (IDE) for faster development. Pycharm is one of the widely used IDE’s for Python and this blog will focus on that.

  1. Download Pycharm. Note: This blog uses the Free Community version of Pycharm
  2. Launch the executable installer.
  3. Follow the setup wizard by clicking the Next button and finally click Install to complete the installation.

Interpreter Configuration

We need to configure the Python interpreter in the Pycharm, so that any project inherits the interpreter by default.

  1. Launch the Pycharm
  2. Click Configure > Settings as shown below.

    Appium Testing using Python

  3. This will open up the Settings screen.

    Pycharm settings screen

  4. In the next screen click ‘+‘ and this will display all the interpreters installed in the system. Choose the System Interpreter left pane and select the interpreter path which you installed before in the Python Installation section and Click OK button.

    Pycharm System Interpreter

  5. In the next screen, click OK and Apply to finish the configuration. This screen displays the available packages in the selected Python interpreter.

    Pycharm Python interpreter

Appium Testing Using Python

This section is a quick start for a basic mobile app automation test using Appium Python testing. The Sample project is developed in Pycharm IDE and for an Android device.

Prerequisites

  1. The latest version of Java, needed for Android Studio.
  2. Installation of Android Studio with SDK. This is needed since adb gets installed as part of the Android SDK. The adb utility is required to get the device list connected to the PC.
  3. Download sample Android application called Eribank using the link https://experitest.s3.amazonaws.com/eribank.apk
  4. Installation of the latest version of PyCharm.
  5. Connecting the Mobile device to the PC using USB cable and enabling the developer mode/USB debugging in the Android device.

Creating Your First Test Using Appium and Python

Step 1. Start Appium Server

Launch the Appium Desktop and start the server.

By default it will run on localhost:4723. This example will consider the default.

Appium start server screen

Note: You can use any host:port of your choice, in that case, make sure you use the same in this example.

Step 2. Create a Python project in Pycharm.

Pycharm create project

Name the Project and click Create to create the project.

Pycharm Name the Project

Step 3: Set the Interpreter and add the Appium dependency for the project.

Select the newly created project and in the menu navigate to File > Settings.

Pycharm file settings

The interpreter for the project will be set automatically, See ‘Interpreter Configuration’ section above.

Click ‘+‘ and then search for Appium in Available Packages dialog box.

Select Appium-Python-Client and click Install Package to install Appium Python Client.

Pycharm Appium-Python-Client

This will download the Appium Client dependency for this project.

Click OK to save the project.

Step 4: Creating a test in Python.

Select the project and then right-click. Click New > Python File

Pycharm Select the project and then right-click

This will open a dialog box, Specify a test name and click the Python unit test.

Pycharm specify test name

This will create an initial Python unit test class.

We must now create an Appium Driver instance by passing the Desired Capabilities in the initialization function of the test. Basically the Appium Driver instance connects to the device and installs the application in the device.

Driver Initialization

[python]def setUp(self):[/python]

# This is the Application and ‘app’ desired capability to specify a path to Appium.

[python]self.dc[‘app’] = “c:\eribank.apk”[/python]

# appPackage and appActivity desired capability specify app details to Appium

[python]self.dc[‘appPackage’] = “com.experitest.ExperiBank”[/python] [python]self.dc[‘appActivity’] = “.LoginActivity”[/python]

# platformName desired capability specify platform detail to Appium

[python]self.dc[‘platformName’] = ‘Android'[/python]

# deviceName desired capability specify the device id detail to Appium

# device id is got from running adb devices command in PC

[python]self.dc[‘deviceName’] = ‘a3ae1c63′[/python]

# Creating the Driver by passing Desired Capabilities.

[python]self.driver = webdriver.Remote(“http://localhost:4723/wd/hub”,self.dc)[/python]

Test method

We now will create a test method that sends out the automation commands to the Appium Server (device). In general, it contains the way to locate UI elements of the application and perform an action on the elements.

[python]def testFirstAutomationTest(self): if len(self.driver.find_elements_by_xpath(“//*[@text=’OK’]”)) > 0 : self.driver.find_element_by_xpath(“//*[@text=’OK’]”).click();[/python]

# Find location of Elements and perform actions.

[python]self.driver.find_element_by_xpath(“//*[@text=’Username’]”).send_keys(‘company’) self.driver.find_element_by_xpath(“//*[@text=’Password’]”).send_keys(‘company’) self.driver.find_element_by_xpath(“//*[@text=’Login’]”).click()[/python]

Release the Driver

# Function to Release the driver

[python]def tearDown(self): self.driver.quit()[/python]

Copy the above functions to the Python test we created before. The final code will look like this.

Appium Testing using Python

Executing the Test

To run the test, create a Run Configuration by navigating to Pycharm menu, Run > Edit Configurations. This will popup the Run Configuration screen. Click ‘+‘ > Python Tests > Unittests and then OK Button as shown below.

Appium Testing using Python

This creates a new Run test in the left pane. Select the Run test, specify the run test name in Name. Select the Module name in the Target field, and click ‘‘. This will popup a dialog, please type the test name in the Search by Name text field and select the test. Click the OK button to save this Run Configuration.

Appium Testing using Python

Run this configuration using Pycharm Run Menu.

The Appium Python testing example project associated with this blog is located at First Test using Appium and Python.

Guy ArieliCTO

Are you ready to scale your enterprise?

Explore

What's New In The World of Digital.ai

February 22, 2024

How Futurism is Shaping Cloud Testing: A Forecast

Unlock the future of cloud testing: strategic approaches to leverage technology effectively, enhance software quality, and ensure business success.

Learn More
December 4, 2023

The Drive for Quality: Continuous Automated Software Testing for the Automotive Industry

From AI-powered test creation to self-healing systems, discover how continuous testing and innovative developments are shaping the future of connected, safe, and reliable vehicles.

Learn More
November 13, 2023

“Don’t Sweat the AI Techniques”: How AI and ML are Revolutionizing Web and Mobile Automated Testing

Discover AI’s role in automated testing—AI-powered creation, self-healing, analytics, and change risk predictions for efficient, high-quality results.

Learn More