NightwatchJS for Native Mobile testing

Daniel Maioni
6 min readJul 18, 2023

Preparing environment for Native Mobile test automation

Requirements

  1. Install Appium V2

Add appium to your dev project

npm i appium --save-dev
appium -v
2.5.1

Note: As appium is officially version 2, we do not need anymore to use appium@next

2. Install Drivers

npx appium driver list

npx appium driver install uiautomator2

✔ Installing 'uiautomator2' using NPM install spec 'appium-uiautomator2-driver'
ℹ Driver uiautomator2@2.29.2 successfully installed
- automationName: UiAutomator2
- platformNames: ["Android"]

OR

npx appium driver install xcuitest

✔ Installing 'xcuitest' using NPM install spec 'appium-xcuitest-driver'
ℹ Driver xcuitest@4.32.21 successfully installed
- automationName: XCUITest
- platformNames: ["iOS","tvOS"]

To update use:

appium driver update uiautomator2
OR
appium driver update xcuitest

3. Create a NightwatchJS project for native mobile:

  • npm init nightwatch@latest <project-directory> - - - -app
  • Confirm if asked.
npm init nightwatch@latest nightwatch-appium-mobile -- --app

Need to install the following packages:
nightwatch@3.0.1
Ok to proceed? (y)

? Select language + test runner variant:
> JavaScript / default
? Select target mobile platform(s):
> Android (iOS is only available for Macs)
? Enter source folder where test files are stored:
> test
? Select where to run Nightwatch tests:
> On localhost
? Allow Nightwatch to collect completely anonymous usage metrics?:
> No
? Select target device(s):
> Both (Android and iOS)
[Emulator] Select browser(s) to set up on Emulator:
> Both (Google Chrome and Mozilla Firefox)
? Do you wish to setup the missing requirements for Appium?
Yes

NPM will install nightwatch, chrome and firefox webdrivers for hybrid apps, missing android binaries: avd, android emulator image (PIs Intel x86_6) and others.

4. Verify the environment with mobile-helper (Optional):

cd cd nightwatch-appium-mobile

npx @nightwatch/mobile-helper android --appium
OR
npx @nightwatch/mobile-helper xcuitest --appium

Checking the value of ANDROID_HOME environment variable...
✔ ANDROID_HOME is set to '/home/popos/Android/Sdk'

? Select target device(s): Both
? [Emulator] Select browser(s) to set up on Emulator: Both

Verifying the setup requirements for real devices/emulator...
✔ adb binary is present at '/home/popos/Android/Sdk/platform-tools/adb'
✔ avdmanager binary is present at '/home/popos/Android/Sdk/cmdline-tools/latest/bin/avdmanager'
✔ emulator binary is present at '/home/popos/Android/Sdk/emulator/emulator'

✔ platforms subdirectory is present at '/home/popos/Android/Sdk/platforms'

✔ nightwatch-android-11 AVD is present and ready to be used.

Making sure adb is running...
Success! adb server is running.

Verifying if browser(s) are installed...

Checking if AVD is already running...
! 'nightwatch-android-11' AVD not found running!

Launching emulator with 'nightwatch-android-11' AVD...
✔ 'nightwatch-android-11' AVD launched!

Waiting for emulator to boot up...
✔ Boot up complete!

Making sure adb has root permissions...
✔ adb is running with root permissions!

Verifying if Firefox is installed...
✔ Firefox browser is installed in the AVD.

Checking the version of installed Firefox browser...
✔ Your Firefox browser is up-to-date.

Verifying if Chrome is installed...
✔ Chrome browser is installed in the AVD.

Checking the version of installed Chrome browser...
Version: 83.0.4103.106

Note: Automatic upgrade of Chrome browser is not supported yet.

Checking if chromedriver is already downloaded...
✖ chromedriver not found at '/home/popos/Projetos/nightwatch-appium-mobile/chromedriver-mobile/chromedriver'

? Do you wish to setup the missing requirements for Chrome browser? Yes

Downloading chromedriver to work with the factory version of Chrome browser...
[████████████████████████████████████████] 100% | ETA: 0s
Done! chromedriver downloaded at '/home/popos/Projetos/nightwatch-appium-mobile/chromedriver-mobile/chromedriver'

You can run your tests now on your Android Emulator's Chrome browser.

Closing emulator...
Emulator will close shortly. If not, please close it manually.

Success! All requirements are set.
You can go ahead and run your tests now on an Android device/emulator.

Note: Please make sure you have required browsers installed on your real-device before running tests.

Great! All the requirements are being met.
You can go ahead and run your tests now on an Android device/emulator.

5. Setup selenium to support Appium V2 instead of V1

Update the nightwatch.conf.js file:

a. uncomment the line default_path_prefix

