Skip to content
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
7 changes: 5 additions & 2 deletions docs/webapi/saveScreenshot.mustache
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Saves a screenshot to ouput folder (set in codecept.json).
Filename is relative to output folder.
Filename is relative to output folder.
Optionally resize the window to the full available page `scrollHeight` and `scrollWidth` to capture the entire page by passing `true` in as the second argument.

```js
I.saveScreenshot('debug.png');
I.saveScreenshot('debug.png',true) \\resizes to available scrollHeight and scrollWidth before taking screenshot
```
@param fileName
@param fileName
@param fullPage (optional)
25 changes: 19 additions & 6 deletions lib/helper/Nightmare.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,19 +797,32 @@ class Nightmare extends Helper {
}, lctype(locator), lcval(locator));
}

/**
* {{> ../webapi/saveScreenshot }}
*/
saveScreenshot(fileName) {
/**
* {{> ../webapi/saveScreenshot }}
*/
saveScreenshot(fileName, fullPage = false) {
let outputFile = path.join(global.output_dir, fileName);
this.debug('Screenshot is saving to ' + outputFile);
let recorder = require('../recorder');
return this.browser.screenshot(outputFile);

if (!fullPage) {
return this.browser.screenshot(outputFile);
}
return this.browser.evaluate(() => ({
height: document.body.scrollHeight,
width: document.body.scrollWidth
})).then(({
width,
height
}) => {
this.browser.viewport(width, height);
return this.browser.screenshot(outputFile);
});
}

_failed(test) {
let fileName = test.title.replace(/\W/g, '_') + '.failed.png';
return this.saveScreenshot(fileName);
return this.saveScreenshot(fileName, true);
}

/**
Expand Down
26 changes: 22 additions & 4 deletions lib/helper/SeleniumWebdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class SeleniumWebdriver extends Helper {

_failed(test) {
let fileName = test.title.replace(/ /g, '_') + '.failed.png';
return this.saveScreenshot(fileName);
return this.saveScreenshot(fileName, true);
}

_withinBegin(locator) {
Expand Down Expand Up @@ -571,20 +571,38 @@ class SeleniumWebdriver extends Helper {
/**
* {{> ../webapi/saveScreenshot }}
*/
saveScreenshot(fileName) {
saveScreenshot(fileName, fullPage = false) {
let outputFile = path.join(global.output_dir, fileName);
this.debug('Screenshot has been saved to ' + outputFile);
return this.browser.takeScreenshot().then(function (png) {

const writeFile = (png, outputFile) => {
let fs = require('fs');
var stream = fs.createWriteStream(outputFile);
let stream = fs.createWriteStream(outputFile);
stream.write(new Buffer(png, 'base64'));
stream.end();
return new Promise(function (resolve) {
return stream.on('finish', resolve);
});
};

if (!fullPage) {
return this.browser.takeScreenshot()
.then(png => writeFile(png, outputFile));
}
return this.browser.executeScript(() => ({
height: document.body.scrollHeight,
width: document.body.scrollWidth
})).then(({
width,
height
}) => {
this.browser.manage().window().setSize(width, height);
return this.browser.takeScreenshot()
.then(png => writeFile(png, outputFile));
});
}


/**
* {{> ../webapi/setCookie}}
*
Expand Down
23 changes: 19 additions & 4 deletions lib/helper/WebDriverIO.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class WebDriverIO extends Helper {
} else {
fileName = test.title.replace(/ /g, '_') + '.failed.png';
}
return this.saveScreenshot(fileName);
return this.saveScreenshot(fileName, true);
}

_withinBegin(locator) {
Expand Down Expand Up @@ -859,12 +859,27 @@ class WebDriverIO extends Helper {
/**
* {{> ../webapi/saveScreenshot}}
*/
saveScreenshot(fileName) {
saveScreenshot(fileName, fullPage = false) {
let outputFile = path.join(global.output_dir, fileName);
this.debug('Screenshot has been saved to ' + outputFile);
return this.browser.saveScreenshot(outputFile);

if (!fullPage) {
this.debug('Screenshot has been saved to ' + outputFile);
return this.browser.saveScreenshot(outputFile);
}
return this.browser.execute(() => ({
height: document.body.scrollHeight,
width: document.body.scrollWidth
})).then(({
width,
height
}) => {
this.browser.windowHandleSize(width, height);
this.debug('Screenshot has been saved to ' + outputFile);
return this.browser.saveScreenshot(outputFile);
});
}


/**
* {{> ../webapi/setCookie}}
*
Expand Down
6 changes: 6 additions & 0 deletions test/helper/Protractor_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ describe('Protractor', function() {
return assert.ok(fileExists(path.join(output_dir, 'protractor_user.png')), null, 'file does not exists');
});

it('should create full page a screenshot file in output dir', function*() {
yield I.amOnPage('/');
yield I.saveScreenshot('protractor_user_full.png',true);
return assert.ok(fileExists(path.join(output_dir, 'protractor_user_full.png')), null, 'file does not exists');
});

it('should create a screenshot on fail', function*() {
let test = { title: 'protractor should do smth' };
yield I.amOnPage('/')
Expand Down
41 changes: 24 additions & 17 deletions test/helper/webapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,23 @@ module.exports.tests = function() {
});
});

describe('window size #resizeWindow', () => {
it('should set initial window size', () => {
return I.amOnPage('/form/resize')
.then(() => I.click('Window Size'))
.then(() => I.see('Height 400', '#height'))
.then(() => I.see('Width 500', '#width'))
});

it('should resize window to specific dimensions', () => {
return I.amOnPage('/form/resize')
.then(() => I.resizeWindow(800, 600))
.then(() => I.click('Window Size'))
.then(() => I.see('Height 600', '#height'))
.then(() => I.see('Width 800', '#width'))
});
});

describe('#saveScreenshot', () => {
beforeEach(() => {
global.output_dir = path.join(global.codecept_dir, 'output');
Expand All @@ -472,6 +489,13 @@ module.exports.tests = function() {
.then(() => assert.ok(fileExists(path.join(output_dir, 'screenshot_'+sec)), null, 'file does not exists'));
});

it('should create a full page screenshot file in output dir', () => {
let sec = (new Date()).getUTCMilliseconds();
return I.amOnPage('/')
.then(() => I.saveScreenshot(`screenshot_full_${+sec}`,true))
.then(() => assert.ok(fileExists(path.join(output_dir, `screenshot_full_${+sec}`)), null, 'file does not exists'));
});

it('should create a screenshot on fail', () => {
let sec = (new Date()).getUTCMilliseconds().toString();
let test = { title: 'sw should do smth '+sec };
Expand Down Expand Up @@ -524,23 +548,6 @@ module.exports.tests = function() {
});
});

describe('window size #resizeWindow', () => {
it('should set initial window size', () => {
return I.amOnPage('/form/resize')
.then(() => I.click('Window Size'))
.then(() => I.see('Height 400', '#height'))
.then(() => I.see('Width 500', '#width'))
});

it('should resize window to specific dimensions', () => {
return I.amOnPage('/form/resize')
.then(() => I.resizeWindow(800, 600))
.then(() => I.click('Window Size'))
.then(() => I.see('Height 600', '#height'))
.then(() => I.see('Width 800', '#width'))
});
});

describe('#waitForElement', () => {
it('should wait for visibile element', () => {
return I.amOnPage('/form/wait_visible')
Expand Down