Vous êtes sur la page 1sur 86

What Is Appium? Why Need Appium?

Limitations OfAppium
What Is Appium?

Appium is mobile web, native and hybrid software application


test automation tool.
It is open-source software automation tool which is useful
to automate android and IOS platform apps.
Most important thing is : Appium is cross-platform automation
tool so you can write software automation tests against iOS and
Android (multiple platforms) using same API.
cross-platform enables you large amount of code reuse
between iOS and Android test suites.
Appium support software test automation on emulator or simulator
and physical mobile devices too.
Aooium is developed on few key philosophy points : 1. Should not
have to recompile your app to automate it. 2. Should not be locked
into a specific language or framework. 3. Should be open source
and 4. Should not reinvent the wheel when it comes to automation
APIs.

Why Need Appium?


If you are mobile app software test engineer, Appium can makes your
mobile app regression testing task easy. Specially for large mobile apps
where continues apps are updating with new features and functionalities.
Another main benefit of using appium for mobile app automation is : It
supports bellow given multiple platforms and languages. Also you can
use any testing framework.
Multiple Platforms Support
Appium support bellow given different platforms.

Android
IOS

FirefoxOS

Multiple Languages Support


Appium supports bellow given languages with
WebDriver API and language-specific client libraries.

the Selenium

Java
Objective-C
JavaScript with Node.js
PHP
Python
Ruby
C#
Clojure
Perl

Also there are other advantages like no source code is needed to test app
as you can test it directly, also you can engage built in apps like camer a,
calendar
etc
in
your
test
script
if
required.
Limitations
Of
There are few limitation in appium right now.

Appium

For Android, No Support for Android API level < 17.


Script execution is very slow on IOS platform.
Gestures support is limited.
No support for Toast messages.

I think, this is enough to use any open source software automation


testing
tool
in
terms
of
usage
flexibility.
Note :Appium supports only Android 17+ API level versions. Otherwise
you need to use selendroid. You can find API level for your phone based
on its version at THIS PAGE. That means, You can use appium if your
phone
contain
Android
4.2
or
greater
version.

We will see how to execute android native app automation test in


android device and emulator using appium in my upcoming
posts.Upcoming posts will also describe you how to configure
environment in windows to use appium software testing tool.
-

Steps To Download And Install Java/JDK In


Windows
JDK is java development kit. If you are working with java software
programming language then you must need to install it as it is
prerequisite to code and run java software programs, use testng
framework or to use SDK. Many peoples don't know how to install
JDK and configure environment variable for JAVA_HOME. Bellow
given steps will guide you to install java software in windows and set
JAVA_HOME.

Step 1 : Install JDK

Go to JDK DOWNLOAD PAGE of software web application.


Click on JDK download button as shown in bellow image.

It will take you to Java SE Development Kit 8 Downloads page.


Tick "Accept License Agreement" radio button and click on .exe
link to download JDK based on your OS as shown in bellow
image. Select "Windows x86" for 32 bit system and "Windows
x64" for 64 bit system.

When you will click on link, It will show you dialog to save .exe
file.

Double click on downloaded .exe file.

It will launch JDK software installation dialog as bellow.

Click on Next button on each step using default selections. It will


take some time to install JDK and at last of installation, It will
show screen like bellow to close installation dialog.

Java is installed now. Now you need to set JAVA_HOME variable in


windows. Bellow given steps will set it.

Set JAVA_HOME Variable In Windows


After installation of java software, You need to set JAVA_HOME
variable in your windows system Steps to set JAVA_HOME are as
bellow.
Locate JDK Installation folder path
First of all you need to locate path of JDK installation folder.
For that,

Go to C:\Program Files\Java.
There will be JDK folder with version number. Example : For me
it is "jdk1.8.0_45".
Double click on It.
Copy folder path. Example : C:\P rogram Files\Java\jdk1.8.0_45

This path will be required to set environment variable as described in


bellow given steps.
Set JAVA_HOME Variable

Right click on My Computer icon which is located on your desktop


or win start.
Select Properties.

It will open system configuration dialog.


Click on Advanced system settings as shown in bellow image. It
will open system properties dialog.

Select advanced tab -> Click on Environment Variables button. It


will open Environment Variables dialog.
Under System Variable, Click on New button. It will open New
System Variable dialog.
Set Variable name = JAVA_HOME.

Set Variable Value = Path where JDK is located. For me It is


"C:\P rogram Files\Java\jdk1.8.0_45" which is located during
previous step as described above . It depends on your own java
installation path and installed version.
Close all dialog by clicking on OK buttons as described in above
image.

Set Path Variable For Java


Also you need to set JDK's bin folder path in path variable of system
variables. You will find bin folder inside your JDK folder. To set path
system variable of JDK,

Open Environment Variables dialog as described above.


Under System Variable select Path variable.
Click on Edit button. It will open Path system variable edit dialog.
At the end of Path variable value string, Put semicolon and then
put JDK folder's bin folder path.
Click on OK button to close all opened dialog. See bellow given
image for more reference.

It will set JDK bin folder path in your system environment variable.
Verify java is installed properly or not
To verify java software is installed properly or not,

Open command prompt.


Run command "java -version"

It should show your installed java version detail as shown in bellow


image. That means java is installed in your computer and you are ready
to use it.

Steps To Download And Install Android SDK


In Windows
What Is Android SDK?
Android SDK is software development kit which enables you to create
applications for android platform. For example if you wants to create
game for android phone, you need installed android SDK with java
programming. Also it provides ability to create emulators to test new
created games or any other android apps. that's it.. No need to learn more
than this for software testing engineer to test android apps using
appium.
We learnt about appium software automation testing tool in THIS
POST. Now we are going to setup environment to run software

automation tests on android platform using appium. Using appium,


