Here are some common testing method examples to help you better use the Automation SDK to complete testing tasks.
Error Handling
Method calls in the SDK may throw errors. For example, miniProgram.navigateTo('path/to/page') will throw an error if the page does not exist.
You can handle potential errors by using try catch statements around the relevant code, as shown below:
automator.launch().then(async miniProgram => {
let page
try {
page = await miniProgram.navigateTo('/page/component/index')
} catch (e) {
// Handle error cases
console.log(e.message)
}
})
Template Testing
Templates in Mini Program are written using the WXML markup language. During execution, Mini Program combines the template with the page data to render the final page. The testing framework Jest provides a powerful snapshot feature. By leveraging this feature along with the SDK's interface for setting page data, you can easily test whether the rendering result of a template file is correct under different data conditions.
Below is a code example of testing the interface page template in the Mini Program sample:
const automator = require('miniprogram-automator')
describe('api', () => {
let miniProgram
let page
beforeAll(async () => {
miniProgram = await automator.launch({
projectPath: 'path/to/miniprogram-demo'
})
page = await miniProgram.reLaunch('/page/API/index')
await page.waitFor(500)
})
afterAll(async () => {
await miniProgram.close()
})
it('wxml', async () => {
const element = await page.$('page')
expect(await element.wxml()).toMatchSnapshot()
await page.setData({
list: []
})
expect(await element.wxml()).toMatchSnapshot()
})
})
Test Environment
During testing, you might occasionally need to execute special logic, such as using a test account or different request URLs. In such cases, we recommend inserting this code into your Mini Program and setting a switch on the global object globalData. You can then enable it during testing using the code snippet execution feature provided by the SDK.
Below is an example of the Mini Program code for the API login interface page template:
const automator = require('miniprogram-automator')
describe('api', () => {
let miniProgram
let page
beforeAll(async () => {
miniProgram = await automator.launch({
projectPath: 'path/to/miniprogram-demo'
})
// Directly modify the global variable
await miniProgram.evaluate(() => {
getApp().globalData.hasLogin = true
})
page = await miniProgram.reLaunch('/page/API/pages/login/login')
await page.waitFor(500)
})
afterAll(async () => {
await miniProgram.close()
})
it('wxml', async () => {
const element = await page.$('page')
expect(await element.wxml()).toMatchSnapshot()
})
})
Mocking Request Results
In the test environment, you may occasionally need to mock the results of wx method calls to simulate various scenarios, such as mocking location data or request responses. In such cases, you can use miniProgram.mockWxMethod.
Below is a code example demonstrating how to mock specific request results:
const automator = require('miniprogram-automator')
describe('api', () => {
let miniProgram
beforeAll(async () => {
miniProgram = await automator.launch({
projectPath: 'path/to/miniprogram-demo'
})
})
afterAll(async () => {
await miniProgram.close()
})
it('request', async () => {
const mockData = [
{
rule: 'testRequest',
result: {
data: 'test',
cookies: [],
header: {},
statusCode: 200,
},
},
]
await miniProgram.mockWxMethod(
'request',
function(obj, data) {
for (let i = 0, len = data.length; i < len; i++) {
const item = data[i]
const rule = new RegExp(item.rule)
if (rule.test(obj.url)) {
return item.result
}
// Real backend request if the rule does not match
return new Promise(resolve => {
obj.success = res => resolve(res)
obj.fail = res => resolve(res)
this.origin(obj)
})
}
},
mockData,
)
const result = await miniProgram.callWxMethod('request', {
url: 'https://14592619.qcloud.la/testRequest',
})
console.log(result.data) // -> 'test'
await miniProgram.restoreWxMethod('request')
})
})