Get started with mobile automation: Appium & Python

Problem: There is a perception among testers that getting started with mobile automation is hard.

Why this post?

I strongly believe that a tester must have a wide range of skills. You do not need to be an expert in all of them, but it is extremely important to have contemplated a spectrum of concepts and explored a variety of tools. The context under which you test will inform you of the specific choice of testing methodology and test tools to choose. The more options you are aware of, the better your context-specific choice is going to be. In this regard, I began noticing that mobile testing has become somewhat of a niche among testers. Testers are reluctant to get started with mobile testing – probably because they think getting started involves a lot of time and effort. At Qxf2 Services, we have decided to tackle the problem head-on. This post is the second in a series of quick, easily consumable tutorials geared towards hands-on testers looking to start exploring the mobile testing landscape.

NOTE: In this post we are concentrating only on the “getting started” phase. The tools your test team uses to solve the specific and unique problems can greatly vary. There is no one silver bullet.


A little bit of history

On the eve of Christmas 2013, Selenium officially retired their own AndroidDriver and iPhoneDriver in favour of Selendroid, iosdriver and Appium. In an earlier post we explored using Selendroid and Python. In this post, we will help you get started with Appium.

Appium is an open-source tool you can use to automate mobile native, mobile web, and mobile hybrid applications on iOS and Android platforms. Appium is “cross-platform”: it allows you to write tests on multiple platforms (iOS, Android), using the same API. This enables a large or total amount of code reuse between iOS and Android test suites.


Appium setup

Here are the steps to setup Appium on Windows 7 and use it with Android emulator.
1. Download the latest Appium
I have used appium-desktop-Setup-1.5.0-ia32.exe. You can run the exe to install the appium desktop app
2. Download and Install Android Studio
Once you install Android Studio make sure the AVD Manager is also installed as we are going to run our tests on an emulator. Set ANDROID_HOME to be your Android SDK path and add the tools and platform-tools folders to your PATH variable. I could find the Android SDK installed on the below-mentioned path
$\AppData\Local\Android\Sdk

3. Install the Java JDK
Set JAVA_HOME to your JDK folder
4. Install the Python client library
There are client libraries (in Java, Ruby, Python, PHP, JavaScript, and C#) which support Appium’s extensions to the WebDriver protocol. When using Appium, you want to use these client libraries instead of your regular WebDriver client. I have used the Appium Python client available here. Assuming you have pip installed on your machine, you can use the following command to install it

pip install Appium-Python-Client

Woot! You are now set up. Next stop: launch the emulator and start the Appium server.

5. Start the Android Virtual Device (AVD) Manager
Start the Android Studio and launch the Android Virtual Device (AVD) Manager from by clicking on the link as shown in the screenshot below.
AVD_Manager

Create an emulator with the preferences you need and launch it using the start button
android_emulator
6. Start the Appium server console
Start the Appium server console by double clicking on the Appium file.

start_appium
7. Launch the Appium node server
Click on the ‘rocket’ icon to launch the Appium node server
Launch_Appium
appium_node_server_console


Your first test using Appium

For this test we will use one of my favorite Android apps – the highly recommended Chess Free application created by the UK based AI Factory. For this blog post, we will pretend that our test is to launch the application and click on “PLAY” button.

1. Obtain the .apk for the application under test
We obtained the .apk for Chess Free over here. Copy the chess application to a directory of your choice. I used ($Directory_Of_My_Choice\apps\Chess Free.apk )
2. Peek into the AndroidManifest.xml
To write the test, you need two pieces of information specific to your application:
a) Java package of the Android app you want to run
b) Activity name for the Android activity you want to launch your package

You can get this information by running the following command

ANDROID_HOME\sdk\build-tools\android-4.4.2>aapt dump badging path_to_apk_file

aapt_pkg_activity

Another approach is using the AndroidManifest.xml which is present in the root directory of all Android applications and has the information we need. You can use Android Studio to view the AndroidManifest.xml file. Go to Build/Analyze APK and select your apk. Then you can see the content of the AndroidManifset file.

package="uk.co.aifactory.chessfree" and 
android:name=".ChessFreeActivity"

3. Write the test
Create a test script (android_chess.py) in $Directory_Of_My_Choice based on the snippet below. Pay particular attention to the setup() method.

"""
Qxf2: Example script to run one test against the Chess Free app using Appium
The test will:
- launch the app
- click the 'PLAY!' button
"""
 
import os
import unittest
from appium import webdriver
from time import sleep
 
class ChessAndroidTests(unittest.TestCase):
    "Class to run tests against the Chess Free app"
    def setUp(self):
        "Setup for the test"
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '8.0'
        desired_caps['deviceName'] = 'Pixel'
        # Returns abs path relative to this file and not cwd
        desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__),'apps/Chess Free.apk'))
        desired_caps['appPackage'] = 'uk.co.aifactory.chessfree'
        desired_caps['appActivity'] = '.ChessFreeActivity'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
 
    def tearDown(self):
        "Tear down the test"
        self.driver.quit()
 
    def test_single_player_mode(self):
        "Test the Chess app launches correctly and click on Play button"
        element = self.driver.find_element_by_id("uk.co.aifactory.chessfree:id/ButtonPlay")
        element.click()
        sleep(5)
 
#---START OF SCRIPT
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(ChessAndroidTests)
    unittest.TextTestRunner(verbosity=2).run(suite)

4. Run the test
Run the script android_chess.py. The chess application is launched in the test emulator
Run_Chess_App
5. Check the result
appium_test_run

Celebrate a little – you just upgraded your skills by a little bit in a very short span of time!

There you have it! You have just run your first mobile automated test with Appium and Python. If you are looking for Python based mobile automation framework, check out our open-sourced GUI and API automation framework based on the page object model. Happy exploring from Qxf2 Services!

P.S.: As a chess fanatic, I think it is a crime to test chess applications in an automated fashion. I sincerely believe infinite manual testing is the only correct way to test chess applications 😉

P.P.S: We were pleasantly surprised by positive reaction we got to our last tutorial on mobile application testing with Selendroid. Please chime in on the direction you want our future blog posts to go. We will try our best to keep churning out useful posts for testers.


110 thoughts on “Get started with mobile automation: Appium & Python

  1. How do I test iOS App with Appium + Python .. The above example only says about the Android app testing.

  2. The Above example says about how to test with Android App, Can you please give us the link of how to do the same with iOS Mobile Application.

  3. Hi,
    I got the new project in appium automation tool, and I know only python language, is this possible to create test script with the using of python only, not in other languages.

    please share the steps, for android mobile testing.

    Regards,
    GS
    [email protected]

Leave a Reply

Your email address will not be published.