# Common Examples
Here are some examples of common testing methods to help you better use the custom SDK for testing tasks.
# Error handling
SDK method calls may throw errors, such asminiProgram.navigateTo ('path / to / page')error when the page does not exist.
You can usetry catchon potentially error-prone code, like this:
automator.launch().then(async miniProgram => {
let page
try {
page = await miniProgram.navigateTo('/page/component/index')
} catch (e) {
// 处理出错情况
console.log(e.message)
}
})
# Template Testing
The templates for Weixin Mini Program are written in the WXML markup language.When executed, the Mini Program combines the template and page data to render the final page.The test framework Jest provides a great snapshot feature.Using this feature and the Setup Page data interface provided by the SDK, you can easily test whether the template files render correctly with different data.
Here is a code example of the Weixin Mini Program sample test 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'
})
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()
})
})
# Testing environment
During testing, sometimes you may need to perform some special logic, such as using a test account or using another request address.In this case, we recommend embedding this part of the Weixin Mini Program code and setting a switch on the global object globalData to be turned on during testing through the SDK's ability to execute code fragmentation.
Here is a code example of the Weixin Mini Program sample testing the 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'
})
// 直接更改全局变量
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()
})
})
# Forged request results
In a test environment, sometimes the results of the wx method call need to be falsified in order to simulate various situations, such as falsifying geographical locations, falsifying request results, etc. In this case, you can useminiProgram.mockWxMethod。
Here is an example of code that falsifies the results of a specific request:
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
}
// 没命中规则的真实访问后台
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')
})
})