微信小程序自动化
微信小程序实现了自己自动化,使用自动化,能够帮助更好的处理我们的产品,更好的保证质量。我们从头开始学起,从api开始,会包含jest测试。
自动化与测试
自动化一般都是相辅相成的,移动化当然只能信赖测试部分,测试能够成功,我们自动化才有意义。
- 单元测试
单元测试的意义,保证我们的每一个单元保证自己是正常运转的。我们进行单元测试就对每一个零件进行测试。后面可以更好的实现自动化。通常会有测试块,也被称为测试套件(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 进行测试。
- miniprogram-automator 能够完成小程序模拟操作
- jest 完成一些断言相关的工作。
自动化连接我们小程序
依赖模块: miniprogram-automator
<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来捕获做错误
测试的模拟
- 找到元素,模拟点击事件
测试: Jest 中的复用法则
- 在所有测试项目中复用
- 在 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 都只会指定都是只执行一次的函数
总结: 这其实就是作用域的问题,这些钩子函数在不同的作用域的中执行情况不一样!
测试类型
- 测试 wxml 的属性、text 是不是符合我们的预期
- 测试 wxml 渲染的结构的数量和内容是不是符合我们的预期
- 测试 wxml 的class 属性是不是符合我们的预期
- 测试 页面跳转前后的的行为是不是不是符合我们的预期
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 |