We can create and run automation test for native apps and web a pps too
on android devices or emulators. Android SDK is prerequisite to run
tests on android platform using appium. So let's install android SDK in
windows platform. Please note, We have installed JDK in previous step
as JDK is prerequisite to installing SDK.
We will divide Android configuration in 2 parts
1. Android SDK Download And Installation.
2. Set ANDROID_HOME and Path environment
View NEXT POST.

variables.

Bellow given steps will show you Android SDK software download and
installation steps. My next post will show you how to
set ANDROID_HOME and Path environment variables in windows.

Android SDK Download And Installation Steps


PREREQUISITE : PREVIOUS 2 STEPS of appium tutorials should
be
completed.
First of all you need to download Android SDK for windows and then
we can install and configure it.
System Requirements

2 GB RAM minimum, 4 GB RAM recommended


Microsoft Windows 8/7/Vista/2003 (32 or 64-bit)
20 to 30 GB Hard disk space.

Download Android SDK


For downloading android SDK,

Go
to
this
software
web
application
> http://developer.android.com/sdk/index.html
Scroll down to bottom of page.

page

There will be SDK Tools Only" under "Other Download Options"


section.
Click on android-sdk_r(Version number)-windows.zip link
e.g. android-sdk_r24.3.3-windows.zip as shown in bellow image.

It will download android SDK zip file as sown in bellow image.

Put zip file in E: drive. (Note : Android SDK needs 20 to 30 GB


space on disc to store different files. So select appropriate disc
where enough space is available.)
Extract .zip file. You will get "android-sdk-windows" folder as
shown in above image.

Rename folder name from "android-sdk-windows" to "SDK" for


easy name.

So now you have E:\SDK folder which is responsible to store android


SDK related stuff.
Install Required Packages
In order to create android emulators for testing, You need to download
and install few packages. You can do it using Android SDK Manager as
described in bellow given steps.

Look inside SDK folder. There will be "SDK Manager.exe".


Execute it by double click. It will open Android SDK Manager
dialog as shown in bellow image.
Android SDK Tools package will be installed by default.
You can select your required package from list of different
packages and then click on Install packages button as shown in
bellow image to install selected packages. I have installed di fferent
27 packages as shown in 2nd image.

I have installed different 28 packages from different folders as


shown in bellow given images. You can download all bellow
mentioned 28 packages to create different emulators with different
configurations.

Note : Downloading all packages can take lot of time. It depends on


your internet speed.

Install Intel
Hardware
Accelerated
Execution
Manager
After installation of all packages, You need to install Intel Hardware
Accelerated Execution Manager to avoid error during android virtual
device creation. Following steps will install it.

At
"E:\SDK\extras\intel\Hardware_Accelerated_Execution_Manager"
path, You will find "intelhaxm-android.exe" file.
Double click on "intelhaxm-android.exe" file to execute and install
it.

My recommendation to reduce errors during android emulator


creation and launch : Later on we will create android emulator using
AVD Manager. When you create emulator, You need to set few
properties on create new android virtual device to create new android
emulator. But sometimes you will face problem like not display any
option in Target or CPU/ABI or Skin drop down. Also you may face
error on launch emulator. Reason behind this is some system image or
related files are pending to install. So, I command you to install latest
5 API full packages. That means do not select individual file but select
Tools, top 6 API level full folders and Extras folder and install them full
as shown bellow. It will install all related files of that folder and reduce
error in android emulator creation and launch.

This will reduce errors and complexity during android emulator creation
and
launch
process.
That's it.
After installation of different packages and system images, You need to
set ANDROID_HOME and Path environment variables. View next post
to know how to set it.

Set ANDROID_HOME and Path Environment


Variables For SDK In Windows
In previous post, We learnt about how to download and install android
SDK software and its different packages in windows. In order to use
android environment to run appium software automation tests, You must
need to set ANDROID_HOME and path environment variables for
describing path of android SDK folders to your operating system.
Bellow given steps will explain you how to set ANDROID_HOME

and Path Environment Variables in windows for android SDK


software.
PREREQUISITES : FIRST 3 STEPS of appium automation software
tutorial should be completed.
Set ANDROID_HOME Variable
After installation of different packages, You need to set
ANDROID_HOME and path environment variables. Follow the steps
given bellow to set it.

Open "Environment Variables" dialog from win start menu ->


Right click on My Computer -> Select properties ->Advanced
system settings -> Environment Variables button from Advanced
tab. You can view detailed steps to open Environment Variables
dialog in THIS POST.
Click on New button under User Variable table. It will open New
User Variable dialog.
Set Variable Name = ANDROID_HOME and Variable
value = E:\SDK (Path of SDK folder). Path can be different for
you as per your SDK folder location as described in previous post.
Click on OK button to close New User Variable dialog as shown in
bellow image.

Set Path Variables

Open SDK Folder from E: drive.


You will find "tools" and "platform-tools" folders inside SDK
folder.
Copy
path
of
both
these
folders.
e.g. E:\SDK\tools and E:\SDK\platform-tools\
Open "Environment Variables" dialog as described above.
Locate Path variable line under System Variables table.
Edit Path variable by clicking on Edit button. It will open
Edit System Variables dialog.
Append "tools" and "platform-tools" folder's full path at the end of
line
as
shown
in
bellow
image.
e.g. ;E:\SDK\tools;E:\SDK\platform-tools\;

Now close all dialog by clicking on OK buttons of all opened


dialog as shown in above image.

Verify Android Is Installed And Configured


To check if android is configured properly or not,

Properly

Open command prompt.


Type command android as shown in bellow image.
It will open Android SDK Manager dialog as shown in bellow
image.

That

means

android is

configured properly in

your system.

Now ANDROID_HOME and Path Environment Variables are set for


