

English
uni-app提供了一批API,这些API可以操控uni-app应用,包括运行、跳转页面、触发点击等,并可以获取页面元素状态、进行截图,从而实现对uni-app项目进行自动化测试的目的。
本功能使用到了业内常见的测试库如jest(MIT协议)。
Recommended usage: after R&D submits the source code to the version library, the continuous integration system automatically pulls the source code and automatically runs the automated test.
开发者可以利用API做以下事情:
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快应用 | 快手小程序 | 京东小程序 |
---|---|---|---|---|---|---|---|---|---|
√(ios only supports simulator) | √ | √ | x | x | x | x | x | x | x |
如果您想在终端命令行
进行自动化测试、或使用持续集成进行测试,请使用uni-app CLI 工程,CLI项目自动化测试教程
In order to facilitate everyone to perform uni-app automated testing in HBuilderX
, HBuilderX uni-app automated testing plugin was developed.
The plugin supports automated testing of uni-app common projects
and CLI projects
in HBuilderX.
插件简化了测试环境安装、测试用例创建、测试运行、测试设备选择等步骤。插件使用文档
The jest.config.js file is the test configuration file. The details are as follows:
module.exports = {
globalTeardown: '@dcloudio/uni-automator/dist/teardown.js',
testEnvironment: '@dcloudio/uni-automator/dist/environment.js',
testEnvironmentOptions: {
compile: true,
h5: { // 为了节省测试时间,可以指定一个 H5 的 url 地址,若不指定,每次运行测试,会先 npm run dev:h5
url: "http://192.168.x.x:8080/h5/",
options: {
headless: false // 配置是否显示 puppeteer 测试窗口
}
},
"app-plus": { // 需要安装 HBuilderX
android: {
appid: "", //自定义基座测试需配置manifest.json中的appid
package: "", //自定义基座测试需配置Android包名
executablePath: "HBuilderX/plugins/launcher/base/android_base.apk" // apk 目录或自定义调试基座包路径
},
ios: {
// uuid must be configured, currently only the simulator is supported, you can (xcrun simctl list) view the simulator uuid to be used
id: "",
executablePath: "HBuilderX/plugins/launcher/base/Pandora_simulator.app" // ipa 目录
}
},
"mp-weixin": {
port: 9420, // 默认 9420
account: "", // 测试账号
args: "", // 指定开发者工具参数
cwd: "", // 指定开发者工具工作目录
launch: true, // 是否主动拉起开发者工具
teardown: "disconnect", // 可选值 "disconnect"|"close" 运行测试结束后,断开开发者工具或关闭开发者工具
remote: false, // 是否真机自动化测试
executablePath: "", // 开发者工具cli路径,默认会自动查找, windows: C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat", mac: /Applications/wechatwebdevtools.app/Contents/MacOS/cli
},
"mp-baidu": {
port: 9430, // 默认 9430
args: "", // 指定开发者工具参数
cwd: "", // 指定开发者工具工作目录
launch: true, // 是否主动拉起开发者工具
teardown: "disconnect", // 可选值 "disconnect"|"close" 运行测试结束后,断开开发者工具或关闭开发者工具
remote: false, // 是否真机自动化测试
executablePath: "", // 开发者工具cli路径,默认会自动查找
}
},
testTimeout: 15000,
reporters: [
'default'
],
watchPathIgnorePatterns: ['/node_modules/', '/dist/', '/.git/'],
moduleFileExtensions: ['js', 'json'],
rootDir: __dirname,
testMatch: ['<rootDir>/src/**/*test.[jt]s?(x)'], // 测试文件目录
testPathIgnorePatterns: ['/node_modules/']
}
Precautions
reLaunch
. The solution is as follows:// ReLaunch to the home page and get the page object (where program is the global object automatically injected by uni-automator)
page = await program.reLaunch('/pages/extUI/calendar/calendar')
// If the WeChat applet is a subcontracted page, it needs to be delayed for more than 7s to ensure that the page object can be obtained correctly
await page.waitFor(7000)
page = await program.currentPage()
<uni-tag>
<view class="test"></view>
</uni-tag>
// error, can't get element
await page.$('.test')
// get the element
let tag = await page.$('uni-tag')
await tag.$('.test')
jest.config.js
fileGitHub: https://github.com/dcloudio/hello-uniapp
HBuilderX 4.13
支持 page.data
获取 defineExpose
暴露的数据