0% found this document useful (0 votes)
160 views6 pages

Mobile Automation Using Appium

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Mobile Automation Using Appium

Introduction to Appium
Appium is an open-source tool for automating native, mobile web, and hybrid applications
on iOS and Android platforms. Native apps are those written using the iOS or Android
SDKs.Mobile web apps are web apps accessed using a mobile browser (Appium supports
Safari on iOS and Chrome or the built-in 'Browser' app on Android). Hybrid apps have a
wrapper around a "webview" -- a native control that enables interaction with web content.
Importantly, Appium is "cross-platform": it allows you to write tests against multiple
platforms (iOS, Android), using the same API. This enables code reuse between iOS and
Android test suites.

Appium Concepts
Client/Server Architecture
Appium is at its heart a webserver that exposes a REST API. It receives
connections from a client, listens for commands, executes those commands on a
mobile device, and responds with an HTTP response representing the result of
the command execution. The fact that we have client/server architecture opens
up a lot of possibilities: we can write our test code in any language that has a
http client API, but it is easier to use one of the Appium client libraries. We can
put the server on a different machine than our tests are running on. We can write
test code and rely on a cloud service like Sauce Labs to receive and interpret
the commands.

Session
Automation is always performed in the context of a session. Clients initiate a
session with a server in ways specific to each library, but they all end up sending
a POST /session request to the server, with a JSON object called the 'desired
capabilities' object. At this point the server will start up the automation session
and respond with a session ID which is used for sending further commands.

Desired Capabilities
Desired capabilities are a set of keys and values (i.e., a map or hash) sent to the
Appium server to tell the server what kind of automation session we're
interested in starting up. There are also various capabilities which can modify
the behavior of the server during automation. For example, we might set
the platformName capability to iOS to tell Appium that we want an iOS session,
rather than an Android one. Or we might set the safariAllowPopups capability
to true in order to ensure that, during a Safari automation session, we're allowed
to use JavaScript to open up new windows. See the capabilities doc for the
complete list of capabilities available for Appium.
Appium Server
Appium is a server written in Node.js. It can be built and installed from source or
installed directly from NPM.

Appium Clients
There are client libraries (in Java, Ruby, Python, PHP, JavaScript, and C#) which
support Appium's extensions to the WebDriver protocol. When using Appium, you
want to use these client libraries instead of your regular WebDriver client. You
can view the full list of libraries here.

Appium.app, Appium.exe
There exist GUI wrappers around the Appium server that can be downloaded.
These come bundled with everything required to run the Appium server, so you
don't need to worry about Node. They also come with an Inspector, which
enables you to check out the hierarchy of your app. This can come in handy when
writing tests.

Appiums Architecture
Appium is an HTTP server written in node.js which creates and handles multiple
WebDriver sessions for different platforms like iOS and Android.

Appium starts a test case on the device that spawns a server and listens for
proxied commands from the main Appium server. It is almost same as Selenium
server which perceives http requests from selenium client libraries and it
handles those requests in different ways depending upon the platforms. Each
vendor like iOS and Android has a different way and mechanism to run a test
case on the device so Appium kind of hacks in to it and run this testcase after
listening commands from appium server.

How Appium works in iOS:

On iOS, Appium proxies command to a UIAutomation script running in Mac


Instruments environment. Apple provides this application called instruments
which is used to do lot activities like profiling, controlling and building iOS
apps but it also has an automation component where we can write some
commands in javascript which uses UIAutomation APIs to interact with the App
UI. Appium utilizes these same libraries to automate iOS Apps.
In the above figure, we can see the architecture of the Appium in context to the
iOS automation. If we talk about a command life-cycle, it goes like, Selenium
webdriver picks a command form the code like (Element.click) and sends it in
form of JSON via http request to the Appium server. Appium server knows the
automation context like the iOS and Android and sends this command to the
Instruments command server which will wait for the Instruments
command client (written in node.js) to pick it up and execute it in bootstrap.js
with in the iOS instruments environment. Once the command is executed the
command client sends back the message to the Appium server which logs
everything related to the command in its console. This cycle keeps going till the
time all the commands gets executed.

How Appium works in Android:

The situation is almost similar in case of Android where Appium proxies


commands to a UIAutomator test case running on the device. UIAutomator is
Androids native UI automation framework which supports running junit test
cases directly in to the device from the command line.It uses java as a
programming language but Appium will make it run from any of the WebDriver
supported languages.
In the above diagram we can see, here we have Bootstrap.jar in place of
bootstrap.js which represents out test case when compiled in java. As soon as it
gets launched it spawns a TCP server. Here the TCP server resides inside the
device and client is in the Appium process which is just opposite to the way it is
in iOS.

Setting up Appium on Mac OSX


First we need to download the Appium app for Mac from this link. Once
downloaded you need to move this app in to your applications folder and then you
can launch it from your Mac Launchpad.

Automating mobile web apps in iOS

1) Launch Appium application in Mac, Select Apple radio button if you are
running automation scripts for iOS. The default port the appium uses is
4723.
2) Use DesiredCapabilities as given below for running automation scripts for
mobile web app on IPad simulator:

DesiredCapabilities capabilities = new DesiredCapabilities();


capabilities.setCapability("deviceName", "iPad");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "7.1");
capabilities.setCapability("browserName", "Safari");
//Create an instance of RemoteWebDriver and connect to the Appium server.
driver = new RemoteWebDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
3)

Automating Hybrid apps in iOS

1) Launch Appium application in Mac, Select Apple radio button if you are
running automation scripts for iOS. The default port the appium uses is
4723.
2) Use DesiredCapabilities as given below for running automation scripts for
hybrid app on IPad simulator:

Automating mobile web apps in Android

----Tried automating mobile web apps in Android using Appium Getting some
issues-Need to update this sections------

Automating Native apps in Android

1) Launch Appium application in Mac, Select Android radio button if you are
running automation scripts for Native app on Android. The default port the
appium uses is 4723.
2) Use DesiredCapabilities as given below for running automation scripts for
Native app on Android simulator:

//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);

Getting appPackage and appActivity for an installed app in android


device

I suggest you to use adb (Android Debug Bridge) tool (part of Android
SDK). So, install application under test on target Android device or
emulator. And you should run application to get current activity and
package name. So, after running app execute the following commands
from command line (android-sdk/platform-tools/adb):
adb shell dumpsys window windows | grep -E 'mCurrentFocus|FocusedApp'
And you will get something like this:

mCurrentFocus=Window{43270790 u0
com.estrongs.android.pop/com.estrongs.android.pop.view.FileExplorerActivity}
mFocusedApp=AppWindowToken{44d67b88 token=Token{435e5990 ActivityRecord{434ee320 u0
com.estrongs.android.pop/.view.FileExplorerActivity t133
So, current activity
is com.estrongs.android.pop/.view.FileExplorerActivity and application
package name is com.estrongs.android.pop is this case.
Steps:
1. Install apk on device or emulator
2. Run application
3. Execute adb shell from command line
4. Execute command to get current activity from device(shell) shell:
dumpsys window windows | grep -E 'mCurrentFocus|FocusedApp'
I hope it helps you.

Source :- http://sqa.stackexchange.com/questions/12373/android-app-testing-with-
appium/12382#12382

You might also like