Working of Appium and Sample Automation

Introduction

     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. Projects like Phonegap, make it easy to build apps using web technologies that are then bundled into a native wrapper, creating a hybrid app. Appium is “cross-platform”: which means allows you to write tests against multiple platforms (iOS, Android), using the same API. This enables code reuse between iOS and Android test suites.

How does 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.

ios

     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 does Appium works in Android?

     The situation is almost similar in case of Android where Appium proxies command to a UIAutomator test case running on the device. UIAutomator is Android’s 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.

android

     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.

TestNG Annotations

Following is the list of the most useful and favorable annotations in TestNG:

  • @Test: The annotation notifies the system that the method annotated as @Test is a test method.
  • @BeforeSuite: The annotation notifies the system that the method annotated as @BeforeSuite must be executed before executing the tests in the entire suite.
  • @AfterSuite: The annotation notifies the system that the method annotated as @AfterSuite must be executed after executing the tests in the entire suite.
  • @BeforeTest: The annotation notifies the system that the method annotated as @BeforeTest must be executed before executing any test method within the same test class.
  • @AfterTest: The annotation notifies the system that the method annotated as @AfterTest must be executed after executing any test method within the same test class.
  • @BeforeClass: The annotation notifies the system that the method annotated as @BeforeClass must be executed before executing the first test method within the same test class.
  • @AfterClass: The annotation notifies the system that the method annotated as @AfterClass must be executed after executing the last test method within the same test class.
  • @BeforeMethod: The annotation notifies the system that the method annotated as @BeforeMethod must be executed before executing any and every test method within the same test class.
  • @AfterMethod: The annotation notifies the system that the method annotated as @AfterMethod must be executed after executing any and every test method within the same test class.

Desired Capabilities for Appium Tests

     The desired capabilities are a set of keys and values that will be sent to the Appium server. These keys and values tell the Appium server the specifications of the automated test that you will be running. Using our Platforms Configurator you can easily determine the correct desired capabilities for the programming language.

Here is a list of the main desired capabilities that you will be using for your mobile tests:

  • browserName (required): The mobile web browser that will be automated in the simulator, emulator or device (e.g Mobile Safari, Browser, Chrome, etc). If testing a mobile native application or a mobile hybrid application the value for this capability should be an empty string.
  • deviceName (required): The name of the simulator, emulator or device that will be used (e.g iPhone Simulator, iPad Simulator, Android Emulator, Samsung Galaxy S4 Emulator, Samsung Galaxy S5 Device, etc.). To specify Android phones and tablets read the Android Emulator Skins section.
  • platformName (required): The mobile operating system platform that will be used (e.g iOS or Android).
  • platformVersion (required): The mobile operating system version that will be used (e.g 8.0, 7.1, 4.0, 5.0, etc.).
  • appiumVersion (optional): The version of the Appium driver that will be used. It is recommended to specify the latest Appium version which is the one suggested by the Platforms Configurator. If not specified the test will run against the default Appium version.
  • app (only for mobile native application or mobile hybrid application tests): The path to a .apk or .zip file containing the app to test. This capability is not required for Android if you specify the appPackage and appActivity capabilities.
  • device-orientation (optional): The orientation in which the simulator/device will be rendered (e.g portrait or landscape).
  • appActivity (optional): The activity name for the Android activity you want to launch from your package. This often needs to be preceded by a. (dot) (e.g., .MainActivity instead of MainActivity). Appium will automatically determine the activity to launch, you only need to use this desired capability if you want to specify an activity different than the default one.
  • appPackage (optional): The Java package of the Android app you want to run (e.g com.example.android.myApp, com.android.settings, etc.). Appium will automatically determine the package to launch, you only need to use this desired capability if you want to specify a package different than the default one.
  • automationName(optional): The automation engine that will be used (e.g Appium or Selendroid). By default the automationName used is Appium.

Automate your application

An android app automation steps can be summarize as follows:

  1. Configure System (Install Appium, Eclipse and TestNG plugin)
  2. Launch Appium and start node server
  3. Launch Eclipse and Create a Java automation project
  4. Write script for execution
  5. Copy – paste .apk file into desired location
  6. Connect a physical device or launch emulator
  7. Run the script as TestNG Test

Step 1: The system configuration includes Install Appium, Eclipse and TestNG plugin.

Step 2: Launch Appium and start node server:

app

app1

Observe that the node server started and the launch icon changed to the stop icon. Here the port number for Appium is 4723.

Step 3: Launch Eclipse and create a java automation project:

  1. Launch the Eclipse by search for the eclipse execution file
  2. Create a new workspace (Folder) for first time launch eclipse
  3. User will get the welcome screen
  4. User can create new Java Project from File ->New -> Java Project

eclipse

prj       5. User can create new Java Project from File ->New -> Java Project

save6. Click on the Next button and select the tab ‘Libraries’.

libv7. Add TestNG jar file and selenium-server-standalone-2.48.2 jar file (add latest jars

8. Click on ‘Finish’ button.

Step 4: Write test script for execution:

  1. Create a class file Configuration for configuring our script and the Appium node server running.
  2. Create a driver object.
  3. Declare the desired capabilities for the driver object.
  4. Connect the Appium port with the driver object.

A sample configuration is as follows:

code

Step 5: Copy-Paste .apk file in the desired location:

  1. Paste the .apk file in the location which mentioned in the driver capability.

Step 6: Connect a Physical device or launch the emulator:

  1. If connects a physical device wait for device to mount with the ADB.

Step 7: Run the script as TestNG Test:

  1. Right click on the script class and Run As TestNG Test.

Enjoy your testing and explore more………