android SDK in windows environment. So android environment is
configured and ready to use with appium to execute software automation
tests. Next post will describe you how to integrate SDK with eclipse
using Eclipse ADT Plugin to use android SDK with eclipse.

Steps To Install Eclipse ADT Plugin To Use


Android SDK With Eclipse
Earlier we learnt how to install android SDK in THIS POST and set
ANDROID_HOME and Path variable in THIS POST to set android
development and app test environment in windows. Now this is time
to integrate android SDK with eclipse IDE to access SDK tools in
eclipse IDE. Android offers plugin called Android Developer Tools
(ADT) to integrate android development environment in eclipse

IDE. After installing ADT in eclipse, You can setup android project,
debug apps and build apps.
However we CAN use it to launch android emulators through eclipse to
run automation tests on it using appium. Also you can launch android
virtual devices(Emulator) direct from SDK folder -> AVD Manager.exe.
So this is optional step but still let me describe you how to do it if you
wish to launch AVD Manager from eclipse.
Download And Installing ADT plugin in eclipse
Bellow given steps will describe you how to install ADT plugin in
eclipse.
PREREQUISITES
Bellow given tools/components should be available/installed

Eclipse IDE -> VIEW POST.


JDK -> VIEW POST.
Android SDK -> VIEW POST.

Steps to install ADT plugin in eclipse IDE.

Open eclipse IDE


Go to Help -> Install New Software. It will open Install software
dialog.

Click on Add button as shown in bellow image. It will open Add


Repository dialog.

Set URL "https://dl-ssl.google.com/android/eclipse/" in Location


text box and click on OK button as shown in above snap. It will
load Developer Tools with check box.
Select all check box of developer tools as shown in bellow image
and click on Next button. It will take you to installation details.

Click on Next button as shown in bellow image. It will take you to


Review Licenses screen.

On Review Licenses screen, Select I accept the terms of the license


agreement option and click on Finish button as shown bellow.

It will start installing ADT plugin as shown bellow.

On completion of ADT plugin, It will ask you to restart eclipse


IDE. Click on Yes to restart eclipse IDE.

ADT Plugin is installed in eclipse


Set SDK Location

You need to set SDK folder path after installation of ADT plugin which
enables eclipse to integrate with android development environment.
Follow the steps given bellow.

Open eclipse
Preferences.

Select Android on Preferences dialog.


Set SDK folder path in SDK Location box. SDK folder is located
in my E: drive which contain all android SDK related stuff.
View THIS POST for more detail on SDK folder.

IDE Preferences

dialog

from Windows

->

Verify Android SDK configured properly with eclipse


To verify if android SDK is integrated properly or not

Go to Eclipse IDE's Windows menu -> Select Android SDK


Manager.

It will open Android SDK Manager dialog as shown bellow.

This confirms that android SDK is integrated properly with eclipse IDE
using ADT plugin.

Install Other Required Software ForAppium


Configuration
In previous steps, we learnt how to integrate android SDK with eclipse
using ADT plugin. Before installing appium, Please make sure bellow
given software are installed in your system. If bellow given software are
not installed in your system then it can create problem on any stage
of android app automation testing process. So I recommend you to
Install Microsoft .Net Framework, Node JS and PDANet+ for
Android(optional) in your system. Please me show you quickly how to
install them.
PREREQUISITES : All previous 5 STEPS of appium environment
configuration should be completed.
Download And Install Microsoft .Net Framework
If Microsoft .Net Framework Is not installed then follow bellow given
steps.

Download
To download Microsoft .Net Framework,

Search for "download latest version of Microsoft .Net Framework"


keyword In google.
It will show you latest version download link as bellow.

It will take you to Microsoft .Net Framework latest version


download page.
Current latest version is Microsoft .NET Framework 4.5.
Click on download button. It will download .exe file to install
Microsoft .Net Framework(e.g. dotNetFx45_Full_setup.exe).

Install

Double click on .Net framework installation .exe file.


It will open .Net framework installation dialog.

Proceed for installation with default selected options and settings


on each screen of installation dialog.

Download And Install Node JS


Follow bellow given steps to download and install Node JS
Download
To download Node JS

Go to Node JS DOWNLOAD PAGE.


Download Windows Installer (.msi) file as per your system(32 bit
or 64 bit).

It will download Node JS installation .msifile(e.g. node-v0.12.7x86.msi).

Install

Double click on Node JS installation .msi file.


It will open Node JS installation dialog.
Proceed for installation with default selected options and settings
on each screen of installation dialog.

Download And Install PDANet+ for Android


Note : You need to install this software only if you not able to detect
your connected device in PC. Later on we will learn how to connect

device with PC in THIS POST. Right now skip this software


installation and install it only if you face any problem in detecting
device in PC.
Download

Go to PDANet+ DOWNLOAD PAGE.

Click on Download button. It will take you to PDANet+ for


Android download link page.

Click on download link as shown in above image. It will download


installation file (e.g. : PdaNetA4181.exe).

Install

Connect your android phone with PC with USB debugging mode


enabled.
Double click on PDANet+ installation .exe file.

It will open PDANet+ installation dialog.


Proceed for installation with default selected options and settings
on each screen of installation dialog.

How To Download And Install Appium In


Windows With Steps
You must need to install appium if you need mobile apps automation
testing. Till now, We have configured android development
environment in windows to run appium automation tests on android
devices and emulators and we have installed other required software
for appium in previous post. Now we need to download and install
appium in windows to run native/web ios/android automation tests
using selenium webdriver. Bellow given steps will guide you to install
appium in windows.
1. Download Appium
Before download and install appium in windows, Please make sure
bellow given prerequisites are fulfilled.
1. JDK Installed. View THIS POST.
2. Android SDK Installed. View THIS POST.
3. Set
ANDROID_HOME
and
Path
Environment
Variables. View THIS POST.
4. Eclipse ADT Plugin Installed. View THIS POST.
5. Microsoft .Net Framework, Node JS and PDANet+ for Android
Installed. View THIS POST.
For downloading appium in windows,

