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.
Create an emulator with the preferences you need and launch it using the start button
6. Start the Appium server console
Start the Appium server console by double clicking on the Appium file.
7. Launch the Appium node server
Click on the ‘rocket’ icon to launch the Appium node server
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 |
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
5. Check the result
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.
Hire QA consultants from Qxf2
This is an extremely old article but as you can see, Qxf2 was an early adopter of Appium. Not only did we work with Appium, we shared our learnings with the QA community as well. This has been our culture and over the years we have improved and stayed in touch with the rapid changes in the software space. In fact, we have innovated in the QA services domain too. Qxf2 offer several top-notch, unique testing services aimed towards startups that most folks are not aware of. So, if you are working at a startup or on an early-stage product, browse our services and get in touch. After all, every team benefits from having one good technical tester!
My journey as a tester started at Sun Microsystems (now Oracle). I was part of the testing and sustaining team for the Portal Server and Identity Management products. My first assignment was to test the Rewriter module. I enjoyed understanding the big picture, writing test cases, finding bugs and sometimes suggesting the fix too! I was hooked onto testing. Testing felt natural and intuitive to me. I am technically inclined and can write automation in Java, C++, Perl and Python. I am well versed with SilkTest, Selenium, Appium and Selendroid. I am a Computer Science graduate from BITS-Pilani. I love travelling and listening to music.
What this ERROR Means in iOS:
error: Appium will not work if used or installed with sudo. Please rerun/install as a non-root user. If you had to install Appium using `sudo npm install -g appium`, the solution is to reinstall Node using a method (Homebrew, for example) that doesn’t require sudo to install global npm packages.
Please Help.
Thanks,
AJIT JADHAV.
Appium may not work if node is installed as sudo user. If you have already installed remove it using
sudo apt-get remove nodejs
sudo apt-get remove npm
These are two options to install appium
1: Easiest way is to download and run the latest appium stable build for Mac OS from here: https://bitbucket.org/appium/appium.app/downloads/
2: Compile and run appium from the source
> brew install node # get node.js
> npm install -g appium # get appium
> npm install wd # get appium client
> appium & # start appium
> node your-appium-test.js
For ubuntu
https://coderwall.com/p/rcvkrq/install-nodejs-using-homebrew-and-install-appium
and
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-linuxbrew-on-a-linux-vps
Hi Qxf2,
How to perform actions on already opened App.
When i run my test it launches the app. I dont want that, i will launch the app and will run the test.
I saw that “autoLaunch” have to be set for it, i tried as below but it didnt work.
how to set autoLaunch property in Python.?
i tried as below, but it didnt work.
def setUp(self):
“Setup for the test”
desired_caps = {}
desired_caps[‘platformName’] = ‘Android’
desired_caps[‘platformVersion’] = ‘4.4.4’
desired_caps[‘deviceName’] = ‘XT1022’
# Get the Package and Activity name to launch the Camera app
desired_caps[‘appPackage’] = ‘com.xxx.xxx.dial’
desired_caps[‘appActivity’] = ‘com.xxx.xxx.dial.MainActivity’
desired_caps[‘autoLaunch’] = False
#You may need to change the line below depending on your setup
self.driver = webdriver.Remote(‘http://localhost:4723/wd/hub’, desired_caps)
Please let me know how to do it.
Thanks in advance.
Rajendra
Rajendra,
Which version of Appium are you using?
Similar issue was reported https://github.com/appium/appium/issues/2629.
Please take a look
Hi ,
I am new to appium . I am automating IOS hybrid app on real device. In my app when click on the button in webpage it will open the native settings page(MDM profile install ).
That page giving model dialog box with install and cancel button.In Appium inspector also not loading. Unable to add the screenshots here. Please refer the the below link for more details.
https://github.com/appium/appium/issues/5474
Please help me on this ..
Thanks! This was very helpful! I have not done much automation let alone on mobile..and this just kick started me!:>
Well written and explained! Kudos!
Cheers,
Kate
As a manual QA tester looking to break in to automation (specifically Android automation), this has helped me a lot. Thanks!
Hi,
I tried running the same program on real device and getting following error. Any suggestions.
test_single_player_mode (__main__.ChessAndroidTests)
Test the Single Player mode launches correctly … ERROR
======================================================================
ERROR: test_single_player_mode (__main__.ChessAndroidTests)
Test the Single Player mode launches correctly
———————————————————————-
Traceback (most recent call last):
File “chess_and_j.py”, line 18, in setUp
self.driver = webdriver.Remote(‘http://localhost:4723/wd/hub’, desired_caps)
File “C:\Python27\lib\site-packages\appium\webdriver\webdriver.py”, line 36, i
n __init__
super(WebDriver, self).__init__(command_executor, desired_capabilities, brow
ser_profile, proxy, keep_alive)
File “C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py”, l
ine 87, in __init__
self.start_session(desired_capabilities, browser_profile)
File “C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py”, l
ine 141, in start_session
‘desiredCapabilities’: desired_capabilities,
File “C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py”, l
ine 201, in execute
self.error_handler.check_response(response)
File “C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py”
, line 102, in check_response
value = json.loads(value_json)
File “C:\Python27\lib\json\__init__.py”, line 338, in loads
return _default_decoder.decode(s)
File “C:\Python27\lib\json\decoder.py”, line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “C:\Python27\lib\json\decoder.py”, line 384, in raw_decode
raise ValueError(“No JSON object could be decoded”)
ValueError: No JSON object could be decoded
———————————————————————-
Ran 1 test in 0.141s
FAILED (errors=1)
Hi Jai,
My guesses based on your error message are
1. Appium server is running on a port other than 4723 (use netstat or TCPView on Windows to confirm)
2. localhost may not be getting resolved to 127.0.0.1 (try changing your URL from http://localhost:4723/wd/hub to http://127.0.0.1:4723/wd/hub and try)
Let me know if this resolves your issue or else we can investigate more
Hi
I am new to appium and I am running a selenium script with device connected and while running in terminal i am getting this error “Error: ENOENT: no such file or directory, scandir ‘/Documents/android-sdk-linux/build-tools’
at Error (native)” . I am running in Linux. Can you please help me out ?
Hi Rijo,
I am not sure of the path ‘/Documents/android-sdk-linux/build-tools’ which you used. You can always try “which build tools” or “which adb” to get the path
on entering password in device using appium, some times it open keypad while sometimes not , i use the code “driver.hidekeypad” its hiding the same when display, but not when it doesnt display the keypad and further code get stopped , so how to write code to search whether keypad is open or close if open then enter and go to next text else type directly and then go to next text field .
Hi Neelima,
We use try-except to hide the key board after entering text in a text field.
*Code to enter text in the text field
try:
driver.hide_keyboard()
except Exception,e:
pass
Hope this helps