Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added wait before and after screenshot #15878

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2858,6 +2858,8 @@ declare namespace Cypress {
capture: 'runner' | 'viewport' | 'fullPage'
clip: Dimensions
disableTimersAndAnimations: boolean
waitBefore: number
waitAfter: number
padding: Padding
scale: boolean
onBeforeScreenshot: ($el: JQuery) => void
Expand Down
4 changes: 4 additions & 0 deletions packages/driver/src/cy/commands/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options = {}) => {
padding,
clip,
disableTimersAndAnimations,
waitBefore,
waitAfter,
onBeforeScreenshot,
onAfterScreenshot,
} = screenshotConfig
Expand Down Expand Up @@ -362,6 +364,7 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options = {}) => {
: $dom.wrap(state('document').documentElement)

return before()
.then(() => new Promise((resolve) => setTimeout(resolve, waitBefore)))
.then(() => {
if (onBeforeScreenshot) {
onBeforeScreenshot.call(state('ctx'), $el)
Expand All @@ -379,6 +382,7 @@ const takeScreenshot = (Cypress, state, screenshotConfig, options = {}) => {

return automateScreenshot(state, automationOptions)
})
.then(() => new Promise((resolve) => setTimeout(resolve, waitAfter)))
.then((props) => {
if (onAfterScreenshot) {
onAfterScreenshot.call(state('ctx'), $el, props)
Expand Down
12 changes: 12 additions & 0 deletions packages/driver/src/cypress/error_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,18 @@ module.exports = {
docsUrl: `https://on.cypress.io/${getScreenshotDocsPath(obj.cmd)}`,
}
},
invalid_waitBefore (obj) {
return {
message: `${cmd(obj.cmd)} \`waitBefore\` option must be a number. You passed: \`{{arg}}\``,
docsUrl: `https://on.cypress.io/${getScreenshotDocsPath(obj.cmd)}`,
}
},
invalid_waitAfter (obj) {
return {
message: `${cmd(obj.cmd)} \`waitAfter\` option must be a number. You passed: \`{{arg}}\``,
docsUrl: `https://on.cypress.io/${getScreenshotDocsPath(obj.cmd)}`,
}
},
multiple_elements: {
message: `${cmd('screenshot')} only works for a single element. You attempted to screenshot {{numElements}} elements.`,
docsUrl: 'https://on.cypress.io/screenshot',
Expand Down
26 changes: 25 additions & 1 deletion packages/driver/src/cypress/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const reset = () => {
disableTimersAndAnimations: true,
screenshotOnRunFailure: true,
blackout: [],
waitBefore: 0,
waitAfter: 0,
onBeforeScreenshot () {},
onAfterScreenshot () {},
}
Expand Down Expand Up @@ -118,7 +120,7 @@ const validate = (props, cmd, log) => {
})
}

const { capture, blackout, clip, padding } = props
const { capture, blackout, clip, padding, waitBefore, waitAfter } = props

if (capture) {
if (!validCaptures.includes(capture)) {
Expand Down Expand Up @@ -186,6 +188,28 @@ const validate = (props, cmd, log) => {
values.padding = normalizePadding(padding)
}

if (waitBefore) {
if (!_.isNumber(waitBefore) && waitBefore < 0) {
$errUtils.throwErrByPath('screenshot.invalid_waitBefore', {
log,
args: { cmd, arg: $utils.stringify(waitBefore) },
})
}

values.waitBefore = waitBefore
}

if (waitAfter) {
if (!_.isNumber(waitAfter) && waitAfter < 0) {
$errUtils.throwErrByPath('screenshot.invalid_waitAfter', {
log,
args: { cmd, arg: $utils.stringify(waitAfter) },
})
}

values.waitAfter = waitAfter
}

validateAndSetCallback(props, values, cmd, log, 'onBeforeScreenshot')
validateAndSetCallback(props, values, cmd, log, 'onAfterScreenshot')

Expand Down