App Testing using Appium and Webdriver

Introduction:

Appium is an open source automation tool to test native, webview and hybrid application in iOS and android devices. Native apps are written using iOS or Android SDK. Mobile web apps are web apps access by browser like chrome or safari. Hybrid apps are wrapper around webview, a native control that interacts with webview. Cordova PhoneGap or Ionic apps are wrapper around web technologies and bundled into native UI libraries.

Appium is a “class-platform” test tool that means you can use Appium APIs to write automation for iOS and android.

Support:

Support iOS Android
Operating System Mac OS X > 10.7 Windows, Linux, Mac OS X
Version 7.1, 8.0, 8.1, 8.2, 8.3, 8.4, 9.0, 9.1, 9.2 and 9.3 2.3 and up
Devices iPhone Simulator, iPad Simulator, and real iPhones and iPads Android emulators and real Android devices
Hybrid App Yes ( for real devices, iOS-webkit-remote-debugger required) Yes
Native App Yes

Debug .app ver in simulator

Registered. ipa in real device

Yes
Mobile Web App Yes Yes
Multiple Apps in Single Session No Yes (no selendroid)
Multiple device No Yes

In this tutorial, we will use Java to write automation scripts. Instead of talking too much about introduction, let’s start working on this.

Core Concepts

Appium is based on five core concepts:

Client Server Architecture:

Appium is based on CSA and its actually a web server with REST API. It receives connections from client, accept and process commands and execute those commands on mobile device and return the response back to the client. Being webserver based on REST API gives us an advantage that we can use any language that has REST API and easy to communicate with Appium API to write our test code like Java, Ruby, .NET, php etc.

appium1

Session:

Appium always perform automation in session. Client establish a session with server and send commands that end up sending /POST session with a JSON Object, called Desired Capabilities Object. At this point, server establish a connection with sessionID which can be used to perform further commands.

Desired Capabilities:

Desired capabilities are combination of keys and value like map or hash where you can assign and send various setting for automation to the server. If you want to test on device you can send platform name as “iOS” or “Selendroid”. There are multiple settings on the desired capabilities which you can send. Refer  here for list of desired capabilities.

Appium Server:

Appium server is written in node.js. This process your test code and execute on device or simulator.

Appium Client:

There are multiple libraries( Java, Ruby, .NET, Python etc.) which supports Appium’s extension to the WebDriver protocol.You need to these libraries instead of WebDriver libraries.

Installation:

Mac OS X

If you are using mac then I assume that you are using latest and greatest version of OS X. For Appium on mac, OS X 10.10 is recommended.

Install Xcode from app store.

clip_image002

Install latest and stable version of ruby:

$ \curl -sSL https://get.rvm.io | bash -s stable

$ rvm install ruby

Install bundler and ruby gem:

$ gem update –system

$ gem install –no-rdoc –no-ri bundler

$ gem update

$ gem cleanup

Make sure that Ruby Gem is >=2.1.5

$ gem –v

Install appium_console gem

$ gem uninstall -aIx appium_lib

$ gem uninstall -aIx appium_console

$ gem install –no-rdoc –no-ri appium_console

$ Install flaky gem

$ gem uninstall -aIx flaky

$ gem install –no-rdoc –no-ri flaky

$ Install brew

$ ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

Install nodejs using brew.

Make sure that you are using node version higher than 0.0.6.

$ brew update

$ brew upgrade node

$ brew install node

$ node -v

$ Install grunt

$ grunt –version

$ grunt-cli v0.1.13

$ grunt v0.4.2

install Ant

$ brew install ant (quick and easy) OR

Try following steps.

Apache Ant is a Java library and command-line tool that help building software. To install that go to http://ant.apache.org/bindownload.cgi and download tar.gz

clip_image004

Open terminal and go to the download folder where you have downloaded apache-ant-X.Y.Z-bin.tar.gz

Go to Download folder

$ cd ~/Downloads

Extract the folder

$ tar -xvzf apache-ant-1.8.1-bin.tar.gz

Make sure that /usr/local exists

