Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

APPIUM

What is Appium?

APPIUM is a freely distributed open source mobile application UI Testing framework.


Appium allows native, hybrid and web application testing and supports automation test on
physical devices as well as an emulator or simulator both. It offers cross-platform application
testing, i.e. single API works for both Android and iOS platform test scripts.

It has NO dependency on Mobile device OS. Because APPIUM has framework or wrapper


that translate Selenium Webdriver commands into UIAutomation (iOS) or UIAutomator
(Android) commands depending on the device type, not any OS type.

Appium supports all languages that have Selenium client libraries like- Java, Objective-
C, JavaScript with node.js, PHP, Ruby, Python, C#, etc.

In this tutorial, we will learn about

 How APPIUM Works?


 Prerequisite to use APPIUM
 Install Appium Desktop:
 APPIUM Inspector
 Attach Android Emulator to Appium
 APPIUM Test Case for Native Android App(Calculator)
 Limitations using APPIUM
 Common Encountered Errors and Troubleshooting Steps in Appium

How APPIUM Works?

 Appium is an 'HTTP Server' written using a Node.js platform and drives iOS and an
Android session using Webdriver JSON wire protocol. Hence, before initializing the
Appium Server, Node.js must be pre-installed on the system.
 When Appium is downloaded and installed, then a server is set up on our machine
that exposes a REST API.
 It receives connection and command request from the client and executes that
command on mobile devices (Android / iOS).
 It responds back with HTTP responses. Again, to execute this request, it uses the
mobile test automation frameworks to drive the user interface of the apps. A
framework like:-
o Apple Instruments for iOS (Instruments are available only in Xcode 3.0
or later with OS X v10.5 and later)
o Google UIAutomator for Android API level 16 or higher
o Selendroid for Android API level 15 or less

Prerequisite to use APPIUM

1. Install ANDROID SDK (Studio)[Link]-


2. Install JDK (Java Development Kit) [Link]
3. Install Eclipse [Link]
4. Install TestNg for Eclipse [Link]
5. Install Selenium Server JAR [Link]
6. Appium Client Library[Link]
7. APK App Info on Google Play [Link]
8. js (Not Required - Whenever Appium server is installed, it by default comes with
"Node.exe" & NPM. It's included in Current version of Appium.)
9. Install Appium Desktop

Install Appium Desktop:

Appium Studio is an Open source GUI app to install Appium Server. It comes bundled with
all the pre-requisites to install and use Appium Server. It also has an Inspector to get basic
information on your Apps. It comes with a Recorder to create boilerplate code to automate
your mobile apps.

Step 1) Go to http://appium.io/ and click on Download Appium.

Step 2) For Windows, select the exe file and download. The file is around 162MB will take
time to download based on your internet speed.
Step 3) Click on the downloaded exe.

Step 4) On a Windows machine, there is no need to install Appium. It runs directly from the
exe. Once you click the exe you will see the following image for few minutes.

For Mac, you need to install the dmg


Step 5) Next you will see the Server Start Window. It populates the default host and port
option which you can change. It also mentions the version of Appium being used.

Step 6) On clicking the Start Server Button, a new server is launched on the specified host
and port. Server log output is shown.

Step 7) Click New Session Window.


Step 8) You can enter the Desired Capabilities and start a session.

APPIUM Inspector

Similar to Selenium IDE record and playback tool, Appium has an 'Inspector' to record and
Playback. It records and plays native application behavior by inspecting DOM and generates
the test scripts in any desired language. However, currently, there is no support for Appium
Inspector for Microsoft Windows. In Windows, it launches the Appium Server but fails to
inspect elements. However, UIAutomator viewer can be used as an option for Inspecting
elements.

Steps to start with Appium Inspector on Mac machine:-

Step 1) Download and start your Appium server with the default IP Address 0.0.0.0 and the
port 4725.

1. Select the source file or .app files from local to test.


2. Check the 'App Path' Checkbox to enable 'Choose' button.
Step 2)Now, click on 'Choose' button will give the option to browse and select test file from
the local drive.

Step 3) Start Simulator on Mac machine.

Step 4) Click 'Launch' button from a top right corner, which enables a blue color icon. Again,
click on this blue color icon, it will open the Appium inspector and Simulator with a pre-
selected application.

Step 5)- Launching your Appium Inspector will show the element hierarchy in column-wise
structure. Also, a user can apply actions using buttons like Tap, Swipe, etc.
Step 6) Click on 'Stop' button to stop recording.

