Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): coverage with WebDriver - devtools #4349

Merged
merged 3 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/helper/WebDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ class WebDriver extends Helper {

if (this.options.automationProtocol) {
this.puppeteerBrowser = await this.browser.getPuppeteer();
this.page = (await this.puppeteerBrowser.pages())[0];
}

return this.browser;
Expand Down Expand Up @@ -2731,7 +2732,6 @@ class WebDriver extends Helper {
this.recording = true;
this.recordedAtLeastOnce = true;

this.page = (await this.puppeteerBrowser.pages())[0];
await this.page.setRequestInterception(true);

this.page.on('request', (request) => {
Expand Down
35 changes: 34 additions & 1 deletion lib/plugin/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const defaultConfig = {
outputDir: 'output/coverage',
};

const supportedHelpers = ['Puppeteer', 'Playwright'];
const supportedHelpers = ['Puppeteer', 'Playwright', 'WebDriver'];

const v8CoverageHelpers = {
Playwright: {
Expand Down Expand Up @@ -61,6 +61,33 @@ const v8CoverageHelpers = {
await coverageReport.add(coverageList);
},
},
WebDriver: {
startCoverage: async (page) => {
await Promise.all([
page.coverage.startJSCoverage({
resetOnNavigation: false,
includeRawScriptCoverage: true,
}),
page.coverage.startCSSCoverage({
resetOnNavigation: false,
}),
]);
},
takeCoverage: async (page, coverageReport) => {
const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage(),
]);
// to raw V8 script coverage
const coverageList = [...jsCoverage.map((it) => {
return {
source: it.text,
...it.rawScriptCoverage,
};
}), ...cssCoverage];
await coverageReport.add(coverageList);
},
},
};

/**
Expand Down Expand Up @@ -109,11 +136,17 @@ module.exports = function (config) {
const debug = debugModule(`codeceptjs:plugin:${helperName.toLowerCase()}Coverage`);

const helper = helpers[helperName];

if (helperName === 'WebDriver' && !helper.config.devtoolsProtocol) throw Error('Coverage is currently supporting the WebDriver running with Devtools protocol.');

const v8Helper = v8CoverageHelpers[helperName];

const coverageOptions = {
...config,
};

if (helperName === 'WebDriver') coverageOptions.coverageProvider = 'v8';

const coverageReport = new CoverageReport(coverageOptions);
coverageReport.cleanCache();

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"lodash.merge": "4.6.2",
"mkdirp": "1.0.4",
"mocha": "10.4.0",
"monocart-coverage-reports": "2.7.4",
"monocart-coverage-reports": "2.8.2",
"ms": "2.1.3",
"ora-classic": "5.4.2",
"pactum": "3.6.7",
Expand Down Expand Up @@ -179,4 +179,4 @@
"strict": false
}
}
}
}
49 changes: 49 additions & 0 deletions test/acceptance/codecept.WebDriver.devtools.coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const TestHelper = require('../support/TestHelper');

module.exports.config = {
tests: './*_test.js',
timeout: 10000,
output: './output',
helpers: {
WebDriver: {
url: TestHelper.siteUrl(),
browser: 'Chromium',
windowSize: '500x700',
devtoolsProtocol: true,
waitForTimeout: 5000,
capabilities: {
chromeOptions: {
args: ['--headless', '--disable-gpu', '--window-size=500,700'],
},
},
},
ScreenshotSessionHelper: {
require: '../support/ScreenshotSessionHelper.js',
outputPath: './output',
},
Expect: {},
},
include: {},
mocha: {},
name: 'acceptance',
plugins: {
screenshotOnFail: {
enabled: true,
},
coverage: {
enabled: true,
debug: true,
name: 'CodeceptJS Coverage Report',
sourceFilter: '**/src/**',
sourcePath: {
'todomvc-react/': '',
'todomvc.com/examples/react/': '',
},
outputDir: 'output/coverage',
},
},
gherkin: {
features: './gherkin/*.feature',
steps: ['./gherkin/steps.js'],
},
};
18 changes: 17 additions & 1 deletion test/plugin/plugin_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ describe('CodeceptJS plugin', function () {
expect(lines).toEqual(
expect.arrayContaining([
expect.stringContaining('writing output/coverage'),
expect.stringContaining('generated coverage reports: output/coverage/index.html'),
expect.stringContaining('generated coverage reports:'),
expect.stringContaining('output/coverage/index.html')
]),
);
expect(err).toBeFalsy();
done();
});
});

it('should generate the coverage report - WebDriver - Devtools protocol', (done) => {
exec(`${config_run_config('codecept.WebDriver.devtools.coverage.js', '@coverage')} --debug`, (err, stdout) => {
const lines = stdout.split('\n');
expect(lines).toEqual(
expect.arrayContaining([
expect.stringContaining('writing output/coverage'),
expect.stringContaining('generated coverage reports:'),
expect.stringContaining('output/coverage/index.html')
]),
);
expect(err).toBeFalsy();
Expand Down
Loading