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.
Appium Server can be installed using two ways explained below.
- Download Node
- Double click the downloaded file and follow the steps in the wizard to finish the installation of Node.
- Once NodeJS is installed execute, npm install -g appium
- To run Appium Server execute, appium
Using Appium Desktop
- Download the Appium Desktop
- Launch the Installer of the platform of your choice and follow the setup wizard.
- 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.
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.
- 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.
- Launch the executable installer and Click Install Now. Check the ‘Add Python<version> to Path’ checkbox if you prefer the Python to be added to your environment path.
- Follow the setup wizard to complete the installation.
Python will get installed in the users directory, example c:\Users\\AppData\Local\Programs\Python\<python version> in Windows.
Double-check the installation by executing python in the command prompt as shown below.
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.
- Download Pycharm. Note: This blog uses the Free Community version of Pycharm
- Launch the executable installer.
- Follow the setup wizard by clicking the Next button and finally click Install to complete the installation.
We need to configure the Python interpreter in the Pycharm, so that any project inherits the interpreter by default.
- Launch the Pycharm
- Click Configure > Settings as shown below.
- This will open up the Settings screen.
- 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.
- In the next screen, click OK and Apply to finish the configuration. This screen displays the available packages in the selected Python interpreter.
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.
- The latest version of Java, needed for Android Studio.
- 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.
- Download sample Android application called Eribank using the link https://experitest.s3.amazonaws.com/eribank.apk
- Installation of the latest version of PyCharm.
- Connecting the Mobile device to the PC using USB cable and enabling the developer mode/USB debugging in the Android device.
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.
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.
Name the Project and click Create to create 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.
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.
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
This will open a dialog box, Specify a test name and click the Python unit test.
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.
# This is the Application and ‘app’ desired capability to specify a path to Appium.
self.dc['app'] = "c:\\eribank.apk"
# appPackage and appActivity desired capability specify app details to Appium
self.dc['appPackage'] = "com.experitest.ExperiBank"
self.dc['appActivity'] = ".LoginActivity"
# platformName desired capability specify platform detail to Appium
self.dc['platformName'] = 'Android'
# deviceName desired capability specify the device id detail to Appium
# device id is got from running adb devices command in PC
self.dc['deviceName'] = 'a3ae1c63'
# Creating the Driver by passing Desired Capabilities.
self.driver = webdriver.Remote("http://localhost:4723/wd/hub",self.dc)
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.
def testFirstAutomationTest(self): if len(self.driver.find_elements_by_xpath(“//*[@text=’OK’]”)) > 0 : self.driver.find_element_by_xpath(“//*[@text=’OK’]”).click();
# Find location of Elements and perform actions.
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()
Release the Driver
# Function to Release the driver
def tearDown(self): self.driver.quit()
Copy the above functions to the Python test we created before. The final code will look like this.
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.
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.
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.