Nightwatch 的移动应用测试安装
概述
为了使用 Nightwatch 运行移动应用测试,需要安装一些东西
- Appium
- 命令行工具
- 各个平台的 SDK
- 虚拟设备
但是,Nightwatch 通过 Mobile Helper 工具简化了所有这些的设置。
设置新项目
运行时
npm init nightwatch
<directory-name>
出现的第一个问题是
确保选择了移动应用测试。其余的将是一系列您需要经历的问题/步骤,Nightwatch 将完成安装所有必需内容或在需要时提供说明的繁重工作。
如果您只打算进行移动应用测试,则可以使用以下命令使用应用模式启动 Nightwatch 安装
npm init nightwatch@latest <foldername> -- --app
如果您选择 Android,需要注意 1 点。只有在您没有安装 SDK 时才选择默认选项,以避免重复下载 SDK。如果您已经安装了 SDK,请在出现此问题时提供路径。
就是这样。Nightwatch 移动应用测试设置完成!
将移动应用测试添加到现有项目
Android
步骤 1
转到 Nightwatch 项目目录并运行以下命令
npx @nightwatch/mobile-helper android --appium
步骤 2
根据您的需求回答问题。
如果您选择 Android,需要注意 1 点。只有在您没有安装 SDK 时才选择默认选项,以避免重复下载 SDK。如果您已经安装了 SDK,请在出现此问题时提供路径。
s步骤 3
验证后,如果所有要求都没有满足或出现错误,请按照说明解决这些问题。
步骤 4
接下来,使用以下命令在项目中设置 Appium 2。
npm i appium@next --save-dev
步骤 5
为 Android 安装 Appium UiAutomator2 驱动程序
npx appium driver install uiautomator2
步骤 6
下载示例 wikipedia 应用 并将其保存到项目的根目录(与 nightwatch.conf.js 文件并排)。
步骤 7 为 Android 模拟器和真实设备添加 Nightwatch 环境。
{
...
'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'
],
// Remove below line if using Appium v1
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}/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
}
}
},
'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: '',
}
}
},
}
}
步骤 8 将以下示例测试文件添加到 nightwatch/examples/mobile-app-tests/wikipedia-android.js
文件下
describe('Wikipedia Android app test',function(){
before(function(app) {
app.click('id','org.wikipedia:id/fragment_onboarding_skip_button');
});
it('Search for BrowserStack',async function(app) {
app
.click('id','org.wikipedia:id/search_container')
.sendKeys('id','org.wikipedia:id/search_src_text','browserstack')
.click({selector: 'org.wikipedia:id/page_list_item_title',locateStrategy: 'id',index: 0})
.waitUntil(async function() {
// wait for webview context to be available
const contexts = await this.appium.getContexts();
return contexts.includes('WEBVIEW_org.wikipedia');
})
.appium.setContext('WEBVIEW_org.wikipedia')
.assert.textEquals('.pcs-edit-section-title','BrowserStack'); // command run in webview context
});
});
完成!🎉 您的 Android 设置现在已完成。
iOS
步骤 1
转到 Nightwatch 项目目录并运行以下命令
npx @nightwatch/mobile-helper ios --setups
步骤 2
根据您的需求回答问题。
步骤 3
验证后,如果所有要求都没有满足或出现错误,请按照说明解决这些问题。
步骤 4
完成此操作后,使用以下命令在项目中设置 Appium 2
npm i appium@next --save-dev
步骤 5
使用以下命令为 iOS 安装 Appium XCUITest 驱动程序
npx appium driver install xcuitest
步骤 6
下载示例 wikipedia 应用 并将其保存到项目的根目录(与 nightwatch.conf.js 文件并排)。
步骤 7 为 iOS 模拟器和真实设备添加 Nightwatch 环境
{
...
'test_settings':{
// other envs above this line
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'
],
// Remove below line if using Appium v1
default_path_prefix: ''
},
webdriver: {
timeout_options: {
timeout: 150000,
retry_attempts: 3
},
keep_alive: false,
start_process: false
}
},
'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}/wikipedia.zip`,
bundleId: 'org.wikimedia.wikipedia',
newCommandTimeout: 0
}
}
},
'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}/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'
}
}
},
}
}
步骤 8 将以下示例测试文件添加到 nightwatch/examples/mobile-app-tests/wikipedia-ios.js
文件下
describe('Wikipedia iOS app test',function() {
before(function(app) {
app.click('xpath','//XCUIElementTypeButton[@name="Skip"]');
});
it('Search for BrowserStack',async function(app) {
app
.useXpath()
.click('//XCUIElementTypeSearchField[@name="Search Wikipedia"]')
.sendKeys('//XCUIElementTypeSearchField[@name="Search Wikipedia"]','browserstack')
.click('//XCUIElementTypeStaticText[@name="BrowserStack"]')
.waitUntil(async function() {
// wait for webview context to be available
const contexts = await this.appium.getContexts();
return contexts.length > 1;
},5000)
.perform(async function() {
// switch to webview context
const contexts = await this.appium.getContexts();
await this.appium.setContext(contexts[1]);
})
.useCss()
.assert.textEquals('.pcs-edit-section-title','BrowserStack'); // command run in webview context
});
});
恭喜您,您的 iOS 设置已完成
验证设置
安装完成后,使用以下命令验证设置,以使用以下命令在 Android 模拟器上运行示例测试
npx nightwatch nightwatch/examples/mobile-app-tests/wikipedia-android.js --env app.android.emulator
或在 iOS 模拟器上运行
npx nightwatch./nightwatch/examples/mobile-app-tests/wikipedia-ios.js --env app.ios.simulator
安装 Appium Inspector
Appium 检查器将非常有助于识别选择器和调试测试。要安装 Appium 检查器,请访问 Appium Inspector 版本 并下载最新版本。安装后,只需打开 Appium 检查器,您就可以开始了。以下是该应用程序在 Mac 上的外观。
推荐的下一步
既然您了解了移动应用测试在 Nightwatch 中的工作原理,让我们深入研究设置。我们建议您涵盖以下列出的所有主题,以便全面了解使用 Nightwatch 进行移动应用自动化测试。