Go to THIS PAGE.
Click on Download Appium button.

It will download zip file.


When download completes, Extract zip
get AppiumForWindows folder as shown bellow.

file.You

will

2. Install Appium

Open AppiumForWindows folder. "appium-installer.exe" file will


be there.
Double click on "appium-installer.exe" file to install appium. It
will start installing appium.
It will ask you to select setup language.
Select English and click on OK.

It will launch Appium Setup Wizard. Click on Next button.

Next screen will ask you to select appium destination location.


Leave it as it is and click on Next.

Next screen will ask you to set name of appium. Leave it as it is


and click on Next.

Click Next on Select Additional Tasks screen.

On Ready To Install screen, Click on Install button. It will start


installation.

At the end of installation, It will show Completing the Appium


Setup Wizard. Select Launch Appium check box and click
on Finish button.

It will launch Appium as shown bellow.

Appium is installed and ready to use.


3. Configure Appium
Appium will launch with default configuration parameters. To reduce
errors during test execution, You need to set/modify few appium
parameters as per your requirement if they are wrong or not set properly.
See bellow.
Android Settings :

Click on Android Settings button as shown in bellow image.


Select Platform Name = Android
Select Automation Name = Appium
Select PlatformVersion = Your android device's OS version. For
me it is 4.2 Jelly Bean.

General Settings

Click on General Settings button as shown in bellow image.


Note down Server Address and Post number. We need it during
appium test script creation. Server Address is : 127.0.0.1 and Port
number is : 4723.
Un-check Pre-Launch Application check box if it is checked.

Leave all other settings as it is.

Connect Android Device With PC In USB


Debugging Mode To Run Appium Test
You need to connect real android device with PC in USB debugging
mode in order to run android app automation tests in real android device
using appium. Later on we will also learn how to run android app test in
virtual device. First we will use actual android device which is
connected with PC in USB debugging mode to run test. So you must
know how to enable USB debugging mode in android
device and connect with your PC.
PREREQUISITES : Previous 7 STEPS of appium configuration In
windows should be completed.

Enable Developer Option In Android Device


Previously if you have not enabled "developer options" in your android
device then once you need to enable it in order to switch device in USB
debugging mode. Otherwise you can not access USB debugging mode in
your device.
Verify Developer Option Is Enabled?
To check Developer Option is enabled or not,

Go to Settings.
Check if there is any option like "Developer Option"?

If Developer Option is enabled then it will display there. Otherwise


follow the steps given bellow to enable it.
Enable Developer Option
To enable Developer Option in android device,

Go to Settings.
Scroll down to bottom and tap on About Phone.

Scroll down bottom again on About Phone screen. You will see
option Build number.
Tap seven times on Build number option one by one. After 3 tap,
It will start showing you message like.. "You are now 2 steps away
from being a developer" as shown in bellow image.

After 7 times Build number option tap, It will show you message
"You are now a developer!" as shown in bellow image.

Now go back to Settings and scroll down bottom.


You will see option Developer Options above About Phone as
shown in bellow image.

Connect Device With PC And Start USB Debugging Mode


Note : Let me warn you before enabling USB debugging mode in your
android device. Enable USB debugging only when you need it. Leaving
it enabled all the time is kind of a security risk as it allows high-level
access to your android device. So disable it immediat ely when you have
not any usage of it.
To start USB Debugging mode,

Connect your device with PC using USB cable.


Go to Settings -> Developer options.

There will be option USB debugging with check box. Check it.

It will ask you to "Allow USB debugging?". Tap on OK.

It will enable USB debugging mode for your android device.

Verify Device Connected Properly With PC


To verify device is connected properly with PC with USB debugging
mode,

Open command prompt in your PC.


Run command adb devices.

It will show you list of connected devices with your PC. If not display
any device in list that means there is some issue with device connection
or
USB
debugging
mode
is
not
enabled
properly.
Note : If face any issue in android device detecting with PC, Please
install PDANet+ for Android in your system as described in THIS
POST.
This is the way to enable developer mode and USB debugging mode in
your android device to connect it with PC in development environment.
Now you can run native apps automation test in physical devices using
appium and selenium webdriver. We will learn it in my upcoming posts.
Next post will show you how to locate different elements of android
native app using uiautomatorviewer.

SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL


Enter your e Subscribe

Delivered by FeedBu rner

Locating Android App Elements Using UI


Automator Viewer
In previous post we learnt how to enable developer option and USB
debugging mode in android device to connect android device with PC.
So next steps is to learn element locators for android app. Before
learning about UI Automator Viewer, You must knows why we need it.
To automate android app using appium, You should aware about how to
locate elements of android app to use them in webdriver automation
test scripts. Let's try to understand how can we locate elements of
android app.
As you knows, We are going to automate native android apps so it is not
possible to inspect them using fire-bug and fire-path which we can use
to locate elements of web application as described in THIS POST. So
here we need to use some other tool which can locate elements of
android app.
What Is UI Automator Viewer?
UI Automator Viewer is a tool provided by android SDK. You can
view THIS POST to know how to download and install android SDK in
windows. UI Automator Viewer will provide you an user interface to
scan and analyse UI components of android application. Using UI
Automator Viewer, We can inspect the android app
component hierarchy, Inspect properties of android app components and
then we can use those element's properties to create xpath of element
and use them in automation test scripts.
You will find uiautomatorviewer.bat file in tools folder of SDK.
e.g. E:\SDK\tools.
PREREQUISITES : All PREVIOUS 8 STEPS of android
environment configuration should be performed.
Inspecting App's UI Element Using UI Automator Viewer