b. uncomment the line --allow-insecure=chromedriver_autodownload

app: {
selenium: {
start_process: true,
use_appium: true,
host: 'localhost',
port: 4723,
server_path: '',
// args to pass when starting the Appium server
cli_args: [
// automatically download the required chromedriver
'--allow-insecure=chromedriver_autodownload'
],
// Uncomment below line when using Appium v2
default_path_prefix: ''
},
webdriver: {
timeout_options: {
timeout: 150000,
retry_attempts: 3
},
keep_alive: false,
start_process: false
}
},

The Nightwatch.conf.js file will be create with pre-configured environment profiles:

  • app.android.emulator — to connect to your android
  • app.android.real — to connect to your real android device
  • ios emulator — to connect to your ios emulator
  • ios real — to connect to your real ios device
// environment to run tests on Android emulator
'app.android.emulator': {
extends: 'app',
'desiredCapabilities': {
// More capabilities can be found at https://github.com/appium/appium-uiautomator2-driver#capabilities
browserName: null,
platformName: 'android',
// `appium:options` is not natively supported in Appium v1, but works with Nightwatch.
// If copying these capabilities elsewhere while using Appium v1, make sure to remove `appium:options`
// and add `appium:` prefix to each one of its capabilities, e.g. change 'app' to 'appium:app'.
'appium:options': {
automationName: 'UiAutomator2',
// Android Virtual Device to run tests on
avd: 'nightwatch-android-11',
// While Appium v1 supports relative paths, it's more safe to use absolute paths instead.
// Appium v2 does not support relative paths.
app: `${__dirname}/wikipedia.apk`,
appPackage: 'org.wikipedia',
appActivity: 'org.wikipedia.main.MainActivity',
appWaitActivity: 'org.wikipedia.onboarding.InitialOnboardingActivity',
// chromedriver executable to use for testing web-views in hybrid apps.
// add '.exe' at the end below (making it 'chromedriver.exe') if testing on windows.
chromedriverExecutable: `${__dirname}/chromedriver-mobile/chromedriver`,
newCommandTimeout: 0
}
}
},

New environments can be created, here are some for Browser Stack:

// environment to run tests on Android devices at browserstack
'browserstack.android': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'chrome',
'bstack:options': {
deviceName: 'Samsung Galaxy S20',
osVersion: '10.0'
},
'goog:chromeOptions': {
w3c: true
}
}
},

// environment to run tests on iOS devices at browserstack
'browserstack.ios': {
extends: 'browserstack',
desiredCapabilities: {
browserName: 'safari',
'bstack:options': {
deviceName: 'iPhone 12',
osVersion: '14'
}
}
},

6. Running a sample test case

npx nightwatch <test case path/test.js> — env <environment available at config file>

For Real Android device:

  • Make sure your device is connected with USB Debugging turned on.
  • Make sure required browsers are installed.
# to run one single test
npx nightwatch ./nightwatch/examples/mobile-app-tests/wikipedia-android.js --env app.android.real

OR

# to run all test cases
npx nightwatch ./nightwatch/examples/mobile-app-tests/ --env app.android.real

For Emulator Android device:

# to run one single test
npx nightwatch ./nightwatch/examples/mobile-app-tests/wikipedia-android.js --env app.android.emulator

OR

# to run all test cases
npx nightwatch ./nightwatch/examples/mobile-app-tests/ --env app.android.emulator

Starting Appium Server on port 4723...

[Wikipedia Android app test] Test Suite
──────────────────────────────────────────────────────────────────────────────
ℹ Connected to localhost on port 4723 (70888ms).
Using: wikipedia on ANDROID (11).

Running Search for BrowserStack:
───────────────────────────────────────────────────────────────────────────────────────────────────
✔ Testing if element's <.pcs-edit-section-title> inner text equals 'BrowserStack' (174ms)

✨ PASSED. 1 assertions. (29.978s)
Wrote HTML report file to: /home/popos/Projetos/appium/tests_output/nightwatch-html-report/index.html
Now everything is fine to start automation, let`s understand some things first!

The sample application wikipedia.apk will be installed into your device, and be started to run the sample test case examples/mobile-app-testes/wikipedia-android.js

It will also register a test report at tests_output/nightwatch-html-report/index.html

Test Report in .HTML — with information about test status, performance, test steps details and HTTP requests, the enviroment used, and a option to filter tests as passed, failed or skipped
Test Output Report

For more information about Appium Server and Appium Inspector, check it out our last post: https://medium.com/@dmaioni/appium-server-and-appium-inspector-ff44e25d804

For more information about Native Mobile testing instead of Desktop one, check it out our last post:

Nightwatch logo — a brown owl on a branch
Nightwatch logo

--

--