# Example script
Weixin Mini Program The custom SDK itself does not provide a testing framework. This means that you can use it in conjunction with any of the popular Node.js testing frameworks on the market to achieve the purpose of writing Mini Programs test cases.The next step is to write an actual Mini Program autotest using the Jest testing framework.
# Test objects
Using the Weixin Mini Program example as a test object, download the source code for the Mini Program example locally from GitHub and open the Mini Program tool to import the project.
# to initialize
Create a new document folderminiprogram-demo-testto place the test code and install the dependency by executing the following command:
npm i miniprogram-automator jest
npm i jest -g
Follow the usage instructions in to quickly start with to install the required version of the developer tools and turn on the security settings CLI / HTTP call functions to start writing the script.
# Script Writing
Now we are ready to write a test case for the front page of the Weixin Mini Program example, as shown in the following figure:

After creating the test fileindex.spec.js, the first thing to do is:
- Launch and connect the tool
- Reboot Weixin Mini Program to the home page
- Disconnect and close the tool
The corresponding script is as follows:
const automator = require('miniprogram-automator')
describe('index', () => {
let miniProgram
let page
beforeAll(async () => {
miniProgram = await automator.launch({
projectPath: 'path/to/miniprogram-demo'
})
page = await miniProgram.reLaunch('/page/component/index')
await page.waitFor(500)
}, 30000)
afterAll(async () => {
await miniProgram.close()
})
})
It takes some time to start the initial compilation of the tool project window. Jest default 5 seconds timeout is too short and needs to be modified.
# 1. Description at top of the test
- Get the target element through the.index-desc selector
- The target element should be a view component
- The target element should contain the text "The following will demonstrate Weixin Mini Program official component capabilities"
The corresponding script is as follows:
it('desc', async () => {
const desc = await page.$('.index-desc')
expect(desc.tagName).toBe('view')
expect(await desc.text()).toContain('以下将展示小程序官方组件能力')
})
# 2. Test List Items
- Get a collection of list elements
- The number of elements in the target set should be 8
- The title of the first list element should be "View Windower"
The corresponding script is as follows:
it('list', async () => {
const lists = await page.$$('.kind-list-item')
expect(lists.length).toBe(8)
const list = await lists[0].$('.kind-list-item-hd')
expect(await list.text()).toBe('视图容器')
})
# 3. Test the behavior of list items
- Click on list titles should display or hide sublists
- Clicking on a sublist item should jump to the specified page
The corresponding script is as follows:
it('list action', async () => {
const listHead = await page.$('.kind-list-item-hd')
expect(await listHead.attribute('class')).toBe('kind-list-item-hd')
await listHead.tap()
await page.waitFor(200)
expect(await listHead.attribute('class')).toBe(
'kind-list-item-hd kind-list-item-hd-show',
)
await listHead.tap()
await page.waitFor(200)
expect(await listHead.attribute('class')).toBe('kind-list-item-hd')
await listHead.tap()
await page.waitFor(200)
const item = await page.$('.index-bd navigator')
await item.tap()
await page.waitFor(500)
expect((await miniProgram.currentPage()).path).toBe('page/component/pages/view/view')
})
# Script execution
After writing the script, directly execute the following script:
jest index.spec.js
If you see the console outputting the following information, the test was successful.
PASS ./index.spec.js (5.341s)
index
√ desc (18ms)
√ list (14ms)
√ list action (1274ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 6.378s
Ran all test suites matching /index.spec.js/i.