概觀

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 區塊,如 Android 模擬器範例所示,因為 app.ios.simulator 擴充了 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 模擬器範例所示,因為 app.android.real 擴充了 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. 開啟您 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 區塊,如 Android 模擬器範例所示,因為 app.ios.real 擴充了 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 測試中應用程式的公開網址

步驟 3

使用下列指令執行測試

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

了解如何偵錯行動應用程式測試