$ sudo mkdir -p /usr/local

Copy ant files into /usr/local folder

$ sudo cp -rf apache-ant-1.8.1-bin /usr/local/apache-ant

Add the new version of Ant to future terminal sessions

$ echo ‘export PATH=/usr/local/apache-ant/bin:”$PATH”‘ >> ~/.profile

Verify new version of ant

$ ant –version

 

Install maven >= 3.1.1.

$ brew install maven (if you get 404 error, try brew update and try again)

Now you are ready to go for next step.

 

Setup App-Under-Test, Appium and Appium Inspector

Setup Demo App Under Test:

Now you have setup your computer for write automation script to test your app. I create a sample app for this. App under test is a hybrid app build using ionic. To build and view the app, you need to install ionic first which is quick.

$ npm install -g cordova ionic

Once the installation is done. Clone the following code.

$ git clone git@github.com:learnseleniumtesting/AppiumTestApp.git

$ cd AppiumTestApp

$ ionic platform add ios

$ ionic build ios

$ ionic emulate ios

App will start in emulator. This app has two side menus, one for home and other is for basic form. On basic form, there are two text fields, first and last name respectively and one submit button. This capitalize your first and last name and print on the screen. We will use this application to write very basic automation test and execute on emulator.

These are following screens in the app.

clip_image002[5]clip_image004[5]

After building iOS platform, you will find a folder for app you need to save that location which you need to setup appium app.

/Users/administrator/Documents/dev/ionicapps/AppiumTestApp/platforms/ios/build/emulator/AppiumTestApp.app

Save above url because you need this in appium and appium inspector.

Appium And Appim Inspector:

