How to Use Appium for Mobile Automation Testing
User experience — perhaps the most commonly used word among developers especially when it comes to mobile applications. This makes mobile automation the key to deliver high-quality apps faster and without compromising on performance.
Appium is a powerful tool that facilitates this process by providing a cross-platform solution for automating mobile applications.
What is Appium?
Appium is an open-source tool for testing user interfaces of mobile apps. it enables you to write tests for native, hybrid, and mobile web applications. Appium supports automation on physical devices as well as virtual ones like emulators or simulators, making it a versatile tool for mobile automation.
For more information on Appium, visit website: https://appium.io/
Why is Mobile Automation Important?
Here are some reasons that make mobile automation testing an important aspect of delivering excellent user experience:
Efficiency and Speed
Automated tests run significantly faster than manual testing, offering quicker feedback and accelerating development cycles.
Consistency and accuracy
Automation eliminates human error by executing precise, repeatable steps, ensuring consistent performance across various devices and operating systems.
Cost effectiveness
While automation requires initial investment, it reduces long-term testing costs through reusable scripts and fewer manual efforts.
Scalability
Automation supports simultaneous testing across multiple devices and operating systems with parallel execution to further enhance speed and efficiency.
Improved Test Coverage
Automation enables broader testing, including edge cases and regression testing, ensuring new changes don’t impact existing functionality.
Continuous Integration Support
Mobile automation integrates seamlessly in CI/CD pipelines, enabling frequent, reliable testing as part of the development process.
Early Bug Detection
Frequent automated tests catch bugs early in development, reducing the risks of critical issues in production.
Writing a Python Script Using Appium for Automating an APK with Android Studio Simulator
In this section, we will write a Python script with Pytest Framework and use Appium to automate an APK on an Android Studio simulator. This involves setting up the environment, defining the desired capabilities, and writing the test script.
Prerequisites
- Install Python: Make sure Python is installed on your system. You can download it from python.org.
- Install Appium: You can install Appium using npm (Node Package Manager). Run the following command in your terminal:
npm install -g appium
- Install Pycharm (Recommend): Pycharm is Python IDE and compiler used for writing Python scripts using multiple external libraries
Download & install:
https://www.jetbrains.com/pycharm/download/?section=windows
(I have used pycharm community, download from the above link)
When pycharm is installed, we need to add libraries e.g. Appium-Python-Client and Pytest framework from the packages option of pycharm IDE. - Android Studio: Ensure you have Android Studio installed with the necessary SDKs and an emulator set up.
Your emulator must have an updated Android version with the necessary storage for running the apk with Appium.
Download and install android studio from:
https://developer.android.com/studio/install?gad_source=1&gclid=Cj0KCQjwgL-3BhDnARIsAL6KZ68JOK5RJo2eVyEYcX29DcsTGTfbe3yJH2xTkTN2xn69CwR8_HqMN0saAtR5EALw_wcB&gclsrc=aw.ds
(Your installed emulator will look like the image below) - Environment Variable: Make sure you have installed Java and created an environment variable of Android Studio SDK for accessing adb from the root or any side of the windows from the command terminal.
Step-by-Step Guide for Implementation and Execution.
We are implementing two test cases of application with the script
- Check the first page’s negative case. (We will validate that the correct error message is shown on skipping the name field)
- Check the First page positive case (input valid values in the fields and selections)
- Start Appium Server:
Start the Appium server by running the following command in your terminal:
appium –allow-cors
Create python fixture file conftest.py using the code below,
def setup_capabilties():
capabilities = {"deviceName": "Android Emulator",
"platformName": "Android",
"appWaitActivity": "*",
"automationName": "UiAutomator2"}
return capabilities
def load_driver(capabilities):
opt = AppiumOptions()
opt = opt.load_capabilities(capabilities)
'
appium_server_url= 'http://localhost:4723'
driver= webdriver.WebDriver(appium_server_url, options=opt)
driver.implicitly_wait(2)
return driver
@pytest.fixture(scope='session')
def general_store_fixture():
capabilities= setup_capabilties()
capabilities["appPackage"]="com.androidsample.generalstore"
capabilities["app"]= "C:\\Users\\abbas.khan_codup\\AppData\\Local\\Android\\Sdk\\platform-tools\\hybrid.apk"
driver= load_driver(capabilities)
return driver
What are Appium Capabilities?
Appium capabilities are a set of key-value pairs used to configure and start an Appium session. They specify details like the mobile operating system, device name, app path, and other parameters necessary for the test environment. These capabilities help Appium understand how to set up and run your automated tests.
Here’s the documentation for reference: https://appium.io/docs/en/2.1/guides/caps/
Create general_store.py file for application functions
from appium.webdriver.common.appiumby import AppiumBy
from appium import webdriver
from Locators import *
class store_class:
def __init__(self, general_store_fixture):
self.driver= general_store_fixture
self.loc= general_store_locators
def field_validation_negative_case(self):
self.driver.find_element(by=AppiumBy.XPATH,value=self.loc.Female).click()
self.driver.find_element(by=AppiumBy.XPATH,value=self.loc.Male).click()
self.driver.find_element(by=AppiumBy.XPATH,value=self.loc.lets_shop).click()
name_error_text=self.driver.find_element(by=AppiumBy.XPATH,value=self.loc.name_error).text
assert name_error_text == "Please enter your name"
def field_validation_positive_case(self):
self.driver.find_element(by=AppiumBy.XPATH, value=self.loc.name).send_keys("Abbas")
self.driver.find_element(by=AppiumBy.XPATH, value=
self.loc.lets_shop).click()
self.driver.find_element(by=AppiumBy.XPATH, value=
self.loc.back_btn).click()
- field_validation_negative_case: check the error, it should be “Please enter your name” when name field value is skipped.
- field_validation_positive_case: it will check if the field is entered and the user goes on the next page after clicking the “Lets Shop button”.
Add all Locators in a separate python file Locators.py. We are using POM structure in our script, so debugging the Error/issues can be better managed.
class general_store_locators:
name="//android.widget.EditText[@resource-id='com.androidsample.generalstore:id/nameField']"
lets_shop="//android.widget.Button[@resource-id='com.androidsample.generalstore:id/btnLetsShop']"
back_btn="//android.widget.ImageButton[@resource-id='com.androidsample.generalstore:id/appbar_btn_back']"
#gender
Female="//android.widget.RadioButton[@resource-id='com.androidsample.generalstore:id/radioFemale']"
Male="//android.widget.RadioButton[@resource-id='com.androidsample.generalstore:id/radioMale']"
We use the Pytest framework in our script, creating main test_cases.py file and import the classes and functions from general_store.py
import pytest
# Main test case file
from general_store import *
def negative_case(general_store_fixture):
store= store_class(general_store_fixture)
store.field_validation_negative_case()
def positive_case(general_store_fixture):
store= store_class(general_store_fixture)
store.field_validation_positive_case()
if __name__=='__main__':
pytest.main()
For Additional information:
Write a Test (Python) – Appium Documentation
Free Video: Mobile Automation Testing With Python and Appium from YouTube | Class Central
Appium with Python: Getting Started with App Automation Testing | BrowserStack
Automated Testing With Appium and Python: A How-To Guide | Waldo Blog