Mobile Automation Testing using Appium and Python

CodeStax.Ai
6 min readMar 20, 2023

Introduction

Appium is an open-source tool you can utilize to automate mobile web, mobile native, and mobile hybrid applications on iOS and Android outlets. Appium is “cross-platform” in that it enables you to create tests using the same API for a variety of platforms, including iOS, Android, and others. This enables significant or complete code reuse between iOS and Android test suites.

We will cover the Windows-Android setup in this article.

Requirements for the setup:

  1. Appium and Appium inspector
  2. Latest edition of Python and JAVA
  3. Android Studio
  4. Emulators
  5. Plugins installation
  6. IDE (VS Code or PyCharm)
  7. Sample test run

Appium Setup:

Download the latest Appium and Appium Inspector

Appium server can be installed using two ways.

NPM install Appium

  1. Download Node
  2. Install the Node and follow the setup wizard
  3. After Node is installed, open cmd and run npm install -g appium
  4. Enter the Keyword appium to run the Appium server

Download Appium Desktop

  1. Download the Appium desktop
    (This is link for 1.21.0 version, with this version we don’t need to install the appium inspector separately. Inspector was in-built until this version)
  2. Run Appium and start the server

If you using 1.21.0 or before, the below Appium Inspector installation does not apply. You can directly open the inspector from the Appium server page by clicking the search button.

Download Appium Inspector

  1. Download the Appium inspector
  2. Appium inspector is used to locate the elements and find the paths of the elements in a page
  3. Open the Appium inspector

Download Python and Java

Python Installation

  1. Download the latest version of Python
  2. Launch the installer and install. Check the ‘Add Python to PATH’ checkbox to add the path to the environment path
  3. Open cmd and run the command ‘python’ to check the python is installed properly.

Java Installation

  1. Download the latest version of JAVA Jdk
  2. Add a new path JAVA_HOME in environment variables like below
    (Added the JAVA_HOME for the appium setup)

Android Studio Installation

  1. Download the Android studio
  2. Open the Android studio and launch the AVD manager.
  3. Build an emulator on your choice and launch it.
  4. Add the ANDROID_HOME path in environment variables.
    (ANDROID_HOME path for the appium setup, here app data is hidden in the folder so you need to change the view settings of the folder to show the hidden files also)

5. Add the base paths of the build tools, tools, and platform tools to the environment variable like below.

Emulator Installation

  1. Download and install the BlueStacks emulator follow the steps provided by the installation process.
    (Why we are using the Bluestacks? BlueStacks is lite compared to Android Studio. You can also use any other emulators as you wish or even your actual mobile devices for automation)
  2. Open the Settings and in advanced settings turn on the ADB debugging.
  3. Open cmd and enter the command ‘adb devices’ to know the device is compatible for adb debugging.

Plugins installation

Install plugins like Selenium and Appium Python Client

Open cmd and run the below commands

pip install selenium

Install Selenium using this command.

pip install Appium-Python-Client==2.0.0

Install Appium Python Client using this command.

IDE Installation(VSCode or PyCharm)

  1. Download and install the latest version of the VSCode.
  2. Open the VSCode and start writing your script.

Virtual environment setup

Now, all the setups are done, It’s time to begin developing the mobile application’s automation script.

I am using a virtual environment for my project.

Command to create a venv ‘python -m venv /path/to/folder’

I created a folder tests in my venv to write the automation scripts.

Appium Inspector walk-through

Before we start writing the test script we need to locate the elements of the application to write the test scripts.

Run the Appium and Appium Inspector to create the local session.

Add the Desired Capabilities as per your requirements (device name and application path) and start the session. It will start the application and shows the application page in the inspector, where you can get the locators such as id, Xpath, text, class, etc. Here I have used the Flipkart application to test.

In the above screenshot we can see the first page of the Flipkart application. In that we can get the locators of the elements which we need to locate. Using these locators we start writing the sample test case to run.

Sample test run

Here we add some desired capabilities in a file ‘des_cap.py’. This is the capability file to run the automation.

def get_des_cap():
des_cap = {
“app”: “c:\\users\\Jaga\\abc.apk”,(Path of the app to automate)
“automationName” : “UiAutomator2”,
“platformName” : “android”,
“udid” : “device name”,
“noReset” : “true”,
“appWaitForLaunch” : “false”,
“newCommandTimeout” : “120”,
“autoAcceptAlerts” : “true”
}
return des_cap

Create a test file hello.py to start the basic automation.

import unittest
from selenium.webdriver.common.by import By
from appium import webdriver
from des_cap import get_des_cap

class sample(unittest.TestCase):

#driver initialization
driver = webdriver.Remote("http://localhost:4723/wd/hub", get_des_cap())

def test_hello000(self):
self.driver.implicitly_wait(20)
self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"English\")").click()
self.driver.find_element(By.ID, "com.flipkart.android:id/select_btn").click()
self.driver.find_element(By.ID, "com.flipkart.android:id/phone_input").send_keys('9876543210')
self.driver.find_element(By.XPATH, "/hierarchy/android.widget.FrameLayout[3]/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.LinearLayout[2]/android.widget.RelativeLayout/android.widget.TextView").click()
if __name__ == '__main__':
unittest.main()

Here I have imported the needed packages and capability file(des_cap) and then initialized the driver. In the automation script, I tried to choose the language using the name of the element, then clicked the ‘continue’ button using the ID, then input the mobile number using the id and send_keys method and then used Xpath to click the ‘next’ button. Here I have used the most important locators to write the script.

This is a sample test run to show how the automation works. You can check the Appium documentation to know more information. Please comment below if you have any queries.

About the Author

Jagadeesh is a test automation engineer with extensive experience in automating web and mobile applications. His expertise includes Software QA testing across the software development life cycle, including designing, developing, and implementing test plans, test cases and test processes.

About CodeStax.Ai

At CodeStax.Ai, we stand at the nexus of innovation and enterprise solutions, offering technology partnerships that empower businesses to drive efficiency, innovation, and growth, harnessing the transformative power of no-code platforms and advanced AI integrations.

But the real magic? It’s our tech tribe behind the scenes. If you’ve got a knack for innovation and a passion for redefining the norm, we’ve got the perfect tech playground for you. CodeStax.Ai offers more than a job — it’s a journey into the very heart of what’s next. Join us, and be part of the revolution that’s redefining the enterprise tech landscape.

--

--

CodeStax.Ai

Tech tales from our powerhouse Software Engineering team!