概要

Nightwatchテストは、Androidエミュレータ、iOSシミュレータ、実機のAndroidおよびiOSデバイス、そしてクラウドプロバイダ上で実行できます。 Webオートメーションと同様に、ネイティブモバイルアプリのオートメーションも環境の概念を活用します。 テストスイートをどこでも実行するには、環境を一度設定し、テスト実行コマンドで使用すればよいだけです。

Androidエミュレータ

ステップ1

nightwatch.conf.jsファイルに環境が追加されていることを確認してください

nightwatch.conf.js
test_settings: {
  ...
  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
      }
  },
  
'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}/nightwatch/sample-apps/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 chromedriverExecutable: `${__dirname}/chromedriver-mobile/chromedriver`, newCommandTimeout: 0 } } }, ... }

ステップ2

次の値をアプリの値に置き換えてください

  1. app
  2. appPackage
  3. appActivity
  4. appWaitActivity

ステップ3

次のコマンドを使用してテストを実行します

npx nightwatch <path to tests> --env app.android.emulator

iOSシミュレータ

ステップ1

nightwatch.conf.jsファイルに環境を追加します。 app.ios.simulatorappを拡張するため、Androidエミュレータの例に示すように、appブロックも追加されていることを確認してください

nightwatch.conf.js

test_setting:{
  ...
  'app.ios.simulator': {
      extends: 'app',
      'desiredCapabilities': {
        // More capabilities can be found at https://github.com/appium/appium-xcuitest-driver#capabilities
        browserName: null,
        platformName: 'ios',
        // `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: 'XCUITest',
          // platformVersion: '15.5',
          deviceName: 'iPhone 13',
          // While Appium v1 supports relative paths,it's more safe to use absolute paths instead.
          // Appium v2 does not support relative paths.
          app: `${__dirname}/nightwatch/sample-apps/wikipedia.zip`,
          bundleId: 'org.wikimedia.wikipedia',
          newCommandTimeout: 0
        }
      }
    },
  ...
}

ステップ2

アプリに対応する値に置き換えてください

  1. app
  2. deviceName
  3. bundleId

ステップ3

次のコマンドを使用してテストを実行します

npx nightwatch <path to tests> --env app.ios.simulator

Android実機

お手持ちのAndroid実機でNightwatchテストを実行できます。 次の3つの簡単な手順に従って、実機のAndroidデバイスでテストが実行されることを確認してください。

ステップ1

nightwatch.conf.jsファイルに環境を追加します。 app.android.realappを拡張するため、Androidエミュレータの例に示すように、appブロックも追加されていることを確認してください

nightwatch.conf.js

'app.android.real': {
    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',
        // While Appium v1 supports relative paths,it's more safe to use absolute paths instead.
        // Appium v2 does not support relative paths.
        app: `${__dirname}/nightwatch/sample-apps/wikipedia.apk`,
        appPackage: 'org.wikipedia',
        appActivity: 'org.wikipedia.main.MainActivity',
        appWaitActivity: 'org.wikipedia.onboarding.InitialOnboardingActivity',
        // 'chromedriver' binary is required while testing hybrid mobile apps.
        // 
        // Set `chromedriverExecutable` to '' to use binary from `chromedriver` NPM package (if installed).
        // Or,put '--allow-insecure=chromedriver_autodownload' in `cli_args` property of `selenium`
        // config (see 'app' env above) to automatically download the required version of chromedriver
        // (delete `chromedriverExecutable` capability below in that case).
        chromedriverExecutable: '',
        newCommandTimeout: 0,
        // add device id of the device to run tests on,if multiple devices are online
        // Run command: `$ANDROID_HOME/platform-tools/adb devices` to get all connected devices
        // udid: '',
      }
    }
  },

ステップ2

次の値を置き換えてください

  1. app
  2. appPackage
  3. appActivity
  4. appWaitActivity
  5. udid 複数のデバイスがオンラインになっている場合、テストを実行するデバイスを識別するために必要

ステップ3

次の手順で、デバイスがNightwatchと通信するように正しく設定されていることを確認してください

  1. USBデバッグをオンにする AndroidデバイスでUSBデバッグをオンにし、データケーブルでシステムに接続します。
  2. Androidデバイスに最新バージョンのChromeブラウザがインストールされていることを確認してください。 インストールされていない場合は、Google Playストアからインストールしてください。
  3. プロジェクトに最新バージョンのchromedriver NPMパッケージがインストールされていることを確認してください。 インストールされていない場合は、次のコマンドを実行してインストールします
