Skip to content

Commit 4aa73a0

Browse files
authored
Fixed screenshot on failure for active session of WebDriver (#2312)
* Fixed session for WebDriver * fix to a session test * fixed tests for Playwright
1 parent 1957d3d commit 4aa73a0

File tree

8 files changed

+54
-3
lines changed

8 files changed

+54
-3
lines changed

lib/helper/WebDriver.js

+1
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ class WebDriver extends Helper {
594594
return browser;
595595
},
596596
stop: async (browser) => {
597+
if (!browser) return;
597598
return browser.deleteSession();
598599
},
599600
loadVars: async (browser) => {

lib/plugin/screenshotOnFail.js

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ module.exports = function (config) {
118118
|| err.message.indexOf('no such window: target window already closed') > -1
119119
)
120120
) {
121+
output.log(`Can't make screenshot, ${err}`);
121122
helper.isRunning = false;
122123
}
123124
}

lib/session.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ function session(sessionName, config, fn) {
5858
delete savedSessions[sessionName];
5959
};
6060

61-
event.dispatcher.once(event.test.finished, () => closeBrowsers());
61+
event.dispatcher.once(event.test.after, () => {
62+
recorder.add('close session browsers', closeBrowsers);
63+
});
6264

6365
if (!savedSessions[sessionName]) {
6466
throw new Error('Configured helpers do not support starting sessions. Please use a helper with session support.');

test/acceptance/codecept.Playwright.js

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ module.exports.config = {
1919
include: {},
2020
bootstrap: false,
2121
mocha: {},
22+
plugins: {
23+
screenshotOnFail: {
24+
enabled: true,
25+
},
26+
},
2227
name: 'acceptance',
2328
gherkin: {
2429
features: './gherkin/*.feature',

test/acceptance/codecept.Puppeteer.js

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ module.exports.config = {
2424
include: {},
2525
bootstrap: false,
2626
mocha: {},
27+
plugins: {
28+
screenshotOnFail: {
29+
enabled: true,
30+
},
31+
},
2732
name: 'acceptance',
2833
gherkin: {
2934
features: './gherkin/*.feature',

test/acceptance/codecept.WebDriver.js

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ module.exports.config = {
2727
bootstrap: done => setTimeout(done, 5000), // let's wait for selenium
2828
mocha: {},
2929
name: 'acceptance',
30+
plugins: {
31+
screenshotOnFail: {
32+
enabled: true,
33+
},
34+
},
3035
gherkin: {
3136
features: './gherkin/*.feature',
3237
steps: ['./gherkin/steps.js'],

test/acceptance/session_test.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const assert = require('assert');
2-
const path = require('path');
2+
3+
const { event } = codeceptjs;
34

45
Feature('Session');
56

@@ -76,6 +77,28 @@ Scenario('Different cookies for different sessions @WebDriverIO @Protractor @Pla
7677
assert.notEqual(cookies.john, cookies.mary);
7778
});
7879

80+
81+
Scenario('should save screenshot for active session @WebDriverIO @Puppeteer @Playwright', async function (I) {
82+
I.amOnPage('/form/bug1467');
83+
I.saveScreenshot('original.png');
84+
I.amOnPage('/');
85+
session('john', async () => {
86+
await I.amOnPage('/form/bug1467');
87+
event.dispatcher.emit(event.test.failed, this);
88+
});
89+
90+
const fileName = clearString(this.title);
91+
92+
const [original, failed] = await I.getMD5Digests([
93+
`${output_dir}/original.png`,
94+
`${output_dir}/${fileName}.failed.png`,
95+
]);
96+
97+
// Assert that screenshots of same page in same session are equal
98+
assert.equal(original, failed);
99+
});
100+
101+
79102
Scenario('should throw exception and close correctly @WebDriverIO @Protractor @Puppeteer @Playwright', (I) => {
80103
I.amOnPage('/form/bug1467#session1');
81104
I.checkOption('Yes');
@@ -85,6 +108,7 @@ Scenario('should throw exception and close correctly @WebDriverIO @Protractor @P
85108
I.seeCheckboxIsChecked({ css: 'input[value=No]' });
86109
});
87110
I.seeCheckboxIsChecked({ css: 'input[value=Yes]' });
111+
I.amOnPage('/info');
88112
}).fails();
89113

90114
Scenario('async/await @WebDriverIO @Protractor', (I) => {
@@ -203,3 +227,11 @@ Scenario('should return a value @WebDriverIO @Protractor @Puppeteer @Playwright
203227
I.click('Submit');
204228
I.see('[description] => Information');
205229
});
230+
231+
function clearString(str) {
232+
if (!str) return '';
233+
/* Replace forbidden symbols in string
234+
*/
235+
return str
236+
.replace(/ /g, '_');
237+
}

test/unit/plugin/screenshotOnFail_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('screenshotOnFail', () => {
1313
recorder.reset();
1414
screenshotSaved = sinon.spy();
1515
container.clear({
16-
WebDriverIO: {
16+
WebDriver: {
1717
options: {},
1818
saveScreenshot: screenshotSaved,
1919
},

0 commit comments

Comments
 (0)