Skip to content

Commit

Permalink
feat(wd): screenshots for sessions (#4322)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored May 2, 2024
1 parent 2704307 commit 1e78e21
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
15 changes: 11 additions & 4 deletions lib/helper/WebDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1795,14 +1795,21 @@ class WebDriver extends Helper {
* {{> saveScreenshot }}
*/
async saveScreenshot(fileName, fullPage = false) {
const outputFile = screenshotOutputFolder(fileName);
let outputFile = screenshotOutputFolder(fileName);

if (this.activeSessionName) {
const browser = this.sessionWindows[this.activeSessionName];

if (browser) {
this.debug(`Screenshot of ${this.activeSessionName} session has been saved to ${outputFile}`);
return browser.saveScreenshot(outputFile);
for (const sessionName in this.sessionWindows) {
const activeSessionPage = this.sessionWindows[sessionName];
outputFile = screenshotOutputFolder(`${sessionName}_${fileName}`);

this.debug(`${sessionName} - Screenshot is saving to ${outputFile}`);

if (browser) {
this.debug(`Screenshot of ${sessionName} session has been saved to ${outputFile}`);
return browser.saveScreenshot(outputFile);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/plugin/screenshotOnFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ module.exports = function (config) {
allureReporter.addAttachment('Main session - Last Seen Screenshot', fs.readFileSync(path.join(global.output_dir, fileName)), dataType);

if (helper.activeSessionName) {
for (const sessionName in helper.sessionPages) {
const sessions = helper.sessionPages || helper.sessionWindows;
for (const sessionName in sessions) {
const screenshotFileName = `${sessionName}_${fileName}`;
test.artifacts[`${sessionName.replace(/ /g, '_')}_screenshot`] = path.join(global.output_dir, screenshotFileName);
allureReporter.addAttachment(`${sessionName} - Last Seen Screenshot`, fs.readFileSync(path.join(global.output_dir, screenshotFileName)), dataType);
Expand Down
23 changes: 15 additions & 8 deletions test/acceptance/session_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Scenario('screenshots reflect the current page of current session @Puppeteer @Pl
const [default1Digest, default2Digest, john1Digest, john2Digest] = await I.getSHA256Digests([
`${output_dir}/session_default_1.png`,
`${output_dir}/session_default_2.png`,
`${output_dir}/session_john_1.png`,
`${output_dir}/john_session_john_1.png`,
`${output_dir}/session_john_2.png`,
]);

Expand Down Expand Up @@ -77,24 +77,31 @@ Scenario('Different cookies for different sessions @Playwright @Puppeteer', asyn
I.expectNotEqual(cookies.john, cookies.mary);
});

Scenario('should save screenshot for active session @WebDriverIO @Puppeteer @Playwright', async function ({ I }) {
I.amOnPage('/form/bug1467');
I.saveScreenshot('original.png');
I.amOnPage('/');
Scenario('should save screenshot for sessions @WebDriverIO @Puppeteer @Playwright', async function ({ I }) {
await I.amOnPage('/form/bug1467');
await I.saveScreenshot('original.png');
await I.amOnPage('/');
await I.saveScreenshot('main_session.png');
session('john', async () => {
await I.amOnPage('/form/bug1467');
event.dispatcher.emit(event.test.failed, this);
});

const fileName = clearString(this.title);

const [original, failed] = await I.getSHA256Digests([
`${output_dir}/original.png`,
`${output_dir}/${fileName}.failed.png`,
`${output_dir}/john_${fileName}.failed.png`,
]);

// Assert that screenshots of same page in same session are equal
I.expectEqual(original, failed);
await I.expectEqual(original, failed);

// Assert that screenshots of sessions are created
const [main_original, session_failed] = await I.getSHA256Digests([
`${output_dir}/main_session.png`,
`${output_dir}/john_${fileName}.failed.png`,
]);
await I.expectNotEqual(main_original, session_failed);
});

Scenario('should throw exception and close correctly @WebDriverIO @Puppeteer @Playwright', ({ I }) => {
Expand Down
8 changes: 0 additions & 8 deletions test/support/ScreenshotSessionHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ const crypto = require('crypto');
const fs = require('fs');

class ScreenshotSessionHelper extends Helper {
_finishTest() {
// Cleanup screenshots created by session screenshot test
const screenshotDir = fs.readdirSync(this.outputPath, { withFileTypes: true })
.filter(item => item.isFile() && item.name.includes('session'));

screenshotDir.forEach(file => fs.unlinkSync(`${this.outputPath}/${file.name}`));
}

constructor(config) {
super(config);
this.outputPath = output_dir;
Expand Down

0 comments on commit 1e78e21

Please sign in to comment.