npm i chromedriver@latest --save-dev

ステップ4

次のコマンドを使用してテストを実行します

npx nightwatch <path to tests> --env app.android.real

iOS実機

Android実機でのテスト実行と同様に、iOS実機でもNightwatchテストを実行できます。

ステップ1

nightwatch.conf.jsファイルに環境を追加します。 app.ios.realappを拡張するため、Androidエミュレータの例に示すように、appブロックも追加されていることを確認してください

nightwatch.conf.js

'app.ios.real': {
    extends: 'app',
    'desiredCapabilities': {
      // More capabilities can be found at https://github.com/appium/appium-xcuitest-driver#capabilities
      browserName: null,
      platformName: 'ios',
      // `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: 'XCUITest',
        // While Appium v1 supports relative paths,it's more safe to use absolute paths instead.
        // Appium v2 does not support relative paths.
        app: `${__dirname}/nightwatch/sample-apps/wikipedia.zip`,
        bundleId: 'org.wikimedia.wikipedia',
        newCommandTimeout: 0,
        // add udid of the device to run tests on. Or,pass the id to `--deviceId` flag when running tests.
        // device id could be retrieved from Xcode > Window > 'Devices and Simulators' window.
        // udid: '00008030-00024C2C3453402E'
      }
    }
  },

ステップ2

アプリに対応する値に置き換えてください

  1. app
  2. deviceName
  3. bundleId
  4. udid 複数のデバイスがオンラインになっている場合、テストを実行するデバイスを識別するために必要

ステップ3

次のコマンドを使用してテストを実行します

npx nightwatch <path to tests> --env app.ios.real

BrowserStack

Nightwatchモバイルアプリテストは、BrowserStackなどのクラウドプロバイダーでも動作するため、3000以上のデバイスで並列実行を活用できます。

ステップ1

以下に示すように、nightwatch.conf.jsファイルにbrowserstackandroidios環境ブロックを追加します

nightwatch.conf.js

test_settings:{
  ...
  browserstack: {
    selenium: {
      host: 'hub.browserstack.com',
      port: 443
    },
    desiredCapabilities: {
      'bstack:options': {
        userName: '<username>',
        accessKey: '<access_key>',
        appiumVersion: '2.0.0'
      }
    },
    disable_error_log: false,
    webdriver: {
      timeout_options: {
        timeout: 60000,
        retry_attempts: 3
      },
      keep_alive: true,
      start_process: false
    }
  },
  'browserstack.android': {
    extends: 'browserstack',
    'desiredCapabilities': {
      browserName: null,
      'appium:options': {
        automationName: 'UiAutomator2',
        app: 'wikipedia-sample-app',// custom-id of the uploaded app
        appPackage: 'org.wikipedia',
        appActivity: 'org.wikipedia.main.MainActivity',
        appWaitActivity: 'org.wikipedia.onboarding.InitialOnboardingActivity',
        platformVersion: '11.0',
        deviceName: 'Google Pixel 5'
      },
      appUploadUrl: 'https://raw.githubusercontent.com/priyansh3133/wikipedia/main/wikipedia.apk',// url of app to be uploaded to BrowserStack before starting the test
      // appUploadPath: '../../../path/to/app_name.apk' // if the app needs to be uploaded to BrowserStack from local system
    }
  },
  'browserstack.ios': {
    extends: 'browserstack',
    'desiredCapabilities': {
      browserName: null,
      platformName: 'ios',
      'appium:options': {
        automationName: 'XCUITest',
        app: 'BStackSampleApp',
        platformVersion: '16',
        deviceName: 'iPhone 14'
      },
      appUploadUrl: 'https://www.browserstack.com/app-automate/sample-apps/ios/BStackSampleApp.ipa',
      // appUploadPath: '../../../path/to/app_name.ipa'
    }
  ...
}

ステップ2

アプリに対応する値に置き換えてください

  1. Browserstackユーザー名
  2. BrowserStackキー
  3. app
  4. appPackage
  5. appActivity
  6. appWaitActivity
  7. deviceName
  8. platformVersion
  9. appUploadUrl テスト対象アプリの公開URL

ステップ3

次のコマンドを使用してテストを実行します

npx nightwatch <path to tests> --env browserstack.android
npx nightwatch <path to tests> --env browserstack.ios

モバイルアプリテストのデバッグ方法を学ぶ