diff --git a/packages/wxa-cli/src/tester/e2eTester.js b/packages/wxa-cli/src/tester/e2eTester.js index 1e3fde17..4d5efa1e 100644 --- a/packages/wxa-cli/src/tester/e2eTester.js +++ b/packages/wxa-cli/src/tester/e2eTester.js @@ -114,9 +114,9 @@ class TesterScheduler extends Schedule { ) { mdl.code = ` var __e2e__state = require('wxa://wxa-e2eTest/state.js') - var __testSuitePlugin = require('wxa://wxa-e2eTest/e2eTestSuite.js'); var __e2eMockWxMethod = require('wxa://wxa-e2eTest/e2eMockWxMethod.js'); __e2eMockWxMethod.mock({state: __e2e__state}); + var __testSuitePlugin = require('wxa://wxa-e2eTest/e2eTestSuite.js'); require('@wxa/core').wxa.use(__testSuitePlugin, {record: ${!!this.cmdOptions.record}, state: __e2e__state}); ${mdl.code} `; diff --git a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs index 9dc66ce8..113f5917 100644 --- a/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs +++ b/packages/wxa-cli/src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs @@ -51,7 +51,11 @@ afterAll(async () => { for (let j = 0; j < testCaseNameArr.length; j++) { let testName = testCaseNameArr[j]; test(testName, async () => { - let screenshotDir = path.join(testDir, testName, screenshotDirname); + let screenshotDir = path.join(testDir, '.replay_result', screenshotDirname, testName, 'screenshot'); + let baseScreenshot = path.join(testDir, testName, 'base_screenshot'); + if (base) { + screenshotDir = baseScreenshot; + } let diffDir = path.join(screenshotDir, 'diff'); if (base) { try { @@ -60,9 +64,9 @@ for (let j = 0; j < testCaseNameArr.length; j++) { } } try { - fs.mkdirSync(screenshotDir) + mkdirsSync(screenshotDir) if (!base) { - fs.mkdirSync(diffDir); + mkdirsSync(diffDir); } } catch(err) { } @@ -80,7 +84,11 @@ for (let j = 0; j < testCaseNameArr.length; j++) { let mapKey = `<%- mockItem.tpl %>` if (apiMockMap[mapKey] && apiMockMap[mapKey].length > 0) { console.log(mapKey, 'mock success') + <% if(mockItem.name === 'request') {%> return apiMockMap[mapKey].shift(); + <% } else {%> + return apiMockMap[mapKey][0]; + <% }%> } return new Promise(resolve => { config.success = res => resolve(res) @@ -104,7 +112,10 @@ for (let j = 0; j < testCaseNameArr.length; j++) { let recordAction = recordItem.action; while (true) { page = await miniProgram.currentPage() - if (page.path === (recordAction.page || recordAction.appStartPage).split('?')[0]) { + if (recordAction.event === 'switchTab') { + await sleep(4000); + break; + } else if (page.path === (recordAction.page || recordAction.appStartPage).split('?')[0]) { break; } await sleep(1000); @@ -129,7 +140,8 @@ for (let j = 0; j < testCaseNameArr.length; j++) { let same = await screenShot({ screenshotDir, screenCount: screenCount++, - diff + diff, + baseScreenshot }) if (customExpect && i !== 0 && record[i - 1].customExpect && typeof record[i - 1].customExpect === 'function') { record[i - 1].customExpect(); @@ -156,7 +168,8 @@ for (let j = 0; j < testCaseNameArr.length; j++) { let same = await screenShot({ screenshotDir, screenCount: screenCount, - diff: screenshotDiff || record[record.length - 1].sceeenshotDiff + diff: screenshotDiff || record[record.length - 1].sceeenshotDiff, + baseScreenshot }) // 无diff图片,则比对通过 if (!base) { @@ -165,9 +178,9 @@ for (let j = 0; j < testCaseNameArr.length; j++) { }) } -async function screenShot({screenshotDir, screenCount, diff}) { +async function screenShot({screenshotDir, screenCount, diff, baseScreenshot}) { let actualImagePath = path.join(screenshotDir, `${screenCount}.png`); - let expectPath = path.resolve(screenshotDir, `../base_screenshot`, `${screenCount}.png` ); + let expectPath = baseScreenshot; await miniProgram.screenshot({path: actualImagePath}); if (diff) { let same = await imageDiff(actualImagePath, expectPath, path.join(screenshotDir, `diff/${screenCount}.png`)); @@ -208,3 +221,14 @@ function imageDiff(actualImage, expectImage, diffPath) { }) }) } +// 递归创建目录 同步方法 +function mkdirsSync(dirname) { + if (fs.existsSync(dirname)) { + return true; + } else { + if (mkdirsSync(path.dirname(dirname))) { + fs.mkdirSync(dirname); + return true; + } + } +} diff --git a/packages/wxa-cli/src/tester/wxa-e2eTest/runTestCase.js b/packages/wxa-cli/src/tester/wxa-e2eTest/runTestCase.js index 57823f4a..60d159a0 100644 --- a/packages/wxa-cli/src/tester/wxa-e2eTest/runTestCase.js +++ b/packages/wxa-cli/src/tester/wxa-e2eTest/runTestCase.js @@ -25,7 +25,7 @@ export default async function(cmd, wxaConfigs) { } else { let files = fs.readdirSync(testDir); files.forEach((item) => { - if (item === '.cache') { + if (item[0] === '.') { return; } let stat = fs.lstatSync(path.join(testDir, item)); @@ -49,7 +49,7 @@ export default async function(cmd, wxaConfigs) { screenshotPath = 'base_screenshot'; } else { let timeStamp = formatDate(+new Date()); - screenshotPath = timeStamp; + screenshotPath = timeStamp.replace(' ', '_').replace(/:/g, '.'); } try { @@ -68,7 +68,7 @@ export default async function(cmd, wxaConfigs) { cliPath: cli.split(path.sep).join('/'), testCaseNameArr: JSON.stringify(testCaseNameArr), testDir: testDir.split(path.sep).join('/'), - screenshotPath: screenshotPath.replace(' ', '_').replace(/:/g, '.'), + screenshotPath, base: !!cmd.base, screenshotDiff: screenshotDiff, mockApi: cmd.mock,