diff --git a/packages/wxa-cli/scripts/buildLib.sh b/packages/wxa-cli/scripts/buildLib.sh index fbb9c9ba..810f4834 100644 --- a/packages/wxa-cli/scripts/buildLib.sh +++ b/packages/wxa-cli/scripts/buildLib.sh @@ -5,6 +5,7 @@ rm -rf ./lib-dist/wxa-e2eTest mkdir -p ./lib-dist/wxa-e2eTest mkdir -p ./dist/tester/wxa-e2eTest ln ./src/tester/wxa-e2eTest/e2eTestSuite.js ./lib-dist/wxa-e2eTest/e2eTestSuite.js +ln ./src/tester/wxa-e2eTest/mockWxMethodConfig.js ./lib-dist/wxa-e2eTest/mockWxMethodConfig.js ln ./src/tester/wxa-e2eTest/e2eRecordBtn.wxa ./lib-dist/wxa-e2eTest/e2eRecordBtn.wxa ln ./src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs ./dist/tester/wxa-e2eTest/e2eTestCaseTpl.ejs echo $MODE; diff --git a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs index ceb002a0..9eab09e7 100644 --- a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs +++ b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs @@ -68,27 +68,70 @@ for (let j = 0; j < testCaseNameArr.length; j++) { let record = require(path.join(testDir, testName, `./record.js`)); let screenCount = 0; // mock接口 - if (!noMockApi) { - let apiMockMap = require(path.join(testDir, testName, `./api.json`)); - await miniProgram.mockWxMethod( - 'request', - function(reqObj, apiMockMap) { - let mapKey = `${reqObj.url}__e2e__${reqObj.method}__e2e__${Object.keys(reqObj.data).join(',')}`; - if (apiMockMap[mapKey] && apiMockMap[mapKey].length > 0) { - console.log(mapKey, 'mock success') - return apiMockMap[mapKey].shift(); - } - return new Promise(resolve => { - reqObj.success = res => resolve(res) - reqObj.fail = res => resolve(res) - // origin 指向原始方法 - console.log(mapKey, 'no mock') - this.origin(reqObj) - }) - }, - apiMockMap - ) - } + let apiMockMap = require(path.join(testDir, testName, `./api.json`)); + // for (let i = 0; i < mockWxMethodConfig.length; i++) { + <% mockWxMethodConfig.forEach(function(mockItem){ %> + <% if(mockItem.name !== 'request' || !noMockApi) %> + await miniProgram.mockWxMethod( + '<%- mockItem.name %>', + function(config, apiMockMap) { + let mapKey = `<%- mockItem.tpl %>` + if (apiMockMap[mapKey] && apiMockMap[mapKey].length > 0) { + console.log(mapKey, 'mock success') + return apiMockMap[mapKey].shift(); + } + return new Promise(resolve => { + config.success = res => resolve(res) + config.fail = res => resolve(res) + // origin 指向原始方法 + console.log(mapKey, 'no mock') + this.origin(config) + }) + }, + apiMockMap + ) + <% }); %> + // } + // if (!noMockApi) { + // await miniProgram.mockWxMethod( + // 'request', + // function(reqObj, apiMockMap) { + // let mapKey = `${reqObj.url}__e2e__${reqObj.method}__e2e__${Object.keys(reqObj.data).join(',')}`; + // if (apiMockMap[mapKey] && apiMockMap[mapKey].length > 0) { + // console.log(mapKey, 'mock success') + // return apiMockMap[mapKey].shift(); + // } + // return new Promise(resolve => { + // reqObj.success = res => resolve(res) + // reqObj.fail = res => resolve(res) + // // origin 指向原始方法 + // console.log(mapKey, 'no mock') + // this.origin(reqObj) + // }) + // }, + // apiMockMap + // ) + // } + // // mock modal框 + // await miniProgram.mockWxMethod( + // 'showModal', + // function(config, apiMockMap) { + // let mapKey = `showModal__e2e__title__${config.titie}__e2e__content__${config.content}__e2e__editable__${config.editable}__e2e__showCancel__${config.showCancel}__e2e__confirText__${config.confirmText}`; + // console.log(mapKey) + // if (apiMockMap[mapKey] && apiMockMap[mapKey].length > 0) { + // console.log(mapKey, 'mock success') + // return apiMockMap[mapKey].shift(); + // } + // return new Promise(resolve => { + // reqObj.success = res => resolve(res) + // reqObj.fail = res => resolve(res) + // // origin 指向原始方法 + // console.log(mapKey, 'no mock') + // this.origin(reqObj) + // }) + // }, + // apiMockMap + // ) // 开始回放+截屏 page = await miniProgram.reLaunch(`/${record[0].action.page}`); diff --git a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js index cf618c68..2a02680d 100644 --- a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js +++ b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestSuite.js @@ -1,6 +1,6 @@ /* eslint-disable no-invalid-this */ /* eslint-disable no-undef */ - +import mockWxMethodConfig from './mockWxMethodConfig'; const IDKEY = '_wxatestuniqueid'; const EVENTMAPKEY = '_wxatesteventmap'; @@ -150,35 +150,36 @@ let $$testSuitePlugin = (options) => { state.recording = true; state.recordMode = true; } - const originRequest = wx.request; - Object.defineProperty(wx, 'request', { - configurable: true, - enumerable: true, - writable: true, - value: function() { - const config = arguments[0] || {}; - let {url, data, method} = config; - let { recording, apiRecord } = state; - if (recording) { - let key = `${url}__e2e__${method}__e2e__${Object.keys(data).join(',')}` - if (!apiRecord.has(key)) { - apiRecord.set(key, []) - } - let originSuccess = config.success; - config.success = function() { - const res = arguments[0] || {}; - apiRecord.get(key).push({ - ...res - }) + mockWxMethodConfig.forEach(item => { + let originMethod = wx[item.name]; + Object.defineProperty(wx, item.name, { + configurable: true, + enumerable: true, + writable: true, + value: function() { + const config = arguments[0] || {}; + let { recording, apiRecord } = state; + if (recording) { + let key = item.recordStringify(config) + if (!apiRecord.has(key)) { + apiRecord.set(key, []) + } + let originSuccess = config.success; + config.success = function() { + const res = arguments[0] || {}; + apiRecord.get(key).push({ + ...res + }) - originSuccess.apply(this, arguments); - } - return originRequest.apply(this, arguments); + originSuccess.apply(this, arguments); + } + return originMethod.apply(this, arguments); + } + return originMethod.apply(this, arguments); } - return originRequest.apply(this, arguments); - } - }); + }) + }) } if (['App', 'Page'].indexOf(type) > -1) { mountStateAndWrapEvent(vm); diff --git a/packages/wxa-cli/src/tester/wxa-e2eTest/mockWxMethodConfig.js b/packages/wxa-cli/src/tester/wxa-e2eTest/mockWxMethodConfig.js new file mode 100644 index 00000000..ef43d86b --- /dev/null +++ b/packages/wxa-cli/src/tester/wxa-e2eTest/mockWxMethodConfig.js @@ -0,0 +1,44 @@ +function getKey(configKey) { + return `${this.name}__e2e__${configKey.join('__e2e__')}` +} +export default [ + { + name: 'showModal', + recordStringify(config) { + return getKey.call(this, [ + config.title, config.content, config.editable, config.showCancel, config.confirmText + ]) + }, + tpl: (() => { + let key = ['config.title || ""', 'config.content || ""', 'config.editable || ""', 'config.showCancel || ""', 'config.confirmText || ""'] + return `showModal__e2e__\${${key.join('}__e2e__${')}}` + })() + }, + { + name: 'showActionSheet', + recordStringify(config) { + return getKey.call(this, [ + config.alertText, config.itemColor, config.itemList.join(',') + ]) + }, + tpl: (() => { + let key = ['config.alertText || ""', 'config.itemColor || ""', 'config.itemList.join(",") || ""'] + return `showActionSheet__e2e__\${${key.join('}__e2e__${')}}` + })() + }, + { + name: 'request', + recordStringify(config) { + console.log(getKey.call(this, [ + config.url, config.method, Object.keys(config.data).join(',') + ])); + return getKey.call(this, [ + config.url, config.method, Object.keys(config.data).join(',') + ]) + }, + tpl: (() => { + let key = ['config.url || ""', 'config.method || ""', 'Object.keys(config.data).join(",") || ""'] + return `request__e2e__\${${key.join('}__e2e__${')}}` + })() + } + ] diff --git a/packages/wxa-cli/src/tester/wxa-e2eTest/runTestCase.js b/packages/wxa-cli/src/tester/wxa-e2eTest/runTestCase.js index 9d3f5b23..021d97cf 100644 --- a/packages/wxa-cli/src/tester/wxa-e2eTest/runTestCase.js +++ b/packages/wxa-cli/src/tester/wxa-e2eTest/runTestCase.js @@ -3,6 +3,8 @@ import path from 'path'; import fs from 'fs'; import testCase2js from './e2eTestCase2js.js'; import {exec, execSync} from 'child_process'; +import mockWxMethodConfig from './mockWxMethodConfig'; + // -t 跑测试用例 // -s --screenshot 进行截屏比对 // --base 截屏作为expected基准,不对截屏进行比对 @@ -58,7 +60,8 @@ export default async function(cmd, wxaConfigs) { base: !!cmd.base, screenshotDiff: !!cmd.screenshotDiff, noMockApi: !!cmd.noMock, - customExpect: !!cmd.customExpect + customExpect: !!cmd.customExpect, + mockWxMethodConfig }); writeFile(path.join(testDir, '.cache', 'index.test.js'), recordString) } catch (err) {