1. 程式人生 > 其它 >小程式自動化測試Demo程式碼

小程式自動化測試Demo程式碼

有挺多很費解的地方,看文件只是個片段,這裡記錄一下

const automator = require('miniprogram-automator')

describe('index', () => {
  let miniProgram
  let page

  beforeAll(async () => {
    miniProgram = await automator.launch({
      cliPath: 'D:\\Program Files\\Tencent\\微信web開發者工具\\cli.bat',
      projectPath: 'D:\\testfile\\miniprogram-demo-master',
    })
    page = await miniProgram.reLaunch('/page/component/index')
    await page.waitFor(3000)
  }, 30000)

  it('desc', async () => {
    const desc = await page.$('.index-desc')
    expect(desc.tagName).toBe('view')
    expect(await desc.text()).toContain('以下將展示小程式官方元件能力')
  })

  it('list', async () => {
    const lists = await page.$$('.kind-list-item')
    expect(lists.length).toBe(9)
    const list = await lists[0].$('.kind-list-item-hd')
    expect(await list.text()).toBe('檢視容器')
  })

  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')
  })
  afterAll(async () => {
    await miniProgram.close()
  })
})

  然後執行命令:jest index.spec.js

  執行結果: