# MiniProgram
The MiniProgram module provides a way to control Weixin Mini Program.
# method
# miniProgram.pageStack
Gets the Weixin Mini Program page stack.
miniProgram.pageStack(): Promise<Page[]>
Example code:
automator.launch().then(async miniProgram => {
const pageStack = await miniProgram.pageStack()
console.log(pageStack.length) // 当前页面栈数量
})
# miniProgram.navigateTo
Keep the current page, jump to a page within the app, same aswx.navigateTo.
miniProgram.navigateTo(url: string): Promise<Page>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| url | string | yes | - | The path to non-tabBar pages in the app that need to be redirected |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.navigateTo('/page/component/index')
console.log(page.path) // -> 'page/component/index'
})
# miniProgram.redirectTo
Close the current page and jump to a page within the app, likewx.redirectTo.
miniProgram.redirectTo(url: string): Promise<Page>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| url | string | yes | - | The path to non-tabBar pages in the app that need to be redirected |
# miniProgram.navigateBack
Close the current page and return to the previous page or multiple levels, same aswx.navigateBack.
miniProgram.navigateBack(): Promise<Page>
# miniProgram.reLaunch
Close all pages, open to a page within the app, same aswx.reLaunch.
miniProgram.reLaunch(url: string): Promise<Page>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| url | string | yes | - | In-app page paths that need to be jumped |
# miniProgram.switchTab
Jump to a tabBar page and close all other non-tabBar pages, same aswx.switchTab.
miniProgram.switchTab(url: string): Promise<Page>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| url | string | yes | - | The path to the tabBar page you need to jump to |
# miniProgram.currentPage
Get the current page.
miniProgram.currentPage(): Promise<Page>
# miniProgram.systemInfo
Get system information withwx.getSystemInfo.
miniProgram.systemInfo(): Promise<Object>
Example code:
automator.launch().then(async miniProgram => {
const systemInfo = await miniProgram.systemInfo()
if (systemInfo.platform === 'devtools') {
// Do something
}
})
# miniProgram.callWxMethod
Calls the specified method on the wx object.
miniProgram.callWxMethod(method: string, ...args: any[]): Promise<any>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| method | string | yes | - | Method name that needs to be called |
| ...args | array|no | - | Method parameters |
An asynchronous method is called without passing in the success and fail callbacks.
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.callWxMethod('setStorage', {
key: 'test',
data: 'test'
})
const { data } = await miniProgram.callWxMethod('getStorageSync', 'test')
console.log(data) // -> 'test'
})
# miniProgram.callPluginWxMethod
Base Library 2.19.3 Start support.
Calls the specified method on the plug-in wx object, as miniProgram.callWxMethod.
miniProgram.callWxMethod(pluginId: string, method: string, ...args: any[]): Promise<any>
# miniProgram.mockWxMethod
The incoming function function automator 0.9.0, the base library 2.9.5 support.
Overrides the result of a call to a specified method on a wx object.
Using this interface, you can easily directly specify the return result of calling system components such aswx.chooseLocation.
miniProgram.mockWxMethod(method: string, result: any): Promise<void>
miniProgram.mockWxMethod(method: string, fn: Function | string, ...args: any[]): Promise<void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| method | string | yes | - | Method names that need to be covered |
| result | any | yes | - | Specify the result of the call |
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| method | string | yes | - | Method names that need to be covered |
| fn | Function string | yes | - | Handling return functions |
| ...args | array|no | - | Input parameters |
As with the appFunction parameter of miniProgram.evaluate, you cannot use closures to reference external variables. In addition, you can use this.origin inside a method to call the original method.
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.mockWxMethod('showModal', {
confirm: true,
cancel: false
})
await miniProgram.mockWxMethod(
'getStorageSync',
function(key, defVal) {
if (key === 'name') return 'redhoodsu'
if (key === 'sex') return 'male'
return defVal
},
'unknown',
)
// 调用 wx.getStorageSync('name') 返回 'redhoodsu'
// 更改 getSystemInfo 中的 platform 字段
await miniProgram.mockWxMethod(
'getSystemInfo',
function(obj, platform) {
return new Promise(resolve => {
// origin 指向原始方法
this.origin({
success(res) {
res.platform = platform
resolve(res)
},
})
})
},
'test',
)
})
# miniProgram.mockPluginWxMethod
Base Library 2.19.3 Start support.
Overrides the result of the invocation of the specified method on the plug-in wx object, as miniProgram.mockWxMethod.
miniProgram.mockWxMethod(pluginId: string, method: string, result: any): Promise<void>
miniProgram.mockWxMethod(pluginId: string, method: string, fn: Function | string, ...args: any[]): Promise<void>
# miniProgram.restoreWxMethod
Resets the wx specified method to eliminate the effect of the mockWxMethod call.
miniProgram.restoreWxMethod(method: string): Promise<void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| method | string | yes | - | Method names that need to be covered |
Example code:
automator.launch().then(async miniProgram => {
console.log(await miniProgram.callWxMethod('getStorageSync', 'test')) // -> ''
await miniProgram.mockWxMethod('getStorageSync', 'mockValue')
console.log(await miniProgram.callWxMethod('getStorageSync', 'test')) // -> 'mockValue'
await miniProgram.restoreWxMethod('getStorageSync')
console.log(await miniProgram.callWxMethod('getStorageSync', 'test')) // -> ''
})
# miniProgram.restorePluginWxMethod
Base Library 2.19.3 Start support.
Reset the plug-in wx specified method to eliminate the effect of the mockPluginWxMethod call, using the same miniProgram.restoreWxMethod.
miniProgram.restoreWxMethod(pluginId: string, method: string): Promise<void>
# miniProgram.evaluate
Injects a snippet of code into AppService and returns the execution result.
miniProgram.evaluate(appFunction: Function | string, ...args: any[]): Promise<any>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| appFunction | Function string | yes | - | Code snippet |
| ...args | array|no | - | Input parameters during execution |
The appFunction is eventually serialized and passed to the developer tools, so you can't use closures to reference external variables in functions.That is, passing
function () {}Function is essentially the same as passing its character string.
Example code:
automator.launch().then(async miniProgram => {
let systemInfo = await miniProgram.evaluate(() => {
return new Promise(resolve => {
wx.getSystemInfo({
success(result) {
resolve(result)
}
})
})
})
systemInfo = await miniProgram.evaluate(() => {
return wx.getSystemInfoSync()
})
console.log(systemInfo)
await miniProgram.evaluate(key => {
wx.setStorageSync(key, 'test')
}, 'test')
const hasLogin = await miniProgram.evaluate(() => getApp().globalData.hasLogin)
console.log(hasLogin)
})
# miniProgram.pageScrollTo
Scroll the page to the destination, same aswx.pageScrollTo.
miniProgram.pageScrollTo(scrollTop: number): Promise<void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| scrollTop | number | yes | - | Scroll to the target position of the page, in px |
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.pageScrollTo(50)
})
# miniProgram.screenshot
Automator 0.9.0, Base Library 2.9.5, Developer Tools 1.02.2001082 Supported
Screenshots of the current page are currently only supported by the developer tools emulator and cannot be used by the client.
miniProgram.screenshot(options?: Object): Promise<string | void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| options | Object | no | - | Screenshot options |
If options are not passed, this method returns the base64 encoding of the picture data.
The options field is defined as follows:
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| path | string | yes | - | Picture Save Path |
automator.launch().then(async miniProgram => {
await miniProgram.screenshot({
path: 'screenshot.png'
})
})
# miniProgram.exposeFunction
Methods are exposed globally in AppService for the Weixin Mini Program side to call methods in the test script.
miniProgram.exposeFunction(name: string, bindingFunction: Function): Promise<void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| name | string | yes | - | Global methodology name |
| bindingFunction | Function | yes | - | Scripting method |
You can use this method to listen for events. Getting the result of the call on the Weixin Mini Program side is not supported.
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.exposeFunction('onAppShow', options => {
// Do something...
})
await miniProgram.evaluate(function() {
wx.onAppShow(function(options) {
onAppShow(options)
})
})
})
# miniProgram.testAccounts
Automator 0.9.0, Developer Tools 1.02.2002272 Started Support
Get a list of users added in a multi-account debugging.
miniProgram.testAccounts(): Promise<Account[]>
The Account field is defined as follows:
| field | type | Introductions |
|---|---|---|
| nickName | string | User nickname |
| openid | string | Account name: openid |
Example code:
automator.launch().then(async miniProgram => {
const testAccounts = await miniProgram.testAccounts()
for (let i = 0, len = testAccounts.length; i < len; i++) {
const miniProgram = await automator.launch({
projectPath: 'path/to/project',
account: testAccounts[i].openid
})
// 控制多个用户登录的不同小程序
}
})
# miniProgram.stopAudits
Automator 0.10.0, Developer Tools 1.04.2006242 Started Support
Stop the experience rating and get the report.
miniProgram.stopAudits(options?: Object): Promise<Object>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| options | Object | no | - | to make a choice |
The options field is defined as follows:
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| path | string | no | - | Path to report preservation |
You need to turn on the automatic runtime experience rating option.
automator.launch({
projectConfig: {
setting: {
autoAudits: true,
},
},
}).then(async miniProgram => {
const data = await miniProgram.stopAudits({
path: 'report.html'
})
console.log(data) // 体验评分报告数据
})
# miniProgram.getTicket
Get the developer tool's current login note.
Make sure the tool security settings have enabled the ability to obtain a tool login note.
miniProgram.getTicket(): Promise<Object>
Return value explaination
| field | type | Introductions |
|---|---|---|
| ticket | string | Signing in notes |
| expiredTime | number | Time of expiry of the note |
Example code:
automator.launch().then(async miniProgram => {
const result = await miniProgram.getTicket()
console.log(result.ticket)
})
# miniProgram.setTicket
Set up a developer tool login note to update invalid login notes during a test run of the tool.
miniProgram.setTicket(ticket: string): Promise<void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| ticket | string | yes | - | Signing in notes |
Example code:
// Ticket gets from logged in developer tools
automator.launch({
ticket,
}).then(async miniProgram => {
// 如果初始票据已失效,获取新票据后通过 setTicket 更新
await miniProgram.setTicket(ticket)
})
# miniProgram.refreshTicket
Refreshing the Developer Tools login note resets the expiration time of the note to two hours.
miniProgram.refreshTicket(): Promise<void>
The original note will expire after it is renewed even if the expiration date is not yet available.
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.refreshTicket()
})
# miniProgram.remote
Turn on the tool's real-world debugging function.
miniProgram.remote(auto?: boolean): Promise<void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| auto | boolean | no | false | Is there automatic real machine debugging? |
When called, the script starts the tool native debugging function and prints the QR code on the console. You then need to use the native barcode connection to keep the automated script running.
When auto is true, the real machine will automatically turn up Weixin Mini Program, no need to scan the code, only support WeChat 7.0.6 (Android), 6.6.7 (iOS) and above.](remote.md) Click here to view the content of the real machine self-activation.
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.remote()
// 扫码连接成功后在真机上执行自动化脚本
})
# miniProgram.disconnect
Disconnect from the Weixin Mini Program runtime.
miniProgram.disconnect(): void
Example code:
automator.launch().then(async miniProgram => {
miniProgram.disconnect()
})
# miniProgram.close
Disconnect the Weixin Mini Program runtime and close the project window.
miniProgram.close(): Promise<void>
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.close()
})
# event
# console
The log is triggered when it is printed.
Pass an msg parameter with the following fields:
| field | type | Introductions |
|---|---|---|
| type | string | Log types, log, info, etc. |
| args | array|Log content |
Example code:
automator.launch().then(async miniProgram => {
miniProgram.on('console', msg => {
console.log(msg.type, msg.args)
})
})
# exception
Triggered when a page JS error occurs.
Pass an error parameter with the following fields:
| field | type | Introductions |
|---|---|---|
| message | string | Error message |
| stack | string | Error Stack |
Example code:
automator.launch().then(async miniProgram => {
miniProgram.on('exception', err => {
console.log(err.message, err.stack)
})
})