We will use default and simple calculator app of android phone to learn
how to inspect its's UI elements using UI Automator Viewer tool.
Follow the steps given bellow.

Connect your android device with PC (USB debugging mode


should be enabled) as described in THIS POST.

Run command "adb devices" in command prompt to verify device


is connected properly with PC.

Run uiautomatorviewer.bat file from E:\SDK\tools folder.


View THIS POST to know more about SDK folder. It will open UI
Automator Viewer tool's UI as bellow.

Open Calculator application in your android phone. You can


download Android Calculator App from THIS PAGE if it is not available
with you.

In UI Automator Viewer, Click on Device Screenshot image


button as shown in bellow image. (Before clicking on button please make sure
Calculator app is open in your android phone and it is in active mode. Means
phone is unlocked and you are able to see Calculator app on screen).

It will process to capture device screenshot.

After capturing screenshot of android phone screen, It will show


your android phone's calculator's UI in UI Automator Viewer as bellow.
1. On left site it will show you calculator app's screenshot which is
open in android device.
2. Right side top part will show calculator app's UI element's
hierarchy view. It will display node structure to explain how elements
are arranged.
3. Right side bottom part will show property detail of selected
element.

In calculator screenshot (which is display in UI Automator


Viewer), Select button 5 to view it's different properties as shown in bellow
image.

It is showing different properties of button 5 in right side node


detail section which we can use to locate it using different element locating
strategy.
This way you can inspect any element of android native app using UI
Automator Viewer. You just need to click on element and it will show
you that element's relative property detail.NEXT POST will show you
different ways(XPath, id, className) to locate android app elements.

UI AutomatorViewer : Get Android App


Element's XPath, ID, Name And className
We learnt how to use UI Automator Viewer in PREVIOUS
POST to locate and get properties details of android native software
app's any element. Now we need to learn what are the different ways to
locate elements of android software app and how to determine those
locators to use them in test script. Most successful ways to locate
android software app's elements are XPath, ID, Name
AndclassName so we will learn them rights now. Also we can
use findElements method of webdriver to locate element of android
app.We will use UI Automator Viewer to locate android app
element and get it property and hierarchy detail.
PREREQUISITES : Previous 9 STEPS of appium android software
test automation tutorials should be completed.

Locating Android App Element By XPath


Earlier we learnt how to locate element by XPath in selenium webdriver
software testing tool in THIS POST and also given different examples
to write XPath of element in different ways in THIS POST. So i am
suggesting you to refer both these posts before going ahead.

We will use android calculator app to get element's XPath in different


ways. Supposing I need XPath of button 5. So i can write XPath in
multiple ways for it as bellow.

UI Automator Viewer screenshot for button 5 is as bellow. You can


download Android Calculator App from THIS PAGE if it is not
available with you.

1. XPath using class and text attribute :


In above image you can see that button 5 has text attribute with value =
5 which is unique. Class name is android.widget.Button which is same
for all number buttons. So we can create XPath using text attribute value
with class name as bellow.
xpath("//android.widget.Button[@text='5']")

2. XPath using class and resource-id :


Here resource-id for button 5 is com.android.calculator2:id/digit5.
Trailing part (e.g digit5, digit4, digit7) of resource-id is changing for
every number buttons so we can use contains function to get relative
XPath of button 5 as bellow.
xpath("//android.widget.Button[contains(@resource-id,'digit5')]")

3. XPath using class, text attribute and resource-id :


We can use combination of text attribute and resource-id with class
name to create unique XPath of element button 5 as bellow.
xpath("//android.widget.Button[contains(@resource-id,'digit5') and
@text='5']")

4. XPath using class, text attribute and index :


We can use same approach as described in point 3 but with index
number. Button 5 has index 1 but it is not unique as number buttons 0,2
and 8 has also same index with same class name. So we can
include text attribute with index attribute to locate number button 5.
xpath("//android.widget.Button[@text='5' and @index='1']")

5. XPath using parent and child class hierarchy

As
you
can
see
in
above
image,
Parent
class android.widget.LinearLayout class with index = 1 has buttons 4,
5, 6 and X. So we can locate that specific row by it's class index. Child
element is member of class android.widget.Button with index = 1. So
we can format XPath using parent and child class hierarchy as bellow.
xpath("//android.widget.LinearLayout[@index='1']/android.widget.Button[@index
='1']")

6. XPath using content-desc

Supposing element has content-desc which is unique then you can use it
to create XPath. Example : DELETE button has unique content-desc =
delete. So we can write XPath for delete button using it as bellow.
xpath("//android.widget.Button[@content-desc='delete']")

7. XPath using class name

Note : Using class name In By.className or By.xpath will works only if


it is unique for element. If same class name is provided to multiple
elements then it will not work.

If software application's element has a class name and it is unique for


element then you can use class name only to create XPath. See above
image.
Options
button
has
class
name android.widget.ImageButton which is unique. So we can use it
only to create xpath as bellow.
xpath("//android.widget.ImageButton")

Locating Android App Element By ID


Earlier we learnt how to locate element by ID for web application
in THIS POST. We can locate elements by ID in android app too. We
can use resource-id as a id. Example is as bellow.

resource-id for button 5 is "com.android.calculator2:id/digit5" so we can


locate to it by id as bellow.
id("com.android.calculator2:id/digit5")

Locating Android App Element By className


Same way, We can use class name to locate element if it is unique.

Class name for above option button is "android.widget.ImageButton".


We can locate it using className as bellow.
By.className("android.widget.ImageButton")

Locating Android App element by Name


It is possible to locate element by name if element contains unique text.

If you see in above image, button Equals has text value "=". So we can
locate it by name using bellow given syntax.
By.name("=")

Locating element by findElements