Attach Android Emulator to Appium

Step 1) Install Android SDK in your system.

Go to Control panel >> System and Security >> System and from the left panel click on
'Advanced System Settings'. From 'System Properties' pop up, click on 'Advanced' tab and
then click on "Environment Variables" button.
Step 2) Now, from 'Environment variables' pop up, 'double click on 'Path' and set
ANDROID_HOME variable that point to your SDK directory. In the path append the whole
SDK folder path.

e.g. -

C:\User\ABC\Desktop\adt-bundled-windows-x86_64-20140321\sdk
Step 3) Start your Android emulator or any attach any Android device to your system (Make
sure you have Android Debugging option enabled in your Android device. To check
Debugging Option. Go to Device Settings >> Developer Options >> Check "Debugging
Option").

Step 4) Open Command Prompt and navigate to your Android SDK's \platform-tools\
directory (E.g. D:\adt-bundle-windows-x86_64-20130514\sdk\platform-tools).

Step 5)- Run 'adb devices' command. You can see your connected device listed in Command
Prompt window. (In CMD write '>adb devices'- This command will list the connected
emulator instances. E.g.: adb –s emulator-5554 install <Location of .apk file>)
Step 6)- Run 'adb start-server' command. It will start ADB server that will be used by
Appium to send commands to your Android device.

Step 7) Now, navigate to Appium directory in your system and start Appium by clicking an
Appium.exe file.

Step 8) Do not alter the IP address or port number and click 'Launch' button. Your Appium
console starts at 127.0.0.1:4723 as shown in below.

Step 9) Click on 'Start' button, Appium server started running on your system.
APPIUM Test Case for Native Android App(Calculator)

Step 1)) Download ADT eclipse plugin or download ADT bundled separately here

Step 2) Open Eclipse and Create a new Project >> Package >> Class

Step 3) Import Selenium library and Testng inside that new project.

Step 4) Now Create a small test Program for 'Calculator.app' to sum two numbers.

package src_Appium;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.*;

public class Calculator {


WebDriver driver;

@BeforeClass
public void setUp() throws MalformedURLException{
//Set up desired capabilities and pass the Android app-activity and app-package to
Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "4.4.2");
capabilities.setCapability("deviceName","Emulator");
capabilities.setCapability("platformName","Android");

capabilities.setCapability("appPackage", "com.android.calculator2");
// This package name of your app (you can get it from apk info app)
capabilities.setCapability("appActivity","com.android.calculator2.Calculator"); //
This is Launcher activity of your app (you can get it from apk info app)
//Create RemoteWebDriver instance and connect to the Appium server
//It will launch the Calculator App in Android Device using the configurations specified in
Desired Capabilities
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void testCal() throws Exception {
//locate the Text on the calculator by using By.name()
WebElement two=driver.findElement(By.name("2"));
two.click();
WebElement plus=driver.findElement(By.name("+"));
plus.click();
WebElement four=driver.findElement(By.name("4"));
four.click();
WebElement equalTo=driver.findElement(By.name("="));
equalTo.click();
//locate the edit box of the calculator by using By.tagName()
WebElement results=driver.findElement(By.tagName("EditText"));
//Check the calculated value on the edit box
assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match
with expected value: 6";

@AfterClass
public void teardown(){
//close the app
driver.quit();
}
}

Appium Server and Android Emulator from 'AVD Manager' and Click Run >> TestNG.
Above program will run the 'Calculator.app' on selected emulator and Result displayed under
Eclipse console using a TestNG framework.

Limitations using APPIUM

1. Appium does not support testing of Android Version lower than 4.2
2. Limited support for hybrid app testing. e.g.: not possible to test the switching action
of application from the web app to native and vice-versa.
3. No support to run Appium Inspector on Microsoft Windows.

Common Encountered Errors and Troubleshooting Steps in Appium

Error Troubleshooting Steps


1. error:- The following desired capabilities are 1. Add desired capabilities: device Name, platformName in A
required, but were not provided: device Name, e.g:capabilities.setCapability ("deviceName","Emulator");
platformName capabilities.setCapability ("platformName","Android");
2. error: Could not find adb. Please set the
2. You probably need to set up SDK root directory path in sys
ANDROID_HOME environment variable with
'Environment Variables' in 'Path' column
the Android SDK root directory path.
3.error:org.openqa.selenium.SessionNotCreate 3. You need to set a correct App path and restart the Appium
dException: A new session could not be
Error Troubleshooting Steps
created.
4. How to find DOM element or XPath in a
4. Use 'UIAutomatorviewer' to find DOM element for Androi
mobile application?

UIAutomatorViewer Tutorial: Inspector for Android Testing

What is UiAutomatorViewer?

"UIautomatorviewer" is a GUI tool to scan and analyze the UI components of an Android


application.To automate any android application using Appium, a user needs to identify the
objects in AUT (Application under test). With "UIautomatorviewer" you can inspect the UI
of an android application to find out the hierarchy and view different properties (id, text…) of
the element.

While executing automation scripts, Appium uses "Uiautomatorviewer" to identify different


properties of the object and use the properties to identify the required object.
In this tutorial, you will learn-

