diff --git a/docs/webapi/saveScreenshot.mustache b/docs/webapi/saveScreenshot.mustache index 3014ee9df..cc9305659 100644 --- a/docs/webapi/saveScreenshot.mustache +++ b/docs/webapi/saveScreenshot.mustache @@ -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 \ No newline at end of file +@param fileName +@param fullPage (optional) \ No newline at end of file diff --git a/lib/helper/Nightmare.js b/lib/helper/Nightmare.js index dd3c5bb05..01db4085d 100644 --- a/lib/helper/Nightmare.js +++ b/lib/helper/Nightmare.js @@ -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); } /** diff --git a/lib/helper/SeleniumWebdriver.js b/lib/helper/SeleniumWebdriver.js index 6c799702b..e2218fb6a 100644 --- a/lib/helper/SeleniumWebdriver.js +++ b/lib/helper/SeleniumWebdriver.js @@ -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) { @@ -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}} * diff --git a/lib/helper/WebDriverIO.js b/lib/helper/WebDriverIO.js index 0a7b5cd40..070d8b2af 100644 --- a/lib/helper/WebDriverIO.js +++ b/lib/helper/WebDriverIO.js @@ -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) { @@ -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}} * diff --git a/test/helper/Protractor_test.js b/test/helper/Protractor_test.js index 4cec20691..ab39964f5 100644 --- a/test/helper/Protractor_test.js +++ b/test/helper/Protractor_test.js @@ -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('/') diff --git a/test/helper/webapi.js b/test/helper/webapi.js index 781c51256..c24fb721a 100644 --- a/test/helper/webapi.js +++ b/test/helper/webapi.js @@ -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'); @@ -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 }; @@ -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')