If you inspect all buttons of android calculator app, All the buttons has
same class "android.widget.Button". If you know, findElements method
is useful to get list of all the matching elements from current page as per
given element locator mechanism. VIEW MORE DETAIL on
findElements. So here we can collect list of all buttons
using findElements and store list using java
List interface. Then we access required element using get() method of
List interface.

Array list Index IDs of button elements of "android.widget.Button" class


is as bellow.

List<WebElement>calcButtons =
driver.findElements(By.xpath("//android.widget.Button"));

Above given syntax will store list of all buttons in list calcButtons with
bellow given array list index ids. Bellow given table illustrate array
index ids for different buttons of calculator application.

Button

Array index ID

DELETE

(divide)

Button

Array index ID

(multiply)

10

11

(minus)

12

.(point)

13

14

=(equals)

15

+(plus)

16

So now we can access required button using it's index which is stored in
list calcButtons. Means if you wants to tap on button 5 then you can use
bellow given syntax.

calcButtons.get(6).click();

Here 6 is list index id of button 5 as shown in above table.

So all these are different ways to locate android software app elements.
We will use all these element locator methods practically in our test
scripts which I will publish in upcoming steps.

How To Find .Apk File Package Name And


Launcher Activity Name
In previous step, we learnt about how to locate android native software
app elements by XPath, ID and className and We will learn how to run
your first android software app test in next step. But before creating your
first android native software app, You must know how to find package
name and launcher activity name of your android app which you are
going to test using appium. Because we need to provide package
name and launcher activity name of android software app in test
script to launch it in device or emulator. This post will describe you how
to get package name and launcher activity name of.APK file using
different ways. More 2 methods of finding package name and launcher
activity name are given on THIS PAGE.
PREREQUISITES : Previous appium tutorial's 10 STEPS should be
completed.

Method 1 : Using APK Info App


If your android app is installed in device and you need it's package name
and launcher activity name then you can use APK Info android app to
get detailed information of any installed application. Let's try to get
package name and launcher activity name of app called Contact
Manager which i have installed. You can follow same steps for any app.

Go to GOOGLE PLAY STORE, Search for app using keyword


"APK Info".
It will show you list of apps. There will be app with name APK
info as shown in bellow image.

Install this android software app in your android device.


Alternatively you can download APKInfo app from THIS
PAGE too.
Launch APK info app in android device.

It will show you list of all installed apps in your device.


Locate Contact Manager android software app from list which is

supplied by APK info app. Tap on Contact Manager app for 2 to


3 seconds.

It will show you popup message as shown in bellow image. Select


Detailed Information option.

It will show you your Contact Manager app detail as shown in


bellow image.

See above image, APK path contains App Package name.


So Package
name for Contact
Manager app
is com.example.android.contactmanager and Activity
Name for Contact
Manager app
is com.example.android.contactmanager.ContactManager.

Method 2 : Using Command Prompt


This is another way to get android app package and activity name. Let's
try to get package name for Contact Manager app.
Get Package Name

Connect your android phone with PC and turn on USB debugging


mode as described in THIS POST.
Open Command prompt.
Run command adb shell pm list packages -f

It will show you list of apps(Which are installed in your device)


with package name.
Find your app from list. It will show you package name as shown
in bellow image.
Bellow given image shows package name of Contact
Manager app.

Package name for Contact Manager app


is com.example.android.contactmanager.

Method 3 : Using logcat In Command Prompt

Connect your android phone with PC and turn on USB debugging


mode.
Open Command prompt.
Run command adb logcat.

Open app in android phone. Immediately press CTRL + C in


command prompt to stop logging in command prompt.
Android phone's latest activity will be logged in command prompt.
If you see in log, It will show you app launcher log as shown
bellow.

Here com.example.android.contactmanager is package


name and com.example.android.contactmanager.ContactMana
ger is activity name of Contact Manager app.

This way you can get any android software application's package name
and launcher activity name easily. We have to use both these parameters
in test script.

Configure Project In Eclipse For Appium


Earlier we learnt how to setup environment in windows for run appium
automation project, SDK installation, usage of tools like UI Automator
Viewer to locate android app elements etc. Now everything is ready and
we are just step away from running appium test in android device.
Before that we need to create project in eclipse so let's configure
project in eclipse for appium. Bellow given steps will tell you how to
setup appium project. I hope most of you already knows all these
things but still let me describe you how to configure first appium
project in eclipse.
PREREQUISITE : All previous 11 STEPS should be completed
without any error.
Create

New

Project

In

Eclipse

Before creating new project In eclipse, You must be aware about how to
download Eclipse IDE, How to create workspace for project, Creating
new project and package In eclipse and adding jar files In your project's
build path. I have already described all these thing In THIS POST so I
will not repeat It here.

Start Eclipse IDE. It will ask you to set workspace If you are using
It first time.
Set workspace.
Create new project(With your desired name. For me It Is
MavenProject1) In eclipse with package name Android.
Install TestNG : I hope testng is already installed in your eclipse.
View THIS POST to know how to install testng in eclipse.

Not using maven? :


If you are not using maven with eclipse then follow bellow given steps
in eclipse.
Download Latest Jars of WebDriver and Appium Client Libraries
We need to add latest version jar files of selenium webdriver and
Appium Client Libraries in our project's build path.

You can download jar files for selenium webdriver from THIS
PAGE. View THIS POST for more detail.
You can download latest version of appium client libraries(javaclient-x.x.x.jar where x.x.x is current latest version of
jar) from THIS PAGE. Click on Java link, It will take you on jar
file download page.

Then click on jar link as shown bellow.


downloading appium client libraries jar file.

It

will

start

Download
Latest
Jar
File
Of google-gson
You also need google-gson's latest jar file. Current latest version Is
2.3.1. You will find google-gson download link on THIS PAGE.

Add jar files in project's build path


Now we have selenium webdriver, appium client libraries and gson jar
files.
HERE you can learn how to add jar files in project's build path

