The MiniProgram module provides methods to control Mini Program.
Methods
miniProgram.pageStack
Retrieves the page stack of Mini Program.
miniProgram.pageStack(): Promise<Page[]>
Sample code:
automator.launch().then(async miniProgram => {
const pageStack = await miniProgram.pageStack()
console.log(pageStack.length) // Number of pages in the current page stack
})
miniProgram.navigateTo
Keeps the current page and navigates to a specific page within the application. Equivalent to wx.navigateTo.
miniProgram.navigateTo(url: string): Promise<Page>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| url | string | Yes | - | The path to the non-tabBar page within the application that you want to navigate to |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.navigateTo('/page/component/index')
console.log(page.path) // -> 'page/component/index'
})
miniProgram.redirectTo
Closes the current page and redirects to another page within the Mini Program, equivalent to wx.redirectTo.
miniProgram.redirectTo(url: string): Promise<Page>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| url | string | Yes | - | The path of a non-tabBar page within the Mini Program to redirect to |
miniProgram.navigateBack
Closes the current page and navigates back to the previous page or multiple levels back, similar to wx.navigateBack.
miniProgram.navigateBack(): Promise<Page>
miniProgram.reLaunch
Closes all pages and opens a specific page within the application, equivalent to wx.reLaunch.
miniProgram.reLaunch(url: string): Promise<Page>
Parameter Description
| Field | Type | Required | Default Value | Description |
|---|---|---|---|---|
| url | string | Yes | - | The path of the page within the application to navigate to |
miniProgram.switchTab
Navigate to a tabBar page and close all other non-tabBar pages, same as wx.switchTab.
miniProgram.switchTab(url: string): Promise<Page>
Parameters
| Field | Type | Required | Default Value | Description |
|---|---|---|---|---|
| url | string | Yes | - | The path to the tabBar page to navigate to |
miniProgram.currentPage
Get the current page.
miniProgram.currentPage(): Promise<Page>
miniProgram.systemInfo
Retrieves system information, equivalent to wx.getSystemInfo.
miniProgram.systemInfo(): Promise<Object>
Sample code:
automator.launch().then(async miniProgram => {
const systemInfo = await miniProgram.systemInfo()
if (systemInfo.platform === 'devtools') {
// Do something
}
})
miniProgram.callWxMethod
Calls a specified method on the wx object.
miniProgram.callWxMethod(method: string, ...args: any[]): Promise<any>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| method | string | Yes | - | The name of the method to call |
| ...args | array<any> | No | - | The arguments for the method |
When calling asynchronous methods, there is no need to pass success and fail callback functions.
Sample 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
Supported since Base Library 2.19.3.
Calls the specified method on the plugin's wx object. The usage is the same as miniProgram.callWxMethod.
miniProgram.callWxMethod(pluginId: string, method: string, ...args: any[]): Promise<any>
miniProgram.mockWxMethod
Passing function features are supported from automator 0.9.0 and base library 2.9.5 onwards.
Overrides the calling result of a specified method on the wx object.
Using this interface, you can conveniently specify the return result of methods that call system components, such as wx.chooseLocation.
miniProgram.mockWxMethod(method: string, result: any): Promise<void>
miniProgram.mockWxMethod(method: string, fn: Function | string, ...args: any[]): Promise<void>
Parameter Description
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| method | string | Yes | - | The name of the method to be overridden |
| result | any | Yes | - | The specified calling result |
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| method | string | Yes | - | The name of the method to be overridden |
| fn | Function string | Yes | - | The function to handle the return value |
| ...args | array<any> | No | - | Input arguments |
The
fnparameter is similar to theappFunctionparameter inminiProgram.evaluateand cannot use closures to reference external variables. Additionally, you can usethis.originwithin the method to call the original method.
Code Example:
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',
)
// Calling wx.getStorageSync('name') returns 'redhoodsu'
// Change the platform field in getSystemInfo
await miniProgram.mockWxMethod(
'getSystemInfo',
function(obj, platform) {
return new Promise(resolve => {
// origin points to the original method
this.origin({
success(res) {
res.platform = platform
resolve(res)
},
})
})
},
'test',
)
})
miniProgram.mockPluginWxMethod
Supported in Base Library 2.19.3 and later.
Overwrites the invocation result of a specified method on the plugin's wx object. Usage is identical to 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 a specified wx method to eliminate the effects of a mockWxMethod call.
miniProgram.restoreWxMethod(method: string): Promise<void>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| method | string | Yes | - | The name of the method to be reset |
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
Supported from Base Library 2.19.3.
Resets the specified wx method of a plugin, eliminating the effects of mockPluginWxMethod calls. The usage is the same as miniProgram.restoreWxMethod.
miniProgram.restoreWxMethod(pluginId: string, method: string): Promise<void>
miniProgram.evaluate
Injects a code snippet into the AppService and returns the execution result.
miniProgram.evaluate(appFunction: Function | string, ...args: any[]): Promise<any>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| appFunction | Function string | Yes | - | Code snippet |
| ...args | array<any> | No | - | Parameters passed during execution |
Since
appFunctionis ultimately serialized and passed to the Developer Tools, you cannot use closures within the function to reference external variables. In other words, passing afunction () {}is effectively equivalent to passing its string representation.
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
Scrolls the page to the target position, similar to wx.pageScrollTo.
miniProgram.pageScrollTo(scrollTop: number): Promise<void>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| scrollTop | number | Yes | - | Target position to scroll the page to, in px |
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.pageScrollTo(50)
})
miniProgram.screenshot
Supported since automator 0.9.0, Base Library 2.9.5, and DevTools 1.02.2001082.
Takes a screenshot of the current page. Currently supported only by the DevTools simulator; it is not available on client devices.
miniProgram.screenshot(options?: Object): Promise<string | void>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| options | Object | No | - | Screenshot options |
If options is not provided, the method returns the base64 encoding of the image data.
The options fields are defined as follows:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| path | string | Yes | - | Image save path |
automator.launch().then(async miniProgram => {
await miniProgram.screenshot({
path: 'screenshot.png'
})
})
miniProgram.exposeFunction
Exposes a method globally in the AppService for Mini Program to call the method in the test script.
miniProgram.exposeFunction(name: string, bindingFunction: Function): Promise<void>
Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
| name | string | Yes | - | Global method name |
| bindingFunction | Function | Yes | - | Script method |
You can use this method to listen for events. Retrieving the call result on the 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
Supported since automator 0.9.0 and Developer Tools 1.02.2002272
Retrieves the list of users added in multi-account debugging.
miniProgram.testAccounts(): Promise<Account[]>
The Account field definitions are as follows:
| Field | Type | Description |
|---|---|---|
| nickName | string | User nickname |
| openid | string | Account openid |
Code example:
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
})
// Control different Mini Program for multiple logged-in users
}
})
miniProgram.stopAudits
Supported from automator 0.10.0 and Developer Tools 1.04.2006242 onwards
Stops the experience audit and retrieves the report.
miniProgram.stopAudits(options?: Object): Promise<Object>
Parameters
| Field | Type | Required | Default Value | Description |
|---|---|---|---|---|
| options | Object | No | - | Options |
The options field is defined as follows:
| Field | Type | Required | Default Value | Description |
|---|---|---|---|---|
| path | string | No | - | Path to save the report |
The automatic experience audit option must be enabled.
automator.launch({
projectConfig: {
setting: {
autoAudits: true,
},
},
}).then(async miniProgram => {
const data = await miniProgram.stopAudits({
path: 'report.html'
})
console.log(data) // Experience audit report data
})
miniProgram.remote
Enables the tool's real device debugging feature.
miniProgram.remote(auto?: boolean): Promise<void>
Parameter Description
| Field | Type | Required | Default Value | Description |
|---|---|---|---|---|
| auto | boolean | No | false | Whether to enable automatic real device debugging |
After the call, the script starts the tool's real device debugging feature and prints a QR code in the console. You then need to scan the code with a real device to establish a connection so that the automation script can continue execution.
If
autois set totrue, Mini Program is automatically launched on the real device without requiring a scan. This feature is only supported on WeChat version 7.0.6 (Android), 6.6.7 (iOS), or later. For more information about real device automation, please click here.
Sample Code:
automator.launch().then(async miniProgram => {
await miniProgram.remote()
// After scanning and connecting successfully, execute the automation script on the real device
})
miniProgram.disconnect
Disconnect from the Mini Program runtime.
miniProgram.disconnect(): void
Example:
automator.launch().then(async miniProgram => {
miniProgram.disconnect()
})
miniProgram.close
Disconnects from the Mini Program runtime and closes the project window.
miniProgram.close(): Promise<void>
Example code:
automator.launch().then(async miniProgram => {
await miniProgram.close()
})
Events
console
Triggered when logging.
Passes a msg parameter with the following fields:
| Field | Type | Description |
|---|---|---|
| type | string | Log type, such as log, info, etc. |
| args | array<any> | Log content |
Example code:
automator.launch().then(async miniProgram => {
miniProgram.on('console', msg => {
console.log(msg.type, msg.args)
})
})
exception
Triggered when a JavaScript error occurs on the page.
An error parameter is passed with the following fields:
| Field | Type | Description |
|---|---|---|
| message | string | Error message |
| stack | string | Error stack trace |
Example code:
automator.launch().then(async miniProgram => {
miniProgram.on('exception', err => {
console.log(err.message, err.stack)
})
})