 How to Download & Install UIAutomator


 How to use Uiautomatorviewer to find objects in my application
 How to use these properties to identify elements for automation
 Error one might encounter while using Uiautomatorviewer

How to Download & Install UIAutomator

"Uiautomatorviewer" is a part of the Android SDK manager and will be accessible once you
install the SDK manager. Download and install Android SDK manager from here

Once Android SDK installed, navigate to link

c:\users\<username>\AppData\Local\Android\sdk\tools
You'll notice a batch file with name
uiautomatorviewer.bat
. Double click on it to launch "Uiautomatorviewer" GUI
How to use Uiautomatorviewer to find objects in my application

1. Enable "developer" options on your device. Click here to know how to enable


developer options on Android devices
2. Connect your android device to PC via USB cable
3. Select "Guru99" app from applications
4. Click the 'Device screenshot' button to refresh the "Uiautomatorviewer" and to load
the guru99 application GUI on "Uiautomatorviewer"
5. After refresh is completed, a screenshot of Guru99 application opens
6. As you see in the above image, on the right side of the window there are 2 panels.

Upper panel contains node hierarchy the way the UI components are arranged and contained,
clicking on each node gives properties of UI elements in the lower panel

7. Select 'Quiz' button in the above image to view different properties (text, resource-
id...)
How to use these properties to identify elements for automation

Well, you cannot use the properties directly, each property has other names. Let's see how to
use those properties values to work. Following attributes can be used to identify 'Quiz' button
in Guru99 app.

 text attribute can be used as "name"

 resource-id attribute can be used as "id"

 class attribute can be used as "className"

 content-desc attribute can be used as "AccessibilityId"

Along with above attributes, we can write xpaths for object identification
Error one might encounter while using Uiautomatorviewer

 I see the error- "No Android devices were detected by adb" as shown in below
screenshot how I can resolve this

Solution: Make sure your device is connected to PC


Appium Desired Capabilities for Android Emulator [Example]

This tutorial will help you to understand APPIUM automation tool. It will cover desired
capabilities and APPIUM with Maven uses.

In this tutorial, you will learn-

 What is Desired Capabilities?


 Extracting Packages & Activities information

What is Desired Capabilities

'Desired Capabilities' help us to modify the behavior of server while Automation. In Appium,
it is a type of hashmap or key-value pair, used to send a command to APPIUM server. In
APPIUM, all the client commands are running in the context of a session.

For example, a client sent POST/session request containing JSON object to APPIUM server.

Hence, to send any desired request or to maintain any desired session with the server, a set of
Key and value pair is used. This is known as 'Desired Capabilities.'

import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
}

Important Role of Desired Capability-

 'DesiredCapabilities' help the user to control the session request with the server. For
example- if we want iOS session then we might set Capability as PlatformName =
iOS. Or if we want Android session then we might set Capability as PlatformName =
Android.
 'DesiredCapabilities' are used to set up the Webdriver instance eg: FirefoxDriver,
ChromeDriver, InternetExplorerDriver etc.
 DesiredCapability is very useful for Selenium Grid. Eg: It is used to access different
test cases on a different browser and different operating system. Based on mentioned
DesiredCapability Grid, hub will point to the corresponding node. Here, these nodes
are defined using 'set' property method eg:-
 DesiredCapabilities obj = new DesiredCapabilities();
 obj.setBrowserName("firefox");
 obj.setVersion("18.0.1");
 obj.setPlatform(org.openqa.selenium.Platform.WINDOWS);
 A desired capability is a library defined package. Prior to use 'DesiredCapabilities',it
should be imported from below mentioned library
Org.openqa.selenium.remote.DesiredCapabilities

APPIUM supports both Android and iOS. Therefore there are a separate set of Appium server
capabilities.

