Skip to content

save videos for sessions #3505

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

Merged
merged 2 commits into from
Nov 29, 2022
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
3 changes: 2 additions & 1 deletion lib/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ class Codecept {
runHooks() {
// default hooks
runHook(require('./listener/steps'));
runHook(require('./listener/artifacts'));
runHook(require('./listener/config'));
runHook(require('./listener/helpers'));
runHook(require('./listener/timeout'));
runHook(require('./listener/exit'));

// custom hooks
// custom hooks (previous iteration of plugins)
this.config.hooks.forEach(hook => runHook(hook));
}

Expand Down
56 changes: 40 additions & 16 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,11 @@ class Playwright extends Helper {
browserContext = browser.context();
page = await browser.firstWindow();
} else {
browserContext = await this.browser.newContext(config);
browserContext = await this.browser.newContext(Object.assign(this.options, config));
page = await browserContext.newPage();
}

if (this.options.trace) await browserContext.tracing.start({ screenshots: true, snapshots: true });
await targetCreatedHandler.call(this, page);
await this._setPage(page);
// Create a new page inside context.
Expand Down Expand Up @@ -2039,37 +2040,42 @@ class Playwright extends Helper {
}

if (this.options.recordVideo && this.page && this.page.video()) {
test.artifacts.video = `${global.output_dir}${pathSeparator}videos${pathSeparator}${clearString(test.title)}_${Date.now()}.failed.webm`;
this.page.video().saveAs(test.artifacts.video).then(() => {
this.page.video().delete().catch(e => {});
});
test.artifacts.video = await saveVideoForPage(this.page, `${test.title}.failed`);
for (const sessionName in this.sessionPages) {
test.artifacts[`video_${sessionName}`] = await saveVideoForPage(this.sessionPages[sessionName], `${test.title}_${sessionName}.failed`);
}
}

if (this.options.trace) {
const path = `${`${global.output_dir}${pathSeparator}trace${pathSeparator}${clearString(test.title)}`.slice(0, 251)}.zip`;
await this.browserContext.tracing.stop({ path });
test.artifacts.trace = path;
test.artifacts.trace = await saveTraceForContext(this.browserContext, `${test.title}.failed`);
for (const sessionName in this.sessionPages) {
if (!this.sessionPages[sessionName].context) continue;
test.artifacts[`trace_${sessionName}`] = await saveTraceForContext(this.sessionPages[sessionName].context(), `${test.title}_${sessionName}.failed`);
}
}
}

async _passed(test) {
if (this.options.recordVideo && this.page && this.page.video()) {
test.artifacts.video = `${global.output_dir}${pathSeparator}videos${pathSeparator}${clearString(test.title)}_${Date.now()}.passed.webm`;

if (this.options.keepVideoForPassedTests) {
this.page.video().saveAs(test.artifacts.video).then(() => {
this.page.video().delete().catch(e => {});
});
test.artifacts.video = await saveVideoForPage(this.page, `${test.title}.passed`);
for (const sessionName of Object.keys(this.sessionPages)) {
test.artifacts[`video_${sessionName}`] = await saveVideoForPage(this.sessionPages[sessionName], `${test.title}_${sessionName}.passed`);
}
} else {
this.page.video().delete().catch(e => {});
}
}

if (this.options.trace) {
if (this.options.keepTraceForPassedTests) {
const path = `${global.output_dir}${pathSeparator}trace${pathSeparator}${clearString(test.title)}.zip`;
await this.browserContext.tracing.stop({ path });
test.artifacts.trace = path;
if (this.options.trace) {
test.artifacts.trace = await saveTraceForContext(this.browserContext, `${test.title}.passed`);
for (const sessionName in this.sessionPages) {
if (!this.sessionPages[sessionName].context) continue;
test.artifacts[`trace_${sessionName}`] = await saveTraceForContext(this.sessionPages[sessionName].context(), `${test.title}_${sessionName}.passed`);
}
}
} else {
await this.browserContext.tracing.stop();
}
Expand Down Expand Up @@ -2940,3 +2946,21 @@ async function refreshContextSession() {
});
}
}

async function saveVideoForPage(page, name) {
if (!page.video()) return null;
const fileName = `${global.output_dir}${pathSeparator}videos${pathSeparator}${Date.now()}_${clearString(name).slice(0, 245)}.webm`;
page.video().saveAs(fileName).then(() => {
if (!page) return;
page.video().delete().catch(e => {});
});
return fileName;
}

async function saveTraceForContext(context, name) {
if (!context) return;
if (!context.tracing) return;
const fileName = `${`${global.output_dir}${pathSeparator}trace${pathSeparator}${Date.now()}_${clearString(name)}`.slice(0, 245)}.zip`;
await context.tracing.stop({ path: fileName });
return fileName;
}
2 changes: 0 additions & 2 deletions lib/helper/extras/PlaywrightRestartOpts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ module.exports = {

restarts = Object.keys(RESTART_OPTS).find(key => RESTART_OPTS[key] === restart);

console.log(restarts);

if (restarts === null || restarts === undefined) throw new Error('No restart strategy set, use the following values for restart: session, context, browser');
},

Expand Down
19 changes: 19 additions & 0 deletions lib/listener/artifacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const event = require('../event');
const recorder = require('../recorder');

/**
* Create and clean up empty artifacts
*/
module.exports = function () {
event.dispatcher.on(event.test.before, (test) => {
test.artifacts = {};
});

event.dispatcher.on(event.test.after, (test) => {
recorder.add('clean up empty artifacts', () => {
for (const key in (test.artifacts || {})) {
if (!test.artifacts[key]) delete test.artifacts[key];
}
});
});
};