在真实、虚拟和云设备上运行测试
概述
Nightwatch 测试可以在 Android 模拟器、iOS 模拟器、真实的 Android 和 iOS 设备以及云提供商上运行。类似于 Web 自动化,原生移动应用自动化也利用了 环境 的概念。要在任何地方运行测试套件,您只需配置一次环境并在测试运行命令中使用它即可。
Android 模拟器
步骤 1
确保在 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
确保您将以下值替换为您的应用程序的值
- app
- appPackage
- appActivity
- appWaitActivity
步骤 3
使用以下命令运行测试
npx nightwatch <path to tests> --env app.android.emulator
iOS 模拟器
步骤 1
在 nightwatch.conf.js
文件中添加环境。确保如 Android 模拟器示例所示添加了 app
块,因为 app.ios.simulator
扩展了 app
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
确保您将以下内容替换为与您的应用程序相对应的值
- app
- deviceName
- bundleId
步骤 3
使用以下命令运行测试
npx nightwatch <path to tests> --env app.ios.simulator
真实的 Android 设备
您可以在您可能拥有的真实的 Android 设备上运行 Nightwatch 测试。按照这三个简单的步骤,您将看到测试在您的真实的 Android 设备上执行。
步骤 1
在 nightwatch.conf.js
文件中添加环境。确保如 Android 模拟器示例所示添加了 app
块,因为 app.android.real
扩展了 app
'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
确保您替换以下值
- app
- appPackage
- appActivity
- appWaitActivity
- udid
需要识别需要运行测试的设备,如果有多个设备在线
步骤 3
确保您的设备已正确设置以通过以下步骤与 Nightwatch 进行通信
- 在您的 Android 设备上打开 USB 调试 并通过数据线将其连接到您的系统。
- 确保您的 Android 设备上安装了最新版本的 Chrome 浏览器。如果没有,请从 Google Play 商店安装。
- 确保在您的项目中安装了最新版本的 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
文件中添加环境。确保如 Android 模拟器示例所示添加了 app
块,因为 app.ios.real
扩展了 app
'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
确保您将以下内容替换为与您的应用程序相对应的值
- app
- deviceName
- bundleId
- udid
需要识别需要运行测试的设备,如果有多个设备在线
步骤 3
使用以下命令运行测试
npx nightwatch <path to tests> --env app.ios.real
BrowserStack
Nightwatch 移动应用测试也可以与 BrowserStack 等云提供商合作,以便您可以在 3000 多台真实的设备上利用并行运行。
步骤 1
在 nightwatch.conf.js
文件中添加 browserstack
& android
& ios
环境块,如下所示
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
确保您将以下内容替换为与您的应用程序相对应的值
- Browserstack 用户名
- BrowserStack 密钥
- app
- appPackage
- appActivity
- appWaitActivity
- deviceName
- platformVersion
- appUploadUrl
被测应用程序的公共 URL
步骤 3
使用以下命令运行测试
npx nightwatch <path to tests> --env browserstack.android
npx nightwatch <path to tests> --env browserstack.ios
推荐的下一步
了解 如何调试移动应用程序测试