If you are new to appium then please go through previous chapters. If you have done all and want to install appium inspector then please download the latest version of appium.dmg file. ( https://bitbucket.org/appium/appium.app/downloads/)

Note: It may appear that file not downloaded as the download file will be like Unconfirmed 247104.crdownload”. Rename this file to appium.dmg.

Click on the appium.dmg to mount the disk image.

Drag and Drop Appium to the Application folder.

clip_image005

Go to application and start appium. You are ready to use appium and appium inspector.

Appium:

Go to Application and start Appium. You will so see following screen.

clip_image007

Appium Doctor:

This is to diagnose and fix common issue related to node, iOS and Android configuration.

clip_image009

Appium Server:

Click to start the Appium server. By default it starts on port 4723. Before starting the server, you need to setup your app to either Android or iOS. I am going to setup iOS. Click on the apple icon on the Appium.

clip_image011

Here we will discuss about basic configuration:

1. App Path: Path of .app file for application under test. In previous chapter, you build iOS, using command ‘ionic build ios’ which will app folder for iOS and that you can add in APP Path;

(/Users/administrator/Documents/dev/XXXX/AppiumTestApp/platforms/ios/build/emulator/AppiumTestApp.app)

2. Check ‘Use Mobile Safari’ if you are testing mobile website.

3. In Device Setting, you can choose type of device and corresponding iOS version based on environment under test. If you want to run on actual device then provide your UDID.

4. Choose option to see the iOS system and simulator log.

Click on apple icon to close this dialog. Now click on “Launch” button. This will start the appium server for and you can see similar info on the appium console.

clip_image013

If you are seeing to many things on console and getting difficult to follow along then you can delete the log from console by clicking trash can icon button as given above.

Appium Inspector:

Start the server. Click on the lance icon on Appium. This will start the application in emulator and start the inspector as well.

clip_image015

Detailed view of Inspector:

clip_image017

You need to use the xpath to find element in our automation code. You will use that in later chapter.

I think this should be enough to write basic script to perform an automation scenario using Appium.

Write your basic automation script.

In the end of previous chapter, you were able to setup Eclipse with all libraries needed for writing automation test. And now you are ready with your application opened in Appium tester and you are able to navigate through out you application in emulator and refresh the Appium inspector it will update the screen and update all elements present in the screen.

There is ‘Record’ button on the Appium inspector

clip_image019

This will turn into RED and this will launch code editor on bottom side of Appium Inspector. You can change the choice of language you want. We will choose Java.

clip_image021

You will utilize these code into next chapter when we will actually start writing our code

Setup Project and write code in Eclipse IDE

Download required Libraries for Appium:

Since you already have installed and setup Eclipse so before going to tool I would like you download required webdriver and Appium libraries that is required by our automation script.

Appium Java Client:

Download latest Appium java client from maven

clip_image002[9]

Click on the latest version to download. Save this file to specific folder as per your choice.

clip_image004[9]

Webdriver Java client:

Download webdriver java client from selenium official website www.seleniumhq.org/download/

clip_image006[4]

Unzip file and save extraced file/folder in the same folder you saved Appium-java client.

Start eclipse and select a workspace.

clip_image007[7]

You will get blank eclipse workspace.

clip_image009[7]

Right click on the package explorer and create new project (New -> Java Project)

clip_image011[9]

Choose any name for your project and click “Next” button.

clip_image013[8]

Click on Finish button to complete the process.

clip_image014[4]

Now you will have a project created that you can see in package explorer.

clip_image016[4]

Go to the property of the Java project to add supporting libraries for Appium and selenium beforehand.

clip_image018[4]

Choose JavaBuildPath and go to Libraries tab and click on Add External JARs.

clip_image020[4]

And choose all jar files Appium and webdriver folder and subfolders.

clip_image022[4]

All those added jars will appear in Referenced Libraries as below:

clip_image023[4]

Now add the test class. Src -> right click -> New -> Class.

clip_image025[4]

Name the package as com.Appiumtesting and Name of the class is AppiumiOSTest. You can choose name as per your need. Click Finish once you are done.

clip_image027[4]

You will get following standard code in the class.

clip_image029[4]

Now you have got the skeleton of your code. You will not be able to run any test so far.

Start Writing Automation Test:

Replace content of AppiumiOSTest.java file with following code.

package com.Appiumtesting;

import io.Appium.java_client.ios.IOSDriver;

import java.net.MalformedURLException;

import org.openqa.selenium.By;

import org.openqa.selenium.remote.DesiredCapabilities;

import io.Appium.java_client.remote.HideKeyboardStrategy;

import io.Appium.java_client.AppiumDriver;

import io.Appium.java_client.MobileElement;

import io.Appium.java_client.remote.MobileCapabilityType;

import io.Appium.java_client.service.local.AppiumDriverLocalService;

import org.junit.*;

import java.net.URL;

import org.openqa.selenium.Point;

import org.openqa.selenium.ScreenOrientation;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.html5.Location;

public class AppiumiOSTest {

private IOSDriver driver;

@Before

public void setUp() throws MalformedURLException {

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability("Appium-version", "3.4.1");

capabilities.setCapability("platformName", "iOS");

capabilities.setCapability("platformVersion", "9.3");

capabilities.setCapability("deviceName", "iPhone 5");

capabilities.setCapability("app", "/Users/administrator/Documents/dev/ionicapps/AppiumTestApp/platforms/ios/build/emulator/AppiumTestApp.app");

driver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);

}

@Test

public void testiOS() throws InterruptedException {

driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[2]/UIAWebView[1]/UIAButton[1]")).click();

driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[2]/UIAWebView[1]/UIALink[2]/UIAStaticText[1]")).click();

driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[2]/UIAWebView[1]/UIATextField[1]")).sendKeys("jACk");

driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[2]/UIAWebView[1]/UIATextField[2]")).sendKeys("sMith");

driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[2]/UIAToolbar[1]/UIAButton[3]")).click();

driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[2]/UIAWebView[1]/UIAButton[2]")).click();

String text = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAScrollView[2]/UIAWebView[1]/UIAStaticText[5]")).getText();

Assert.assertEquals(text, "Jack Smith", text);

}

@After

public void tearDown() {

driver.quit();

}

}

xpath value will come from Appium inspector. You just need to change the screen to screen and click refresh button on Appium inspector and locate element and copy the xpath.

 

liked us?

Archives