This Tutorial will Explain how to Automate a Native App on an Android Device. You will also Learn to Interact with Elements using UIAutomator Viewer:
To automate a native application on Android device, please follow the steps listed below:
- Connect the mobile device or Create an Emulator
- Get details of the mobile device
- Get details of the app which is to be launched
- Start Appium Server
- Write Appium test script
- Interact with elements using the UIAutomator Viewer
- Run the script and automate the app
What You Will Learn:
- Automate Native App On Android Device
Automate Native App On Android Device
Here are the Video Tutorials:
Appium – Automate Native App on Android Device – Part – I
Appium – Automate Native App on Android Device – Part – II
Steps To Automate A Native App
#1) Connect The Mobile Device Or Create An Emulator
- Connect the real-time mobile device using a USB cable or create a virtual device in the system.
(Note: In order to create an emulator of a device, you can use AVD manager from Android SDK bundle or GenyMotion software)
- Now open a command prompt and run this command: adb devices
- Once you run this command, the device name will be listed in the response as shown in the image (if you get a response such as “daemon not running. daemon started successfully”, then try to run the command again).
- Note down the device ID. From the above example, the device Id/Name is: AVY9KA9632202030
#2) Get Details Of Mobile Device
- In your mobile device, go to Settings>About Phone.
- Please note the Android Version as shown below.
#3) Get Details Of App To Be Launched
- Install the application in the device/emulator.
There are two ways to install the application
- Through play store: Install directly from the PlayStore.
- Using apk file: Every application has an ‘apk’ file through which it is installed. One can directly install the application in the device using the apk file.
To install the application from the system command line,
- Open a Command prompt and navigate to the location where the apk file is placed.
- Then type >adb install example.apk, where example is the filename.
While running the adb command, wait for the application to get installed. You will get a Success message and also the application will appear on the device listed, as shown in the screenshot.
- Get application details (package name, activity name)
Once the application is installed, get the below details :
Application details like Package name and Application activity name, as these would be used by Appium to identify the application.
appPackage is the technical Java name of the application which is provided by its developers. It is a top-level package where all the code for the application resides.
For Example, appPackage name for ‘YouTube’ – Android is ‘com.google.android.youtube’. Similarly for WhatsApp, the appPackage is – ‘com.whatsapp’. So if you want to launch Whatsapp from Appium, it is required to provide its appPackage name as ‘com.whatsapp’ in Appium.
appActivity refers to the different functionalities that are performed by the app.
For Example, WhatsApp provides multiple functionalities such as conversations, video calls, profile information, setting profile photos, changing status, setting notifications, etc. All these functionalities are performed by different appActivity. Together with these activities, every application has a main activity which is the main screen you see when you launch the app.
For Example, For WhatsApp, the main screen is it is the Chats window, and for Facebook, the main screen is the Wall where we see posts.
So, when you launch the app with Appium, it needs to know which activity has to be launched. And you would need to provide the main activity name.
To get the Application Package name and Activity name:
- Open the application in the mobile device.
- Open a command prompt and type > ‘adb shell’ in command line.
- Type the shell command as follows > dumpsys window windows | grep -E ‘mCurrentFocus|mFocusedApp’
- It returns the details in its response as shown in the screenshot which holds the Package name and Activity name of the application
appPackage starts with com. and ends before backslash (/). So from the above example, appPackage name is – com.google.android.youtube
appActivity starts after the backslash (/) and goes till the end. From the above example, the appActivity name is – com.google.android.apps.youtube.app.WatchWhileActivity
Note down the Package name and Activity name of the application.
#4) Start Appium Server
- Open Appium Desktop in the system. It might take a while to open.
- Once Appium Desktop opens, click on Settings Tab.
- Note down the server address and port number.
- Click on the Play button to start the Appium Server.
- Once the server is launched, you will get a success message saying ‘Welcome to Appium….’
#5) Write Appium Test Script
- Set Initial configuration to start Appium.
Desired Capabilities, Properties of Device, Application, and Browser are defined.
RemoteWebDriver driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub”), cap);
An object of RemoteWebDriver is initiated since automation is to be run on a remote device, not on the local computer.
URL of the Appium Server is passed along with the device and application details.
At runtime, the driver is initiated and Appium is connected using the Server address (http://127.0.0.1:4723/wd/hub). Also, all other device details are read from capabilities(cap) with which Appium will create a session for automation.
#6) Interact With Elements Using UIAutomator Viewer
In order to inspect elements, we have a tool called UIAutomatorViewer in the Android SDK package. This is a tool provided by Android SDK that lets you inspect elements in your mobile app.
UIAutomatorViewer comes with an Android SDK bundle. So, when you install Android SDK, UIAutomatorViewer would automatically get installed with it. There are 2 ways in which you can open UIAutomatorViewer.
- Open UIAutomator Viewer From Andoird SDK Bundle:
- To open the tool, navigate to the location where Android SDK is installed.
- Once the batch file is run, the tool gets opened.
- Open UIAutomator Viewer From Command Prompt:
- Go to the command prompt.
- Type >uiautomatorviewer and hit Enter.
How to inspect Mobile Elements using UI Automator Viewer:
- Make sure the mobile is connected to the system.
- Open the application on the mobile device.
- To inspect elements on the active mobile screen, click on Icon in the UIAutomatorViewer tool on the top left, to capture screenshot of the active screen on the device.
- The icon on the left takes a screenshot of the screen currently active on the mobile device and its properties are populated in the tool with which each element can be identified.
a) You will find the screenshot of the mobile device in the left pane, and the right pane will hold the node structure and properties of each element/node.
b) As and when you hover on each element in the screenshot, its attributes get listed in the right pane with its corresponding value. With this information, each element can be identified either by using Id, ClassName, Xpath, etc.
c) Once an element is identified using any of the identification criteria, it can be used in the automation script to perform actions.
WebElement Video1 = driver.findElementByXpath(//android.widget.ImageView[contains(@package,’youtube’)];
In this way, elements can be recognized and interacted for automation.
- There are various Locators used for identification.
See an example of inspecting an element as highlighted below:
Locating Android App Element By Name
- text attribute can be used as “name”
WebElement SearchBox = driver.findElement(By.name(“Search”));
Locating Android App Element By Id
- resource-id attribute can be used as “id”
WebElement SearchBox = driver.findElement(By.id(“com.pinterest:id/search_tv”));
Locating Android App Element By ClassName
- class attribute can be used as “className”
WebElement SearchBox = driver.findElement(By.className(“android.widget.TextView”));
Locating Android App Element By Content-Desc
- content-desc attribute can be used as “AccessibilityId”
WebElement SearchBox = driver.findElement(By.AccessibilityId(“Search”));
Along with the above attributes, we can write xPaths for object identification.
Locating Android App Element By Xpath
There are three ways of creating an Xpath for identifying an element in the Native app.
- XPath using class and any attribute: In the above image you can see that the Search box has text attribute with value = ‘Search’ and Class name is android.widget.TextView. So we can create XPath using text attribute value with class name as below.
//className[@attributename=’attributevalue’] or //className[contains(@attributename,’attributevalue’)]
xpath("//android.widget.Button[@text='Search']") //Here we have used equals relation xpath("//android.widget.Button[contains(@resource-id,'Search')]") //Here we have used contains relation
We can also use a combination of one or more attributes along with the class name as :
//className[@attributename=’attributevalue’ and @attributename2=’attributevalue2’]
xpath("//android.widget.Button[contains(@resource-id,’Search’) and @text='Search']")
- XPath using className alone
Here Class name is android.widget.TextView. So we can create XPath value with class name as below.
- XPath using any Attribute without ClassName:
We can create XPath with attribute values alone. Here, we selected two attributes, say resource-id and text, then XPath is created as below.
//*[@attributename=’attributevalue’] or //*[contains(@attributename,’attributevalue’)]
Where * – represents an element in any node/class.
xpath("//* [contains(@resource-id,'Search') and @text='Search']")
#7) Run The Script And Automate The App
- Run the script as a Java application or TestNG application from eclipse.
- After execution, you can see that the Appium files logs for all actions performed against the mobile device.
The log holds details as:
- Appium session is established.
- Device details, Application details are recognized and the connection is made.
- Actions in the script are executed and responded to the client with a success message.
We have seen all the steps required to automate a native app on an Android device i.e on a real-time mobile device and also learned to locate an element in a native application.
In the next chapter, we will focus on automating a Web application on Android Chrome Browser.