Mobile App Testing

Mobile Native App/Web App testing using Appium and Selendroid(for Android)

Created by Kavan Sheth



Best viewed in Chrome, Firefox, Opera or A browser supporting HTML5. Use arrow keys ( up ↑, down ↓, left ←, right ↑) to navigate. Uses Reveal.js by Hakim El Hattab Press ESC to enter the slide overview.

Introduction

  • As world moving towards mobile, now a days it is necessary to know how to automate your tests for Mobile Applications.
  • Appium provides ability to test both kind of apps using webdriver.(Yes, with Appium you can test native apps as well!!)
  • Appium supports only Android version >= 4.2, But lower versions are supported through Appium's bundled version of Selendroid.
  • Here we will mainly focus on android, as I do not have OS X machine or any device with iOS :(.

Appium



Appium Installation



  • I was going through appium.io for installation of appium. Initially I found it scary :P, but after looking here and there I decided to follow it and believe me setting up for Appium is not that hard.
  • Section of your interest will be http://appium.io/slate/en/master/?java#running-appium-on-windows
  • for Appium you need to install following:
    • node.js
    • Android SDK API >=17
    • Appium and Appium Client Libs

node.js

Android SDK

  • For Android SDK, you can either download eclipse ADT from http://developer.android.com/sdk/index.html
  • Otherwise you need to install SDK and ADT plugin separately for existing Eclipse.
  • Installing SDK:
    • Just scroll down a bit and you will get standalone SDK under "GET SDK FOR AN EXISTING IDE".
    • Once you run installer, it will install Android SDK Tools and open Android SDK manager for further installation.
    • Make sure that you also download "Android Platform-tools"
    • and then set Path variable similarly "C:\Android\sdk\tools;C:\Android\sdk\platform-tools"
  • Installing ADT:

Appium

  • Install Appium as node-module using npm(node package manager) as following:
  • npm install -g appium(run from command prompt(cmd))
  • OR you can download it from http://appium.io/downloads.html


Appium Client Libs


  • "These libraries wrap standard Selenium client libraries to provide all the regular selenium commands dictated by the JSON Wire protocol, and add extra commands related to controlling mobile devices, such as multi-touch gestures and screen orientation."
  • So apart from Selenium client lib of specific language, you will need appium client lib.
  • Which you can download from http://appium.io/downloads.html

Before Moving Ahead

  • in Command prompt, run appium command, you should get similar output as following:
  • if command is not identified then there is some issue with your appium installation.
  • to check devices(real or virtual) connected with/running on your system. use adb devices command on command prompt.
  • Android Debug Bridge(adb) is daemon which lets you communicate with virtual or real android based devices.You should get something like following:

Setup your AVD(Android virtual device)

  • Once you install Eclipse ADT plugin, In Eclipse, Go to Windows -> Android Virtual Device Manager.
  • Configure a AVD by clicking on create button. For me it was as following:
  • Once configured, Start it. AVD launch may take some time(taking 5-10 minutes for me). So don't panic. When it will be fully launched it will be just like your mobile screen with few extra buttons and you can play with it. If you are able to start your avd successfully, we are ready to take off :).

Automating Mobile Web Apps



//setup the web driver and launch the webview app.
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
URL url = new URL("http://127.0.0.1:4723/wd/hub");
AppiumDriver driver = new AppiumDriver(url, desiredCapabilities);

// Navigate to the page and interact with the elements on the guinea-pig page using id.
driver.get("http://saucelabs.com/test/guinea-pig");
WebElement div = driver.findElement(By.id("i_am_an_id"));
Assert.assertEquals("I am a div", div.getText()); //check the text retrieved matches expected value
driver.findElement(By.id("comments")).sendKeys("My comment"); //populate the comments field by id.

//close the app.
driver.quit();
  • We will try to run this code and make required changes one by one.
  • Change #1 - Remove compilation errors

    • Add given code snippet to main method of a newly created class
    • Add Selenium libs to your build path
    • Add Appium Client lib to your build path.(e.g. java-client-2.1.0.jar). downloaded under section Java Client Libs
    • Now you will be getting only error is "Cannot instantiate the type AppiumDriver"
    • just Googling a bit you will find that with JavaClient2.0, you need to use either AndroidDriver or IOSDriver. here we will use AndroidDriver. so changes
      AppiumDriver driver = new AppiumDriver(url, desiredCapabilities);
      as following
      AndroidDriver driver = new AndroidDriver(url, desiredCapabilities);
    • Also you have to catch or throw MalformedURLException.

    After removing compilation errors your code should look like following:

    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.android.AndroidDriver;
    import io.appium.java_client.remote.MobileCapabilityType;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import org.junit.Assert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    public class androidAuto {
    
    	public static void main(String[] args)
    	{
    		//setup the web driver and launch the webview app.
    		DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    		desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Safari");
    		URL url=null;
    		try {
    			url = new URL("http://127.0.0.1:4723/wd/hub");
    		} catch (MalformedURLException e) {
    			e.printStackTrace();
    		}
    		AndroidDriver driver = new AndroidDriver(url, desiredCapabilities);
    
    		// Navigate to the page and interact with the elements on the guinea-pig page using id.
    		driver.get("http://saucelabs.com/test/guinea-pig");
    		WebElement div = driver.findElement(By.id("i_am_an_id"));
    		Assert.assertEquals("I am a div", div.getText()); //check the text retrieved matches expected value
    		driver.findElement(By.id("comments")).sendKeys("My comment"); //populate the comments field by id.
    
    		//close the app.
    		driver.quit();
    	}
    }

    Now we will run this piece of code and remove run time errors.

    Change #2 - Remove setup related errors

    • If you are getting exception org.openqa.selenium.remote.UnreachableBrowserException with Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
      It means your appium is down and appium is node.js based server. your test script communicates with appium using HTTP based RESTFul APIs and server communicates with your device using either UIAutomation(IOS), UIAutomator(Android > 4.2) or Selendroid(Android > 2.3). and when you start appium it starts with default port 4723. with following message on console: info: Appium REST http interface listener started on 0.0.0.0:4723

    Change #3 - Remove capability related errors

    • If you are getting exception org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: The following desired capabilities are required, but were not provided: deviceName)

    • Message clearly tells you that deviceName capability is mandatory. so lets add this capability as per documentation. To run on emulator we need it as following:
      desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");

    Change #4 - Remove capability related errors - cont.

    • Did you notice one more thing? we have BROWSER_NAME capability set as Safari. but we will be running it on avd with default browser.

    • As per Documentation, So we need to reset it as following:
      desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Browser");

    Change #5 - Remove one more setup related error

    • If you are getting exception org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not find a connected Android device.)
      It is also straight forward, you haven't connected your device. So here we will launch our configured AVD. Also note that apart from getting these exception in Eclipse console, we will get these exception in cmd window where appium server is running.

    Change #6 - Some peculiar error

    • If you are getting exception Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: A new session could not be created.

    • Hmm. Error doesn't say much, let's check cmd logs. If you get something like following:

      It is a strange error possibly a bug. which I could not resolve, but while searching found that this might be an issue with chromedriver.exe used by appium. At https://discuss.appium.io/t/did-not-get-session-redirect-from-chromedriver/1086 @ramshankar_testing has provided chromedriver_dev.exe. which is compiled from source to ignore chrome version. Follow the step given in discussion and replace your chromedriver.exe with downloaded exe.
      Note 1: If you have installed appium using npm then you will find appium at C:\Users\XXXXX\AppData\Roaming\npm\node_modules.
      Note 2: Before running your test again make sure that no instance of chromedriver.exe is running in your task manager.


      this issue is resolved in chromedriver version 2.13

    Change #6 - Some peculiar error - Cont.

    • If you are getting exception Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: A new session could not be created.

    • But Appium logs are not having error for chrome version. but just following:

      error: Chromedriver create session did not work. Status was 200 and body was 
      					{"sessionId":"ca8c519a79d5ed140bbfc153bdd8ec3f","status":100,"value":{"message":"chrome not reachable\n 
      					(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64)"}}
      info: [debug] Cleaning up appium session
      error: Failed to start an Appium session, err was: Error: Did not get session redirect from Chromedriver
      info: [debug] Error: Did not get session redirect from Chromedriver
      at null. (d:\Job\appium git\appium\lib\devices\android\chromedriver.js:220:12)

      In this case, you may try replacing chromedriver.exe provided by appium, with latest chromedriver version which is 2.12 as of now, as suggested in this post .
      Note 1: Make sure you restart your AVD or atleast make sure that browser working properly after earlier failure.(otherwise browser will keep on crashing)
      Note 2: Path for chromedriver.exe is appium\build\chromedriver\windows under your appium installation.

    Few Points

    • After these changes your code should work fine. It will open given link in browser. find an element and asserts its text and then add comment.
    • You still might be wondering how to find elements with mobile browsers.
    • don't worry. Appium provides tool to find element. which we will discuss in next section.
    • Apart from these if you face any issue, you have to search online. Appium forum will be a good start.

    Finding Elements



    Using Appium Inspector

    • Appium Inspector can be an easiest way to find element details, if it works. It works properly with iOS. few online post suggest that Appium Inspector is not stable with Android. I was having trouble and couldn't get it work. So you can try your luck. we will see steps to start Appium Inspector briefly here.
    • for Appium Inspector you need to download Appium for Windows from here.
    • Appium.exe provides GUI to manage Appium and Appium Inspector

    you can start appium by clicking on play button on right top corner.

    you first need to click on refresh to load current screen of your emulator or device. In my case, inspector launches browser on emulator but keep on getting "error getting screenshot". Not able to resolve it yet.