From ec52a5ed2a0432c868ecb0c252b7c2422abce539 Mon Sep 17 00:00:00 2001 From: Stefan Lau Date: Fri, 6 Oct 2017 16:37:58 +0200 Subject: [PATCH] feat: introduce option to specify delay before a screenshot is made --- doc/config.md | 5 +++++ lib/browser/browser.js | 6 ++++-- lib/config/browser-options.js | 12 +++++++++++- test/unit/browser/new-browser.js | 17 +++++++++++++---- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/doc/config.md b/doc/config.md index 84e4235e3..1c7bc806b 100644 --- a/doc/config.md +++ b/doc/config.md @@ -236,6 +236,11 @@ Settings list: :warning: Option does not work in Opera@12.16. +* `screenshotDelay` — allows to specify a delay (in milliseconds) before making any screenshot. + By default there is no delay. + + This is useful when the page has elements which are animated. + ## Sets You can link some set of tests with certain browsers using `sets`. diff --git a/lib/browser/browser.js b/lib/browser/browser.js index c1bbcba70..7ef94ef50 100644 --- a/lib/browser/browser.js +++ b/lib/browser/browser.js @@ -1,6 +1,7 @@ 'use strict'; const _ = require('lodash'); +const Promise = require('bluebird'); const wd = require('./wd-bluebird'); const Camera = require('./camera'); @@ -17,11 +18,12 @@ module.exports = class Browser { } captureViewportImage(page) { - return this._camera.captureViewportImage(page); + return Promise.delay(this.config.screenshotDelay) + .then(() => this._camera.captureViewportImage(page)); } serialize() { - const props = ['id', 'gridUrl', 'httpTimeout', 'screenshotMode', 'compositeImage']; + const props = ['id', 'gridUrl', 'httpTimeout', 'screenshotMode', 'screenshotDelay', 'compositeImage']; return { config: _.pick(this.config, props), diff --git a/lib/config/browser-options.js b/lib/config/browser-options.js index 0907ea75c..1fb39e22a 100644 --- a/lib/config/browser-options.js +++ b/lib/config/browser-options.js @@ -29,7 +29,8 @@ const getTopLevel = () => { windowSize: null, retry: 0, screenshotMode: 'auto', - compositeImage: false + compositeImage: false, + screenshotDelay: 0 }; const provideDefault = (key) => defaults[key]; @@ -207,6 +208,15 @@ function buildBrowserOptions(defaultFactory, extra) { } }), + screenshotDelay: option({ + defaultValue: defaultFactory('screenshotDelay'), + parseEnv: Number, + parseCli: Number, + validate: (value) => { + assertNonNegative(value, 'screenshotDelay'); + } + }), + compositeImage: booleanOption(defaultFactory('compositeImage')) }); } diff --git a/test/unit/browser/new-browser.js b/test/unit/browser/new-browser.js index 1ed116a36..ebaf21d16 100644 --- a/test/unit/browser/new-browser.js +++ b/test/unit/browser/new-browser.js @@ -421,16 +421,25 @@ describe('browser/new-browser', () => { }); describe('captureViewportImage', () => { - let browser; - beforeEach(() => { sandbox.stub(Camera.prototype, 'captureViewportImage'); + sandbox.stub(Promise, 'delay').returns(Promise.resolve()); + }); - browser = makeBrowser({browserName: 'browser', version: '1.0'}, {calibrate: true}); + it('should delay capturing by the configured amount', () => { + const browser = makeBrowser({browserName: 'browser', version: '1.0'}, {calibrate: false, screenshotDelay: 42}); + + return browser.launch() + .then(() => browser.captureViewportImage()) + .then(() => { + assert.calledOnce(Promise.delay); + assert.calledWith(Promise.delay, 42); + assert.callOrder(Promise.delay, Camera.prototype.captureViewportImage); + }); }); it('should delegate actual capturing to camera object', () => { - browser = makeBrowser({browserName: 'browser', version: '1.0'}, {calibrate: false}); + const browser = makeBrowser({browserName: 'browser', version: '1.0'}, {calibrate: false}); Camera.prototype.captureViewportImage.returns(Promise.resolve({some: 'image'}));