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 first 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.
For this post, we will show you how to get started with Selendroid and Python in less than 2 hours. Selendroid is a test automation framework which drives off the UI of Android native and hybrid applications (apps) and the mobile web. Tests are written using the Selenium 2 client API. Selendroid can be used on emulators and real devices. We will show you how to setup an Android emulator, launch a browser, click a link and verify the title of the new page that loads.
STEP 1: Download and Install Android-Sdk
From Android web site, download the latest Android-Sdk. This also installs the AVD Manager, a graphical user interface for creating and managing Android Virtual Devices (AVDs).
STEP 2: Setup your environment variables
Make sure you have Java Installed on your machine. Setup the following system environment variables:
ANDROID_HOME= $YOUR_SDK_INSTALL_PATH\sdk JAVA_HOME=$YOUR_JAVA_HOME (e.g.: C:\Program Files\Java\jdk1.8.0_05)
Add ANDROID_HOME/tools, ANDROID_HOME/platform-tools and ANDROID_HOME/build-tools to your PATH environment variable.
STEP 3: Start the emulator
For this tutorial, we are choosing to use the Android emulator. If you do not have an Android device for testing, you can use an Android virtual device. After installing Android SDK and setting up your system variables, run the below command
android create avd --name Device Name --target android-19 --abi armeabi-v7a
Where “name” Name of the new AVD created, “target” Target ID of the new AVD created and “abi” The CPU/ABI to use for the AVD. You can use the below command to display the list of all available targets. You can then use this details to update the “target” and “abi”:
android list target
You can run the below command to view the AVD created:
android list avd
You can run the below command to verify that the AVD is working:
emulator -avd Default
STEP 4: Write the Python test
We are going to be using a chess website called Chessdom to write our test. We will launch a browser, click on the ‘Chess News’ link and verify the title of the page that loads. The code should look something like the snippet below
""" Qxf2 Services: Sample Python script to get started with Selendroid """ import unittest, time from selenium import webdriver from selenium.webdriver.common.touch_actions import TouchActions class FindElementTest(unittest.TestCase): def setUp(self): "Set up the Android Driver" self.driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.ANDROID) self.driver.implicitly_wait(30) def test_Launch_Chessdom(self): "Test the title of the Chess News page on Chessdom.com is correct" # Go to url:http://www.chessdom.com/ self.driver.get('http://www.chessdom.com/') time.sleep(5) #Click on the link "Chess News" ChessNews = self.driver.find_element_by_xpath("//a[@title='View all posts filed under Chess News' and text()='Chess News']") ChessNews.click() time.sleep(5) #Increase sleep time if your emulator has performance issues # Assert that the Page has title "Chess News | Chessdom" self.assertIn("Chess News | Chessdom", self.driver.title) def tearDown(self): "Tear down the test" self.driver.quit() if __name__ == '__main__': unittest.main()
STEP 5: Download and start Selendroid
Download Selendroid and cd to the folder where you downloaded the jar. Spin up a new command prompt and run the command below to start Selendroid
java -jar selendroid-standalone-0.10.0-with-dependencies.jar
Selendroid-standalone will start an http server on port 4444 and will scan all Android virtual devices
STEP 6: Run the test
You can run the test script the normal way you do. We run it via the command prompt.
You can also view the test running in the Android emulator
There you have it! A tutorial to get you started with Selendroid in under two hours.
© 2013-2018, Avinash Shetty. All rights reserved.