Add selenium webdriver jar files in project's build path.


Add java-client-x.x.x.jar in project's build path.
Add gson-x.x.x.jar in project's build path.

That's it..
Using maven? :
If you are using maven project management tool with eclipse then no
need to add above jar files in project's build path but you have to add
Selenium WebDriver, Appium Java Client Libraries and
gson dependencies in pom.xml file.

Visit THIS PAGE to find selenium webdriver dependency.


Visit THIS
PAGE to
find
Appium
Java
Client
Libraries dependency.
Visit THIS PAGE to find gson dependency.

Current latest dependencies for selenium, appium java client and gson
are as bellow. It may change in future so update accordingly.

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>

<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>

This is all about appium project configuration in eclipse for maven and
non maven users.

Appium - Run First Android Automation Test


Using In Eclipse
We have configured appium project in eclipse during previous post.
Now we are all set to create and run first appium software automation
test script on android mobile device. In this steps we will learn how to
create first appium automation softwre test script in eclipse and then
run it in real android mobile device.
PREREQUISITE : All previous 12 STEPS should be completed
without any error.
We will use android mobile phone's default installed calculator app to
run appium software automation test script. You can download Android
Calculator software App from THIS PAGE if it is not installed in your
mobile. This is our first appium software test so we will create simple
test to sum two numbers. Follow the steps given bellow.
1. Gather Required Capabilities
Before creating appium test for android software app, You need bellow
given parameters of android device and app to set webdriver capabilities
in test script.
1. Find Android Device Name : As described in THIS POST,
Connect your android device with PC and get device name by
running adb devices command in command prompt. My android
device name Is : ZX1B32FFXF. Find your device name.

2. Find Android Software App Package Name : You can


view THIS POST to know how to get android app package name.
Package name for my calculator app (Which Is Installed in my
android device) is com.android.calculator2. Find your app
package name.
3. Find App Activity Name : You can view THIS POST to know
how to get android app activity name. Activity name for
my calculator app (Which Is Installed in my android device)
is com.android.calculator2.Calculator. Find your app activity
name.
4. Find Android OS Version : In your android device, Open settings
-> About phone -> Android version. My device's Android version
Is : 4.4.2.
2. Launch And Start Appium Node Server
Appium should be installed and configured and also you need Server
Address and Port number (which is used by appium) as described
in THIS POST.
Launch Appium

Launch Appium from Windows Start menu.

For me, Server Address is : 127.0.0.1 and Port Number is : 4723.

Start Appium Node Server


Click on Start button to start appium node server as shown in bellow
image. It will take some time to launch node server.

Note : You can consider appium node server is started properly once it
shows
log
as
shown
in
above
image.

3. Create Appium Test Sc ript In Eclipse


I have created sample appium software test script using selenium
webdriver to sum two numbers using android calculator application.
Prerequisite :

1.
2.
3.
4.

Calculator app should be installed in your android device.


Install TestNG in eclipse if it is not installed. View THIS POST.
Also replace capabilities values of bellow given list with your own
values in script. Otherwise it will not works.
deviceName - Name of device which is connected with PC.
CapabilityType.VERSION - OS version of your android device.
appPackage - Calculator app's Package name.
appActivity - Calculator app's Activity name.

Now create bellow given test class under Android package of your
appium project. Usage of each code syntax is given in script itself.
SimpleAndroidCalcTest.java
package Android;
importjava.net.MalformedURLException;
import java.net.URL;
importjava.util.concurrent.TimeUnit;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.remote.CapabilityType;
importorg.openqa.selenium.remote.DesiredCapabilities;
importorg.openqa.selenium.remote.RemoteWebDriver;
importorg.testng.annotations.AfterTest;
importorg.testng.annotations.BeforeTest;
importorg.testng.annotations.Test;
public class SimpleAndroidCalcTest {

WebDriver driver;
@BeforeTest
public void setUp() throws MalformedURLException {
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();
// Set android deviceName desired capability. Set your device name.
capabilities.setCapability("deviceName", "ZX1B32FFXF");
// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
// Set android VERSION desired capability. Set your mobile device's OS
version.
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
// Set android platformName desired capability. It's Android in our case
here.
capabilities.setCapability("platformName", "Android");
// Set android appPackage desired capability. It is
// com.android.calculator2 for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appPackage", "com.android.calculator2");
// Set android appActivity desired capability. It is
// com.android.calculator2.Calculator for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appActivity",
"com.android.calculator2.Calculator");
// Created object of RemoteWebDriver will all set capabilities.
// Set appium server address and port number in URL string.
// It will launch calculator app in android device.
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
@Test
public void Sum() {
// Click on DELETE/CLR button to clear result text box before running test.

driver.findElements(By.xpath("//android.widget.Button")).get(0).click();
// Click on number 2 button.
driver.findElement(By.name("2")).click();
// Click on + button.
driver.findElement(By.name("+")).click();
// Click on number 5 button.
driver.findElement(By.name("5")).click();
// Click on = button.
driver.findElement(By.name("=")).click();
// Get result from result text box.
String result =
driver.findElement(By.className("android.widget.EditText")).getText();
System.out.println("Number sum result is : " + result);
}
@AfterTest
public void End() {
driver.quit();
}
}

Note : Before running above script using testng, Please make sure your
android device is connected with PC with USB debugging mode enabled
and Appiun node server is launched and started.
4. Running Appium Test Script
I hope you already know how to run software test script using testng
(VIEW EXAMPLE). Run above test script and view your android
phone screen. It will

Open calculator app in your mobile device.


Tap on buttons in this sequence -> CLR, 2, +, 5 and =.
Get result from text area of calculator app.
Print result in eclipse console.

