Skip to content

Testing Android native application and mobile browsers with Appium

Andrei Rusu edited this page Oct 17, 2019 · 6 revisions

Testing Android native application

Getting started

1. Download Appium - install the latest version of the Appium for your platform from the NPM.

2. Configure the Appium host and port - Set values in your nightwatch.json, inside "test_settings" section like so:

  "test_settings" : {
    "androidNative":{
	"selenium_start_process": false,
	"selenium_port" : 4723,
	"selenium_host" : "127.0.0.1",

Android Capabilities

This is a list of necessary Android desired capabilities:

      "desiredCapabilities": {
        "automationName":"Appium",
        "browserName":"",
	"app":"./examples/tests/app/calc.apk",
        "appPackage":"com.google.android.calculator",
        "appActivity":"com.android.calculator2.Calculator",
        "platformName": "Android",
        "platformVersion": "9.0.0",
        "deviceName": "test"
      }
Name Description
automationName Which automation engine to use, Appium (default) or UiAutomator2 or Espresso
app The absolute local path to a .apk file
appPackage Java package of the Android app you want to run
appActivity Activity name for the Android activity you want to launch from your package
platformName Which mobile OS platform to use
platformVersion Mobile OS version
deviceName The kind of mobile device or emulator to use

For a complete list of supported Appium capabilities please refer to Appium Capabilities

Create a first test

Create the first test "calc.js" in tests folder, in this test we will test calculator application. Test will tap on 5 button, then "+" button and then again on 5 button. Finally it will tap on "=" button and check that result is 10:

module.exports = {
  tags: ['androidNative'],
  '@disabled': false,

  'Add two numbers' : function (client) {
    client
      .useXpath()
      .click('//android.widget.Button[@resource-id="com.google.android.calculator:id/digit_5"]')
      .click('//android.widget.Button[@resource-id="com.google.android.calculator:id/op_add"]')
      .click('//android.widget.Button[@resource-id="com.google.android.calculator:id/digit_5"]')
      .click('//android.widget.Button[@resource-id="com.google.android.calculator:id/eq"]')
      .assert.containsText('//android.widget.TextView[@resource-id="com.google.android.calculator:id/result"]', '10')
    client.end();
  }
};

Execute test

Open a terminal to run Appium sever, we installed the Appium server via npm, so we ran server by this command:

appium

*This will run the Appium server on localhost:4723.

Open another terminal and type following:

$ npx nightwatch ./nightwatch.json ./examples/tests/calc.js --tag androidNative -e androidNative

Done

Test was paased and we received following results:

[Calc] Test Suite
=================
Running:  Add two numbers

√ Testing if element <//android.widget.TextView[@resource-id="com.google.android.calculator:id/result"]> contains text: "10"  - 1253 ms.

OK. 1 assertions passed. (6.628s)

Running test in Android Chrome mobile browser

Getting started

1. Download Appium - install the latest version of the Appium for your platform from the NPM. 2. Download ChromeDriver - download the latest version of the ChromeDriver for your platform from the Downloads page. 3. Configure the Appium host and port - Set values in your nightwatch.json, inside "test_settings" section like so:

  "test_settings" : {
    "androidBrowser":{
	"selenium_start_process": false,
	"selenium_port" : 4723,
	"selenium_host" : "127.0.0.1",

Android Capabilities

This is a list of necessary Android desired capabilities:

    "desiredCapabilities": {
        "automationName":"Appium",
        "browserName": "Chrome",
	"chromedriverExecutable": "./chromedriver.exe",
        "platformName": "Android",
        "platformVersion": "9.0.0",
        "device": "Android",
        "deviceName": "test"
      }
Name Description
automationName Which automation engine to use, Appium (default) or UiAutomator2 or Espresso
browserName Name of mobile web browser to automate
chromedriverExecutable The absolute local path to webdriver executable
appPackage Java package of the Android app you want to run
appActivity Activity name for the Android activity you want to launch from your package
platformName Which mobile OS platform to use
platformVersion Mobile OS version
deviceName The kind of mobile device or emulator to use

For a complete list of supported Appium capabilities please refer to Appium Capabilities

Create a first test

Create the first test "ecosia.js" in tests folder, in this test we will test search in Ecosia search engine:

module.exports = {
  tags: ['androidBrowser'],
  '@disabled': false,

  'Demo test Ecosia' : function (client) {
    client
      .url('https://www.ecosia.org/')
      .waitForElementVisible('body')
      .assert.titleContains('Ecosia')
      .assert.visible('input[type=search]')
      .setValue('input[type=search]', 'nightwatch')
      .assert.visible('button[type=submit]')
      .click('button[type=submit]')
      .assert.containsText('.mainline-results', 'Nightwatch.js')
      .end();
  }
};

Execute test

Open a terminal to run Appium sever, we installed the Appium server via npm, so we ran server by this command:

$ appium

*This will run the Appium server on localhost:4723.

Open another terminal and type following:

$ npx nightwatch ./nightwatch.json ./examples/tests/calc.js --tag androidBrowser -e androidBrowser

Done

Test was paased and we received following results:

[Google] Test Suite
===================
Running:  Demo test Google

√ Element <body> was visible after 208 milliseconds.
√ Testing if the page title contains "Ecosia"  - 61 ms.
√ Testing if element <input[type=search]> is visible  - 161 ms.
√ Testing if element <button[type=submit]> is visible  - 129 ms.
√ Testing if element <.mainline-results> contains text: "Nightwatch.js"  - 559 ms.

OK. 5 assertions passed. (3.898s)