The challenges of hybrid app automation
Mobile application testing comes with its own set of challenges. For example, it is preferable to automate the functional regression test cases since they typically consume a huge chunk of your testing time. However, running automated regression tests (on a deadline) for hybrid mobile apps can be challenging. We faced this very challenge while testing a hybrid mobile app for the hospice industry, and we’re going to talk about how we handled it.

We used Appium to create a single test script for multiple platforms.
This blog is a step by step guide to setting up and using the Appium server for hybrid mobile application development. We’ll talk about the complete installation of the latest Appium version (1.6.4). As of today, this is the only version which supports Android 7 (Nougat) and iOS 10.3.

What is Appium?
Appium is an open-source automation tool that can be used to automate native and hybrid mobile applications for both iOS and Android platforms. With Appium, cross-platform testing is simple since the same test scripts can be used for for multiple platforms.

Appium for hybrid app automation:

If the app has to be tested for both the iOS and Android platforms, it’s better to have Appium installed on a Mac.

  • Appium 1.6.4 requires XCODE 8.2 and it can be installed only on MacOS 10.12 and above, so if you haven’t already, update your Mac to 10.12 (Sierra) or later.
  • Download and install JDK 7 or 8 (we recommend 8).
  • You can find the command line tools on the downloads page under the “Get Just The Command Line Tools” title to download the latest SDK.
  • Java client version 4.1.2 is recommended.
  • We used the IntelliJ IDEA 2017 IDE to develop the automation script, and then set the JDK and SDK in a project structure.
  • Update system preference to open bash_profile on Terminal (nano ~/.bash_profile).

Eg:

export ANDROID_HOME=/Users/jenkins/Library/Android/sdk   //sdk path
export PATH=$ANDROID_HOME/platform-tools:$PATH
export JAVA_HOME=$(/usr/libexec/java_home)   //java path

Appium Installation for hybrid apps:

  • Update your brew.
  • brew update
  • Install npm (v4.2.0 or above) using the command line.
    npm install -g npm
  • Install node (v7.9.0 or above) using the command line.
    brew install node
  • Install appium (v1.6.4 or above) using the command line.
    npm install -g appium@1.6.4
  • In addition to the Git submodules mentioned below (see Development), this package currently depends on libimobiledevice to do certain things. Install it with Homebrew,
    brew install libimobiledevice –HEAD # install from HEAD to get important updates
    brew install ideviceinstaller # only works for iOS 9. for iOS 10, see below
  • There is also another dependency, made necessary by Facebook’s WebDriverAgent, for the Carthage dependency manager. If you do not have Carthage on your system, it can also be installed with Homebrew. To do this, run these commands one by one:
                 
cd/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
brew install carthage
npm i -g webpack
./Scripts/bootstrap.sh -d
  • ideviceinstaller doesn’t work with iOS 10 yet. So, you will need to install iOS-deploy (npm install -g ios-deploy)
  • For real devices we can use Xcpretty to make the Xcode output more reasonable. This can be installed using: gem install xcpretty

Webdriver Installation and prerequisites:

  • While installing Appium 1.6.4 , the “Configuration” file at the Webdriver Agent Package may be missing, so check for the Configuration file on Appium’s Webdriver Agent file. If you can’t find it, download it from here and just copy paste the Configuration file into your Appium Webdriver Agent folder. For more information, take a look at the screenshot below.
  • Open your Webdriver Agent folder through the finder. If you don’t know the location of Appium, run this command in Terminal:

which appium

Sample output:  /usr/local/bin/appium

  • Choose “WebDriverAgent.xcodeproj” and open it with Xcode.
appium-automation-process
Appium automation: Choosing WebDriveAgent
  • You will need to make the following changes in the Webdrivers Xcode project:

1. Select “WebDriverAgentLib” as the target and go to the “General” tab. Check Automatic Signing, and then ask your development team to give you a developer id.

appium-automation-webdriverlib
Appium automation – Selecting theWeb Driver Agent

2. Repeat step 1, this time selecting “WebDriverAgentRunner” as the target.

appium-automation-webdriver-runner
Appium automation – Selecting the Web Driver Runner

Now, run the program in Xcode.

Items to be added in Capabilities:

  •  If you are running an Android project, add the following to your capabilities in order to support Android 7 (Nougat)

desiredCapabilities.setCapability(“automationName”, “uiautomator2”);

  • If you are running an iOS project, add the following to your capabilities in order to support iOS 10:

desiredCapabilities.setCapability(“automationName”, “XCUITest”);

desiredCapabilities.setCapability(“startIWDP”,true); // this will auto-handle the “ iOS Webkit Debug Proxy”     

How to inspect elements in native applications:

  • If you are  automating a native Android application, use “uiautomator” from SDK Platform Tools to inspect native Android elements. Just drag the uiautomator from SDK Platform Tools and drop it in Terminal and run the command it will be started and you can inspect elements.

If you need to inspect native elements for an iOS application, you’ll need to do a little workaround. Take a look at this, because the GUI version for Appium 1.6.4 is still under development.

How to inspect elements in hybrid applications:

  • If you need to inspect elements for hybrid applications, you can easily do so with “chrome://inspect/#devices” Note that this will support only Android devices, and you should have latest version of Chrome.
  • If you need to inspect hybrid applications on the iOS platform, you can use the Safari browser.
  • However,  with hybrid apps, every web element is the same for both iOS and Android, so it will suffice to pick just one for inspection.

Tips for hybrid automation:

  • One of the most common mistakes is to write your hybrid automation script similar to the native app automation script.  The problem is, the script’s behavior is unpredictable. The UI elements differ between multiple device models, so code that works for one device might not work for another. In fact, this model will also require you to write separate scripts for Android and iOS mobile apps.
  • When you work with hybrid apps, you should use “WEBVIEW” and not “NATIVE_VIEW”. This single step avoids separate scripting for iOS and Android.
  • To automate hybrid apps, use any of the above methods to identify elements. For more information, refer to this site and look into the “Using chrome remote debugger” option.
  • Always get the available contexts in a view, then switch to the available Webview context.
                 
public void switchToWebView() {
Set<String> availableContexts = driver.getContextHandles();
logger.info("Total No of Context Found After we reach to WebView = " + availableContexts.size());
for (String context : availableContexts) {
if (context.contains("WEBVIEW")) {
logger.info("Context Name is " + context);
driver.context(context);
break;
}
}
}

 

The above code will get the list of contexts available and set the webview context

  •  In order to switch back to native view of application:
                 
public static void switchToNativeView() {
   driver.context("NATIVE_APP");
   logger.info("Context switched to " + "NATIVE_APP");
}

How to handle network scenarios:

  •  Appium provides an API for enabling/disabling network connections and it supports only Android. For more info, read this.

Eg: driver.setConnection(Connection.AIRPLANE); //supported in Java client 4.1.2

Note: You can use this API only in Native views; if you are using Webview switch context to native view first.

Have something to add to the conversation? We’re all ears!

Leave a Reply