Below table depicts some commonly used Android capabilities and its value to use-

Capabilities Description Values/Uses


Value= com.example.myapp/
Call desired Java package in android that
appPackage
user want to run Obj.setCapability("appPackage", "com.whatsapp

Value= MainActivity, .Settings


Application Activity that user wants to
appActivity
launch from the package. Obj.setCapability("appActivity", "com.whatsapp

Package from which application needs to


appWaitPackage Value=com.example.android.myapp
wait for
Value= SplashActivity
Any Android activity that user need wait
appWaitActivity capabilities.setCapability("appWaitActivity",
time
"com.example.game.SplashActivity")

NOTE- Refer this link 'https://appium.io/docs/en/writing-running-appium/caps/#android-


only' to view more Android Capabilities

Below table depicts some commonly used iOS capabilities and its value to use-

Capabilities Description
LaunchTimeout Total time (in ms) to wait for instrumentation.
UDID To identify unique device number for connected physical device

NOTE- Refer this link 'http://appium.io/slate/en/master/?java#ios-only ' to view more iOS


Capabilities

Extracting Packages & Activities information

Packages are related to bundled files or classes. It gives an organized structure to modular
programming. In Java, different packages are stored in a single Jar file. The user can easily
call the jar file for full execution. Similar concepts followed in Mobile application
development world.

In Android operating system, all applications are installed in the form of JAVA packages.
Hence, to extract packages path information, Android PackageManager class is used.

It retrieves package and activity information of pre and post installed application. It is
installed in Android devices.
You can get an instance of PackageManager class By calling getPackageManager().

This method can access and manipulate the packages and related permission of the installed
applications.

For example -

PackageManager pManager = getPackageManager();


List<ApplicationInfo> list =
pManager.getInstalledApplications(PackageManager.GET_META_DATA)

Summary:

 Desired Capability always runs on key-value pair to send command to APPIUM


Server .
 Use 'PackageManager' class to extract application information in Android.

Connect Mobile Device with Android Debug Bridge(ADB) to


USB, WiFi

What is ADB?

Using any real device for mobile automation Testing is always been a challenge for testers.
But, Android offers a handful solution to connect a real device over USB i.e. Android Debug
Bridge (ADB).
ADB is a command line tool. It is used to bridge communication between an emulator
instance (Android device) and background running daemon process (server).

In this tutorial, you will learn-

 USB debugging and ADB Configuration


 How to Connect to an Emulator
 How to Connect Android Device
 How to Configuring ADB for Wi-Fi Support

USB debugging and ADB Configuration-

APPIUM offers an advantage to execute test on real devices. But prior to run the test, we
need to setup following pre-requisite.

 USB debugging should be enabled


 ADB configuration
 Desired capability setup as per the hardware changes.

Here we will see both, connecting to emulator as well as with real device for testing. See
steps below for connecting to an emulator.

How to Connect to an Emulator

Pre-Requisite- SDK (Software Development Kit) should be installed on the machine. ADB is
packaged with Google's Android SDK (Software Development Kit). Steps to enable ADB
from SDK Manager.

Step 1) Open Android SDK folder

Step 2) Double click on SDK Manager

Step 3) From the list of all packages select Tools and mark the checkbox for

 Android SDK Tools and


 Android SDK Platform-tools.
How to Connect Android Device

Step 1) Enable USB debugging option from 'Developer Option' in Android phone.
Step 2) Open the local folder where Android SDK files has been saved 'Android SDK >>
Platform-tools' eg: C:\android-sdk\platform-tools

Step 3) Inside folder hold Shift + Right click menu >> Select 'Open command window
here' option.

It will open the folder using command prompt.

Note- you can also open the folder path directly from the Run command in command prompt.
This command window will directly open the folder in command prompt window.

Step 4) Now, prior to check the device, user have to connect an external Android device
(mobile phone). To connect use the device USB cable connector to the system. Then in above
command prompt type command-

'adb devices' & press Enter

It will display all list of all the connected devices.


But, prior to this we should check that ADB server. Check whether it is running as
background process or not. Just open the command prompt from above mentioned procedure
and write 'adb' and press enter. It should display all the adb's process running.

When server starts, it always bind the local TCP port 5037. All ADB clients listen to 5037
TCP port to communicate with server request.

Now, the running ADB server can scan all connected emulator or device instances by
scanning the port.

Always remember that ADB daemon runs on odd numbered port between the ranges of 5555
to 5558.

