You already know how to Scroll using Android driver. Now let’s see how to Swipe. Generally while swiping on a device, we move our finger to some X&Y coordinates. We can use TouchAction class to swipe here and there. Also we can use Android Driver function ‘swipe‘ by using screen width and height and we can also use javascript executor to swipe. Here I’m not using any app for demonstration.
Let’s quickly take a look at the code:
ANDROID
Scrolling Gesture – Appium
You already know how to perform longpress gesture using Appium. Now let’s see how to perform a Scroll. I hope you know what is a ‘scrolling‘. We do it often in day to day life. As a quick reference swiping your finger upwards will perform a scroll down and swiping down your finger will perform a scroll up. Just like ‘TouchAction‘ class we also a ‘scrollTo’ function in-built. Lets quickly take a look at the syntax & Example ‘driver.scrollTo(“sampleScroll”); ‘
Long Press Gesture – Appium
You already know, how to launch a web browser using Appium and launching a Native App using Appium. Now lets talk about user interactions. Just like in Selenium – Actions class, here we have TouchAction class to perform Mobile gestures like long press, drag and drop, swipe, and zoom etc. In Android by pressing an element for much long we will get other information about a feature. Let’s say, to uninstall an app we generally longpress on the app icon till we see a delete icon on the screen. So these kinda operations can be handled by ‘TouchAction‘ class
Launching Native App in your Android device using Appium
You already know what are Desired Capabilities and how to launch a web browser from previous article. Now lets launch a native app. You also know how to create an AVD, if not then take a look at this article. Get the device name using the command ‘adb devices‘. Here, we are going to take an example of the Android calculator app.
Things to do:
Launch emulator
Launch Appium server – type ‘appium‘
Get the device name – type ‘adb devices‘
Get the package name and activity name of calculator app – refer article
Set the capabilities (package name and activity name) – refer article
Launching a web browser in your Android device using Appium
From the previous article you know what are Desired capabilities. Now we will quickly launch a browser using in your device. Before diving in, you need to connect your device to the system (Debugging – On). And get the device name using the command ‘adb devices’. By default, Appium will run on port 4723. You can change it as per your need. We will see that later. Just copy and paste the below code, I will explain code after the execution.
Things to do:
Connect your device or launch emulator
Get the device name – type ‘adb devices‘
Set the capabilities – refer article
Launch Appium server – type ‘appium‘
Finding Package Name, Activity Name of .apk file
Objective: Finding Package Name, Activity Name, Device Name
As you know, to start a session we need to set Desired Capabilities. Among those, we need to set Package name and Activity name capabilities as well. There are several methods to find package and activity name.
* Using Apk.info app
* By extracting AndroidManifest.xml
* By using commands in Terminal
* By using UIAutomator
Finding elements using locators in Appium
As shown in the previous article, we can identify elements of Native & Hybrid apps using UIAutomatorviewer. There are few locators used to locate elements for native and hybird apps. Okay let’s look into it. Before that launch your emulator or connect real device. If you don’t know how, check my article. We’ll locate elements of Calculator app. Now Launch UIAutomatorviewer and capture the screenshot of calculator app.
Finding elements using UIAutomatorViewer
Just like in Selenium there are few locators to identify elements in mobile apps. Generally, to find the elements we use UIautomator for native and hybrid apps. This article helps you to know about UIautomator and other various locators. As you know how to create a new AVD from my previous article, we will use an Emulator and Calculator app to identify elements in the calculator app. Within no time you’ll learn how to use UIautomator and locating the mobile app elements. Just hold tight and follow the below steps:
Desired Capabilities for Appium
Desired Capabilities got introduced in Selenium to work with lot of browser capabilities like handling browser cookies , SSL security popup blah blah.
But why are we using this in Appium?
Using Desired Capabilities we can communicate with Appium server by sending a POST request, saying that I’d like to start a session with so and so platform and platformversion, browser etc…Now the server understands it and starts a session with the capabilities which we have set. Still confused?
Desired capabilities is a JSON object (keys and values pair). So, there are few capabilities in-built. We need to set the capability name as ‘key’ and capability value as ‘value’. The capabilities keys are case sensitive. You need to follow correct syntax to set key and value. Without setting these capabilities, you can’t even establish a session.
Basic ADB commands
From the previous article we know how to use ADB to interact with devices. Now let’s see few basic commands.
We can install an app using Terminal as well as through your code. Here we will try to install an app with adb commands. Android apps basically would end with ‘.apk‘ extenstion (Application Packages).
To install an app in your device we can use the following command:
adb install [option] <path>
ex: adb install test.apk
This command will let you install an app in your emulator or real device connected to your system. There are few other switches which can be used with adb install