From 4011b2d2d638543d0095a2599fa88d6a43e758aa Mon Sep 17 00:00:00 2001 From: fanniehuang Date: Thu, 22 Apr 2021 20:43:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(e2e):=20switchTab=E5=85=BC=E5=AE=B9windows?= =?UTF-8?q?=EF=BC=9B=E5=88=A9=E7=94=A8peerDependencies=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E5=AE=89=E8=A3=85=E6=AD=A5=E9=AA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit re #80 --- packages/wxa-cli/README.md | 24 +++++++++---------- packages/wxa-cli/package.json | 7 +++++- .../src/tester/imageSimilarity/init.py | 2 +- .../src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs | 17 +++++++++---- .../src/tester/wxa-e2eTest/e2eTestSuite.js | 2 +- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/packages/wxa-cli/README.md b/packages/wxa-cli/README.md index 2791966b..430bcaf5 100644 --- a/packages/wxa-cli/README.md +++ b/packages/wxa-cli/README.md @@ -22,12 +22,11 @@ # 使用手册 ### 安装 -* 任意目录执行命令 `npm i -g wxa-cli2-apple` -* 先安装项目依赖 `npm i` -* 项目目录下执行命令 `npm i -DE miniprogram-automator looks-same jest` +* 先安装项目依赖 `wnpm i` +* 项目目录下执行命令 `wnpm i @webank/wxa-cli2-apple` -### python 及依赖安装 -* [Python官网](https://www.python.org/)网下载Python安装包(python-3.X.X.exe) +### python 及依赖安装(可选,带参数--py-diff时需要安装) +* [Python官网](https://www.python.org/)下载Python安装包(python-3.X.X.exe) * 全局安装PIL `pip install pillow` ### 测试脚本录制 @@ -86,22 +85,19 @@ ``` -3. 项目目录下执行`wxa2-apple test --e2e -r`,开启录制模式 +3. 项目目录下执行`npx wxa2-apple test --e2e -r`,开启录制模式 * 用开发者工具打开项目,页面左上角有`结束录制`button,说明已成功开启录制模式 * 此时与小程序的每一步交互都会录制为脚本,完成操作后,点击`结束录制`,输入用例名,对应脚本保存在`__wxa_e2e_test__`目录下 ### 进行基准截屏 脚本录制过程中不会截屏,需要跑一次case脚本,完成基准截屏。若无基准截屏,用例回归时就不知道测试结果是否正确,所以这一步骤是必须的 -* 脚本录制完毕后,需执行`wxa2-apple test --e2e --base ` 回放用例,检查录制操作是否正确,且此次回放的截屏会作为后续回放用例的比较基准,用于判断测试是否通过 +* 脚本录制完毕后,需执行`npx wxa2-apple test --e2e --base ` 回放用例,检查录制操作是否正确,且此次回放的截屏会作为后续回放用例的比较基准,用于判断测试是否通过 * 基准截屏存放在`__wxa_e2e_test__/用例名/base_screenshot`中(`--test=test1,test2`可指定要回放的用例,多个用例逗号分隔) ### 测试脚本回放 -* `wxa2-apple test --e2e` 进入测试用例回放模式,操作截屏以时间命名保存在测试用例目录中(`--test=test1,test2`指定执行用例,多个用例逗号分隔) - -### 测试结果校准 - -* 进入项目根目录,执行`wxa2-apple test-diff` +* `npx wxa2-apple test --e2e` 进入测试用例回放模式,操作截屏以时间命名保存在测试用例目录中(`--test=test1,test2`指定执行用例,多个用例逗号分隔) +* `npx wxa2-apple test --e2e --py-diff` 结果截屏进行相似度比对,不需要每个像素点一摸一样,需安装python3 ### 二次开发录制好的测试用例 通过修改`测试用例/record.js`,可以进行用例二次开发 @@ -117,6 +113,10 @@ record.js是一个数组,每一项Object对应用户一次操作(点击、 * `--no-mock` `wxa2-apple test --e2e --no-mock`此次用例回归,不mock api,直连真实接口(默认会用录制时的api数据来mock) * `--screenshot-diff` `wxa2-apple test --e2e --screenshot-diff=fasle` 此次用例回归,截屏是否要和base_screenshot目录下的首次截屏进行比对(不传值默认会比对,false不比对) * `--custom-expect` 进行自定义期望匹配,需要record.js里每一步的customExpect函数编写期望代码 +* `--py-diff` 结果截屏进行相似度比对,不需要每个像素点一摸一样,需安装python3 + + + ``` module.exports = [ diff --git a/packages/wxa-cli/package.json b/packages/wxa-cli/package.json index 5206eaa1..64345f28 100644 --- a/packages/wxa-cli/package.json +++ b/packages/wxa-cli/package.json @@ -1,6 +1,6 @@ { "name": "@webank/wxa-cli2-apple", - "version": "1.0.51", + "version": "1.0.57", "description": "cli for wxa development", "main": "dist/wxa.js", "scripts": { @@ -20,6 +20,11 @@ }, "repository": "https://github.com/wxajs/wxa.git", "homepage": "https://wxajs.github.io/wxa/", + "peerDependencies": { + "miniprogram-automator": "^0.10.0", + "looks-same": "^7.3.0", + "jest": "^26.6.3" + }, "devDependencies": { "@babel/cli": "^7.0.0", "@babel/core": "^7.0.0", diff --git a/packages/wxa-cli/src/tester/imageSimilarity/init.py b/packages/wxa-cli/src/tester/imageSimilarity/init.py index 225549f3..363f1af7 100644 --- a/packages/wxa-cli/src/tester/imageSimilarity/init.py +++ b/packages/wxa-cli/src/tester/imageSimilarity/init.py @@ -86,7 +86,7 @@ def GetDirList(self, cur_path, cur_type): 生成结果 ''' def WriteFile(self, out_dir): - json_path = target_path + "\\.replay_result\\" + out_dir + "\\diff_result.json" + json_path = os.path.join(target_path, ".replay_result", out_dir, "diff_result.json") if os.path.exists(json_path): os.remove(json_path) with open(json_path, 'xt', encoding='utf-8') as f: diff --git a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs index 29732928..ef0d13c0 100644 --- a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs +++ b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs @@ -118,10 +118,14 @@ for (let j = 0; j < testCaseNameArr.length; j++) { break; } else if (page.path === (recordAction.page || recordAction.appStartPage).split('?')[0]) { if (!lastRecordAction) break; - let lastPagePath = (lastRecordAction.page || lastRecordAction.appStartPage).split('?')[0] - if (page.path !== lastPagePath) { - // 和上一步的页面path不一致,说明页面切换了,需要等久一点让页面ready - await sleep(6000); + try { + let lastPagePath = (lastRecordAction.page || lastRecordAction.appStartPage || lastRecordAction.path).split('?')[0] + if (page.path !== lastPagePath) { + // 和上一步的页面path不一致,说明页面切换了,需要等久一点让页面ready + await sleep(6000); + } + } catch(err) { + await sleep(2000); } break; } @@ -159,7 +163,10 @@ for (let j = 0; j < testCaseNameArr.length; j++) { } else if (!!~['input'].indexOf(recordAction.event)) { await element[recordAction.event](recordAction.detail.value); } else if (!!~['switchTab'].indexOf(recordAction.event)) { - await miniProgram.switchTab(path.resolve('/', recordAction.path)) + if (recordAction.path[0] !== '/') { + recordAction.path = '/' + recordAction.path; + } + await miniProgram.switchTab(recordAction.path) } else if (!!~['touchstart', 'touchmove', 'touchend'].indexOf(recordAction.event)) { await element[recordAction.event]({ touches: recordAction.touches, diff --git a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js index dce90051..8cc3644c 100644 --- a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js +++ b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js @@ -153,7 +153,7 @@ const hjSwitchTab = () => { if (e.openType === 'switchTab') { let action = { event: 'switchTab', - path: e.path, + path: '/' + e.path, query: e.query }; if (state.record.length === 0) {