ADB daemon process runs with console connection that acquires even number port for
connection.

For example: If single device connected then server automatically scan the device and get
connected but if multiple device or emulator running the user need to give ADB command
line instruction to connect.

The emulator instance connected on odd numbered port 5557 has the same console running
over even numbered 5556 port ie. Each running devices has 1 odd and 1 even connected port.

emulator 1: console 5556


emulator 1: adb 5557
emulator 2: console 5554
emulator 2: adb 5555

Command to detect all connected device -

<$ adb devices>


emulator-5554 device
emulator-5556 device
emulator-5558 device

Command detecting a single device from multiple connected devices-

<$ adb –s emulator-5554 install Guru99.apk>


It will detect the adb connection for device -5554 and install the application.

So, this way user can set up a successful connection to access ADB instances using ADB
commands.

Syntax used to access ADB instances from commands line:


Adb [-d – An adb command when a single USB device is connected
Adb [-e – An adb command when only single emulator is running
Adb devices--- This will print all the list of emulator / devices attached.
Adb version--- List the adb version number.
Adb help---- Print the list of supported commands.

How to Configuring ADB for Wi-Fi Support

Just like configuring ADB over USB, user can also configure ADB over wi-fi.

Pre-requisite

 Both Android device and the host computer should be connected to same wireless
network and
 Device Bluetooth option should disabled.

Steps to connect-

1. Connect device using USB cable to the host computer. Confirm USB debugging is
enabled in device.
2. Set target device to connect TCP/IP on port 5555

$ adb tcpip 5555

3. Now, disconnect the USB cable from the device.


4. In Android device find the IP address from Settings >> wi-fi Setting >> Advanced >>
IP Address.
Use the same IP address to connect the device via ADB connection

eg: Network IP address is- 148.100.1.17


$ adb devices
List of devices attached
148.100.1.17:5555 device

Hence, the final configuration done and 'adb' successfully configured over wireless network.

NOTE- If any connection error occurred just reset or kill the adb host connection. For that
use following command

<adb kill server> and again connect from first step.

Appium Maven Dependency: Project Example


Apache Maven is a Java-based project management tool. It can provide a
multitasking framework for developers. It eases the complete build lifecycle.
Maven is pre-defined and declared in XML format called as POM (Project Object
Model) and referred to 'pom.xml.'

Using Maven framework, we can easily manage following tasks in any project-

 Build cycle
 Project Documentation
 Report Checks
 Scrums management
 Release Information

Basic uses of Maven are-

 Enforces a standard directory structure.


 Provide a reusable and easy to maintain project structure.
 Resolve packages dependencies.
 Provide a configuration management framework.

Download Appium Maven Dependencies

Before start writing APPIUM test with Maven, we have to download  the JAR file
from Maven central repository website.

or directly add below mentioned POM.xml artifact:

<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.4.1</version>
</dependency>

Please go through our Maven tutorial to learn how to configure Maven with


Eclipse.
Testing Application with APPIUM and Maven
After configuring Maven plug-in Eclipse. It will be ready to test any android .apk
application with Appium and Maven.

Step 1) In this step,

1. Go to NEW >> select Maven project


2. Click on 'next' button
Step 2) Then in 'New Maven Project' window, enter 'Appium Test' in Group Id and
Artifact Id column. In this step, you have to enter.

1. Group Id
2. Artifact Id
3. Version
4. Packaging
5. Name and Description
6. Finish
Clicking on Finish button. It will open a new class on the defined Group Id
(AppiumTest) name.

Step 3) To start with Appium script. Right click on 'src/main/java' from left side
explorer window. Then select New >> class. Write the Appium code inside the
selected class.
Step 4) In the same project, click over pom.xml from left explorer menu. All
dependencies will be visible by default in 'pom.xml' tab. Refer to the Image below-

If in the case of default pom.xml does not exist then just add all Maven Appium
dependencies. (extracted from Maven central repository website}
http://search.maven.org/#search|gav|1|g%3A%22io.appium%22%20AND
%20a%3A%22java-client%22

Step 5) Now, right click on 'pom.xml' from left explorer or over the xml code for
'AppiumTest' project. Then click 'Run As >> Maven Clean' option.
While running, the user could see all Maven related jar files and success message.
Hence, this way the user can run APPIUM test with Maven configured
environment.
Summary:

 Maven is a JAVA based Project Management Framework.


 It is pre-defined and declared in XML format called as POM (Project Object
Model) and referred to 'pom.xml'.

You might also like