This article is targeted to the uses who wish to configure mobile automation testing with android studio. We already have an article on Appium with eclipse. Now this time we have come with more technical aspect of configuration of Appium with android studio. Users can understand how testNG annotation can be used in test scripts if you’re interested in Appium, how it works and how it is automate to mobile app then this topic just state the basics of these concepts. I assume, you might have at least basic knowledge of Appium.
It provides the cross platform and write tests in multiple platforms like iOS, Android, Windows using with the same APIs. This provides code reusability between iOS, Android and Windows test suites.
How it works?
Appium enables Android and iOS automation using Selenium WebDriver .The same WebDriver binding can be used across web and mobile and it supports software test automation on Emulator or Simulator and Physical devices too.
Prerequisite to use Appium:
This blog will let you configure the Appium over android studio using testNG framework. Once we go through the blog things will be defined more precisely but first we should configure Appium & android at our local machine.
- 1. Android studio can be downloaded from https://developer.android.com/studio/
- 2. Appium sever can be downloaded from https://github.com/Appium/Appium-desktop/releases/tag/v1.10.0
- 3. Selenium jar file can be downloaded from https://www.seleniumhq.org/
- 4. Set Java Home & Android Home from environment variable if it is not configured.
Appium Test Case for Native Android App (Calculator):
- a. Create your first test script
- b. Launch Appium Node server
- c. Execute your first test
To start with Appium we should understand with pre-requisites required for setup. Create a project in Android Studio and follow the basic tricks one after another. We need to add the jar files to our android project. You could find the folder named as libs inside folder named as app. We need to paste all jar file except selenium standalone server. Let me tell you the jars you need to add apart from selenium jar files. These jar files are easily available over internet.
For TestNG – testng-6.14.2.jar
Just follow the three step process defined in below image to add external library files to your project in android studio.
We need to perform a bit changes in build.gradle file. Junit is configured as default by android studio. We need to configure testing by committing few changes in build.gradle file.
We need to comment Junit dependencies so that your program not runs the default configuration of Junit, you could easily observe the decencies required in form of jar files through my current build.gradle .
You need to edit the run configuration so that your test runs with testNG
You need to add new configuration is of testNG type and class should be mention as you runnable class and other detail should be configured the details. Please configure the details and become ready to write your test script.
I am having two java class files under test folder inside src. I am storing locators inside Locators.java and creating test script in Appium.java. Creating different classes for different utilities is a best practice. We can easily find out the locators of web elements in browser through developer console. Finding mobile elements is bit tricky but android studio has inbuilt tool.
Let me give you a quick brief so that you can find locators. You need to open
uiautomaterviewer.bat , this tool is available inside sdk folder of android. We don’t need to perform separate installation. The general location of sdk is mentioned below; however it may vary as of your given location at time of installation of android studio.
Default location – C:\Users\Username\AppData\Local\Android\sdk\tools\bin
Your device should be recognized by adb and it should be connect in active mode then only this viewer will capture your current screen content and transform them in xml.
Now you are all set to write your first test script. Create a small test Program for calculator application on your device using Appium. I have placed all the required locator of my test script in this class. I am creating my test script on calculator app. Code has been mentioned below.
I have placed my test script inside Appium.java, it has different testNG annotations, code logics & Desired Capabilities that are require for communication with connected device. My script is written for actual device connected with machine, but you can also create it for emulators. We have choice either to run script on emulator or on actual device.
To run this program, we need to start Appium server & configure server’s host and port as passed for RemoteWebDriver in above program.
Once Appium server is done, you can run your configuration. Make sure device is connected and unlocked. appPackage Name Launch appActivity name mentioned has a match with connected device. All rest desired capabilities configured correctly.
Your first Appium test script on android studio with testNG will run perfectly. Happy testing
Aaqib Ansari is a Sr.QA Engineer currently working with QSS Technosoft. He is a technology writer in .Net, Selenium, SQL and frameworks like TestNG. He has completed his Master’s degree in Computer Science
QSS has a proven track executing enterprise level web and mobile applications for its esteemed customers. The company has a core Testing competency in place for testing Enterprise level applications. The Testing Center of Excellence (TCoE) has experienced and dedicated team of QA engineers who can efficiently perform both manual and automated testing assignmentsTo Know More...