diff --git a/lib/helper/coverage.js b/lib/helper/coverage.js index 32ae710..05514cc 100644 --- a/lib/helper/coverage.js +++ b/lib/helper/coverage.js @@ -26,29 +26,50 @@ module.exports = wd => { * @type utility * @return {Promise.} */ - wd.addPromiseChainMethod('coverage', function() { + wd.addPromiseChainMethod('coverage', function(opts = {}) { + let { + coverageIgnore = program.coverageIgnore, + keys, + } = opts; const tempDir = path.join(cwd, 'coverage', '.temp'); _.mkdir(tempDir); - return this.execute('return window.__coverage__') - .then(__coverage__ => { - if (!__coverage__) { - return this.execute('return location.href').then(url => { - logger.info(`>> coverage failed: ${url}`); + return this.execute('return { allKeys: Object.keys(window.__coverage__) }') + .then(async res => { + const { allKeys } = res; + if (!keys) { + keys = allKeys; + } + if (coverageIgnore) { + logger.info('handle coverageIgnore'); + const ignoreReg = new RegExp(coverageIgnore, 'i'); + keys = keys.filter(k => { + return !ignoreReg.test(k); }); } - const file = path.join(tempDir, `${+new Date()}_coverage.json`); - if (program.coverageIgnore) { - const ignoreReg = new RegExp(program.coverageIgnore, 'i'); - for (const k in __coverage__) { - if (ignoreReg.test(k)) { - delete __coverage__[k]; + if (!keys.length) { + logger.info('coverage keys is empty'); + return; + } + const covFile = path.join(tempDir, `${+new Date()}_coverage.json`); + const writer = fs.createWriteStream(covFile); + writer.write('{'); + for (let i = 0; i < keys.length; i++) { + const coverage = await this.execute(`return window.__coverage__['${keys[i]}']`); + if (coverage) { + const coverageJSON = JSON.stringify(coverage); + writer.write(`"${keys[i]}":${coverageJSON}`); + if (i < keys.length - 1) { + writer.write(','); } } } - fs.writeFileSync(file, JSON.stringify(__coverage__, null, 2)); - }) - .catch(e => { - console.log(e); + writer.write('}'); + await new Promise(resolve => { + writer.end(() => { + writer.close(); + resolve(); + }); + }); }); }); }; diff --git a/package.json b/package.json index 5898881..2efae5a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "macaca-wd", - "version": "4.3.4", + "version": "4.3.5", "description": "Macaca webdirver API for Node.js", "keywords": [ "macaca",