This way, We have executed very simple test script in android mobile
device. If you have noticed in above example test script, We have used
only name locator. We will use all different element locators (Which are
described in THIS POST) in upcoming examples.

How To Create And Start An Android Virtual


Device(Emulator) To Run Appium Test
In previous post, We executed first appium test successfully in android
mobile
device.
Also
you
can
use Android
Virtual
Device(Emulators) to run automation tests on nadroid app using
appium. So let's learnt how to create android emulator in windows
system to rum appium test on it. This post will describe you how to
create (AVD) In Windows.
What Is An Android Virtual Device(Emulators)?
In general term, an emulator is software or hardware that enables one
computer system (Example : Windows) to behave like another computer
system(Example : Android). It provides virtual environment of another
system.
So here we can create emulator of android device in windows system to
run appium test on it.
Creating Emulators(AVD) In Windows
If you remember, Earlier we have installed Android SDK in windows.
Android SDK allows you to create emulators in windows. So let's try to
create android emulator in windows.
PREREQUISITES :
1. Java should be installed as described in THIS POST.
2. Android SDK, it's all required packages and Intel Hardware
Accelerated Execution Manager must be installed as described
in THIS POST.

3. Android environment variables should be set. View THIS POST.


Launch AVD Manager
SDK provides interface called AVD Manager to create android virtual
devices. So first of all you need to start AVD Manager.

Open SDK folder. For me it is located at E:\SDK as described in


SDK installation steps.
There will be "AVD Manager.exe" file.

Double click on "AVD Manager.exe". it will launch AVD


manager dialog as shown bellow.

Create Android Virtual Device


Using AVD Manager interface, We can create emulator as described in
bellow steps.

Click on Create button. It will open Create new Android Virtual


Device dialog as shown bellow.

Note : In Target drop down, You need to select that API level for which
you have installed system images during SDK installation. I have
installed Android 5.1.1 - API Level 22 so selected it here. Also do not
put space in AVD Name textbox.

Fill values in all fields as shown in above image and click


on OK button.
It will create AVD and show message as shown bellow.

New created device will be listed on AVD manager as shown


bellow.

Same way you can create any other size device emulators too as per your
system images installation for different Android API levels.
Start AVD Emulator
Note : Please make sure you have minimum 2 GB RAM in your system.
4 GB RAM is recommended. AVD Emulator uses large amount of
memory. So please close all non-required software applications before
starting it.
To start Emulator

Select New create AVD 2.7_Inch_QVGA from list.


Click on Start Button as bellow.

It will show Launch Options dialog. Click on Launch button.

It will launch android emulator as shown in bellow image. Full


launch can take time from 5 to 10 minutes based on your system
configuration.

Wait for a while. Emulator screen will looks like bellow after few
minutes. Be patient.

So now android emulator is launched.


Unlocking Emulator Screen
Same as android mobile device, You can unlock emulator screen by
dragging lock icon up by mouse as shown in bellow image.

After unlocking, Emulator screen will looks like bellow.

This way you can create and launch emulator for android device in
windows system. We will use this emulator to automate different
android apps in my upcoming posts.
Verify Emulator Detected
You can detect emulator from command prompt using command adb
devices as shown in bellow image.

So my emulator is running fine and detected by my system.

Install/Uninstall App In Emulator (AVD) Of


Android From Command Prompt
I hope, All of you aready know how to install or uninstall app in real
android device. But If you wants to work with android emulators for
manual or automation testing, You must be aware about how to install or
uninstall android applications in android emulators. Here I am present
steps of how to install android app or uninstall any android app from
android virtual device(Emulator).
PREREQUISITES :

All PREVIOUS 14 STEPS of appium tutorial should be executed


without any error.
Android emulator should be created as described in THIS POST.
.apk file should be available in your PC which you wants to install
in emulator. Here I am installing APK Info android application. So
have it's source file com.intelloware.apkinfo.apk in my PC to
install it in emulator. You can download it from Google Play
Store or THIS PAGE.

Steps to Install APK in Android Emulator

Launch android emulator. Wait till it starts properly as shown


bellow

Open platform-tools folder of SDK. For me It is located


at E:\SDK\platform-tools. View THIS POST to know more
about SDK folder.
Copy-paste com.intelloware.apkinfo.apk (Which you wants to
install in emulator) file in platform-tools folder.

Navigate
to platform-tools folder
in
command
prompt. In platform-tools folder, Press
keyboard's CTRL+Shift+Mouse Right Click. It will open right

click context menu as shown in bellow image. Select Open


command window here option from context menu.

It will open command prompt with navigation to platformtools folder.

Run adb
prompt.

install

com.intelloware.apkinfo.apk in

command

Note : Here, com.intelloware.apkinfo.apk Is APK file name. You


need to replace it with your APK file name if you are installing any
other app.

It will start installing app in emulator.


message Success after installation of app.

Now you can verify app is installed or not in your android


emulator. Navigate to main menu in emulator screen. New
installed app APK Info will display there on emulator screen as
shown bellow.

It

will

show

Steps to Uninstall APK From Android Emulator


App uninstall process from emulator is similar to installation process
from command prompt. There are two ways to uninstall app from
android emulator.
1. Uninstall App Manually F rom Emulator Interface :
It is same as you uninstall app from your android device.

Go to Settings of Emulator.
Tap on Apps.

Tap on App which you wants to uninstall.

Click on Uninstall button. It will uninstall app from emulator.

Verify your app will be uninstalled from emulator.


2. Uninstall App Using Command P rompt
To uninstall app using command prompt,

Open command prompt and navigate to platform-tools folder in it.


Run command adb
uninstall com.intelloware.apkinfo in
command prompt.

When app get uninstalled, It will show you message Success. That
means app is uninstalled from your emulator. You can verify it in
your emulator.

This way you can install and uninstall any app in your emulator.

Vous aimerez peut-être aussi