NightwatchJS

Preparing environment for Mobile test automation

Requirements

  1. Install Appium V2

Add appium to your dev project

npm i appium --save-dev
appium -v
2.0.0

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 [email protected] successfully installed
- automationName: UiAutomator2
- platformNames: ["Android"]

OR

npx appium driver install xcuitest

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

3. Create a NightwatchJS project for mobile:

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

Need to install the following packages:
  [email protected]
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 mobile-appium-project
npx @nightwatch/mobile-helper android --appium
OR
npx @nightwatch/mobile-helper ios --appium

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-configurated 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 ones can be created:

Browser-stack Desired capabilities

// 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 - env

For Real Android device:

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

For Emulator Android device:

npx nightwatch ./nightwatch/examples/mobile-app-tests/wikipedia-android.js --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 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

Nightwatch logo
19/07/2023