@小程序自动化

微信小程序自动化


微信小程序实现了本身自动化,使用自动化,可以帮助更好的处理咱们的产品,更好的保证质量。咱们从头开始学起,从api开始,会包含jest测试。web

自动化与测试

自动化通常都是相辅相成的,移动化固然只能信赖测试部分,测试可以成功,咱们自动化才有意义。小程序

  • 单元测试

单元测试的意义,保证咱们的每个单元保证本身是正常运转的。咱们进行单元测试就对每个零件进行测试。后面能够更好的实现自动化。一般会有测试块,也被称为测试套件(describe), 固然还有最小的测试单元(it or test), 在 jest 总 it 和 test 是同样的,只不过的别名而已,在测试 Matca 的 chai 断言库里面,it 称为最小的测试用例单元。一个测试用例就是一个函数,一个测试套件也是一个函数。文件名通常改成 .test.js (表示测试)和 .spec.js (表示规格)文件。微信小程序

小程序的cli的路径

Mac:/Applications/wechatwebdevtools.app/Contents/MacOS/cli
Win:C:/Program Files (x86)/Tencent/微信web开发者工具/cli.bat
复制代码

使用 Node.js 进行测试,miniprogram-automator 是小程序的自动化的 sdk, 通常配合 jest 进行测试。api

  • miniprogram-automator 可以完成小程序模拟操做
  • jest 完成一些断言相关的工做。

自动化链接咱们小程序

依赖模块: miniprogram-automatorbash

<view class="test" bind:tap="testTap">hahah</view>
<youngui-button bind:getuserinfo="handleUserInfo"></youngui-button>
复制代码
const automator = require('miniprogram-automator')

automator.launch({
  cliPath: 'path/to/cli', // 工具 cli 位置,若是你没有更改过默认安装位置,能够忽略此项
  projectPath: 'path/to/project', // 项目文件地址
}).then(async miniProgram => {
  const page = await miniProgram.reLaunch('/page/home/index')
  await page.waitFor(500)
  const element = await page.$('.kind-list-item-hd')
  console.log(await element.attribute('.test'))
  await element.tap()

  await miniProgram.close()
}).catch((error) => {
    console.error(error)
})
复制代码

登陆咱们的小程序: launch 方法微信

  • 登陆以后,进入home主页,
  • 模拟等待500ms
  • 获取 .test 的元素属性,相似于 jQuery 的选择器
  • 点击元素
  • 关闭元素
  • 注意promsie须要使用catch来捕获作错误

测试的模拟

  1. 找到元素,模拟点击事件

测试: Jest 中的复用法则

  1. 在全部测试项目中复用
  2. 在 describe 中复用
  • beforeAll
  • afterAll
  • beforeEach
  • beforeAfter
beforeAll(() => console.log('1 - beforeAll'));
afterAll(() => console.log('1 - afterAll'));
beforeEach(() => console.log('1 - beforeEach'));
afterEach(() => console.log('1 - afterEach'));
test('', () => console.log('1 - test'));
describe('Scoped / Nested block', () => {
  beforeAll(() => console.log('2 - beforeAll'));
  afterAll(() => console.log('2 - afterAll'));
  beforeEach(() => console.log('2 - beforeEach'));
  afterEach(() => console.log('2 - afterEach'));
  test('', () => console.log('2 - test'));
});

// 1 - beforeAll

// 1 - beforeEach
// 1 - test
// 1 - afterEach

// 2 - beforeAll

// 1 - beforeEach //特别注意,1 - beforeEach 会在test it 里面都会调用,且优先于里面的 beforeEach

// 2 - beforeEach
// 2 - test
// 2 - afterEach

// 1 - afterEach //特别注意,1 - afterEach 会在test it 里面都会调用,且优先于里面的 afterEach

// 2 - afterAll

// 1 - afterAll
复制代码

beforeAll, afterAll 都只会指定都是只执行一次的函数app

总结: 这其实就是做用域的问题,这些钩子函数在不一样的做用域的中执行状况不同!async

测试类型

  1. 测试 wxml 的属性、text 是否是符合咱们的预期
  2. 测试 wxml 渲染的结构的数量和内容是否是符合咱们的预期
  3. 测试 wxml 的class 属性是否是符合咱们的预期
  4. 测试 页面跳转先后的的行为是否是不是符合咱们的预期

jest 中几个经常使用重要的 api

  • test(name, fn, timout) 他的别名就是 it 函数使用方式是同样的
  • describe 是将测试分红一个个的块,咱们要在块里面的运行一些东西。
Sort api 名字 参数 做用
1 test 测试函数 name, fn, timeout 测试函数
2 it 测试函数 name, fn, timeout test的别名
2 describe 分做用域函数 name, fn 将测试分红不一样的做用域
2 beforeAll 全局前置钩子函数 name, timeout 前置钩子
2 beforeEach 前置钩子函数 name, timeout 前置钩子
2 afterAll 全局后置钩子函数 name, timeout 前置钩子
2 afterEach 后置钩子函数 name, timeout 前置钩子

jest 测试文件后缀名

.test.js.spec.js 有什么区别函数

jest 在 vscode 里面能够添加插件,添加了watch 以后每次更改 jest 的测试文件自动从新运行 jest 文件的内容,jest 的检查到依赖的文件变化的时候,依然是在运行的阶段,这样的好是咱们能很好提升咱们的代码质量,咱们不该在控制台中不断地调试 console 和 debugger,咱们应该学会写测试用例,这才是进阶了一小步。没有工做,在家里提升本身,没哟经济来源,其余的工做不想作,人生啊-_-,工具

问题

咱们接触的都是在页面写死的内容,咱们在页面中的请求到了数据以后,如何测试,页面的内容?? 咱们应该怎么写??

思路:先发请求,而后测试请求数据

咱们如何测翻页功能??

在 miniProgram对象上,提供了 miniProgram.pageScrollTo 的行为

小程序自动化的提供的api 整理

分为四大类: AutoMator、 MiniProgram、 Page、 Element


Sort AutoMater MiniProgram Page Element
1 connect pageStack path tagName
2 launch navigateTo query $
3 - redirectTo $ $$
4 - reLaunch $$ size
5 - switchTab waitFor offset
6 - systemInfo data text
7 - callWxMethod setData attribute
8 - mockWxMethod size wxml
9 - restoreWxMethod scrollTop outerWxml
10 - evaluate callMethod value
11 - pageScrollTo style
12 - exposeFunction tap
13 - remote longpress
14 - disconnect touchstart
15 - close touchmove
16 - on-console touchend
17 - on-execption callMethod
18 - - - data
19 - - - setData
20 - - - trigger
相关文章
相关标签/搜索