Skip to content

Commit

Permalink
feat(driver): Add overwrite option to Cypress.Screenshot.defaults (
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmunechika committed Oct 13, 2021
1 parent 4f38ffd commit bc21574
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/driver/cypress/integration/cypress/screenshot_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Screenshot } = Cypress
const DEFAULTS = {
capture: 'fullPage',
scale: false,
overwrite: false,
disableTimersAndAnimations: true,
screenshotOnRunFailure: true,
blackout: [],
Expand Down Expand Up @@ -64,6 +65,14 @@ describe('src/cypress/screenshot', () => {
expect(Screenshot.getConfig().scale).to.equal(true)
})

it('sets overwrite if specified', () => {
Screenshot.defaults({
overwrite: true,
})

expect(Screenshot.getConfig().overwrite).to.equal(true)
})

it('sets disableTimersAndAnimations if specified', () => {
Screenshot.defaults({
disableTimersAndAnimations: false,
Expand Down Expand Up @@ -187,6 +196,20 @@ describe('src/cypress/screenshot', () => {
.and.eq('https://on.cypress.io/screenshot-api')
})

it('throws if overwrite is not a boolean', () => {
const fn = () => {
Screenshot.defaults({ overwrite: 'foo' })
}

expect(fn).to.throw()
.with.property('message')
.and.include('`Cypress.Screenshot.defaults()` `overwrite` option must be a boolean. You passed: `foo`')

expect(fn).to.throw()
.with.property('docsUrl')
.and.eq('https://on.cypress.io/screenshot-api')
})

it('throws if disableTimersAndAnimations is not a boolean', () => {
const fn = () => {
Screenshot.defaults({ disableTimersAndAnimations: 'foo' })
Expand Down
1 change: 1 addition & 0 deletions packages/driver/src/cypress/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const _reset = () => {
disableTimersAndAnimations: true,
screenshotOnRunFailure: true,
blackout: [],
overwrite: false,
onBeforeScreenshot () {},
onAfterScreenshot () {},
}
Expand Down
4 changes: 3 additions & 1 deletion packages/runner/__snapshots__/retries.mochaEvents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6476,6 +6476,7 @@ exports['src/cypress/runner retries mochaEvents screenshots retry screenshot in
"height": 660
},
"scaled": false,
"overwrite": false,
"blackout": [],
"startTime": "match.string",
"current": 1,
Expand All @@ -6491,7 +6492,8 @@ exports['src/cypress/runner retries mochaEvents screenshots retry screenshot in
"scale": false,
"waitForCommandSynchronization": false,
"disableTimersAndAnimations": true,
"blackout": []
"blackout": [],
"overwrite": false
}

exports['serialize state - retries'] = {
Expand Down
1 change: 1 addition & 0 deletions packages/runner/__snapshots__/runner.mochaEvents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,7 @@ exports['src/cypress/runner other specs screenshots screenshot after failed test
"height": 660
},
"scaled": true,
"overwrite": false,
"blackout": [],
"startTime": "1970-01-01T00:00:00.000Z"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,39 @@ describe('taking screenshots', () => {
})
})

// @see https://github.com/cypress-io/cypress/issues/7955
it('can set overwrite default option to replace existing filename', () => {
Cypress.Screenshot.defaults({
overwrite: true,
})

cy.viewport(600, 200)
cy.visit('http://localhost:3322/color/yellow')
cy.screenshot('overwrite-test', {
clip: { x: 10, y: 10, width: 160, height: 80 },
})

cy.task('check:screenshot:size', {
name: `${path.basename(__filename)}/overwrite-test.png`,
width: 160,
height: 80,
devicePixelRatio,
})

cy.screenshot('overwrite-test', {
clip: { x: 10, y: 10, width: 100, height: 50 },
})

cy.readFile(`cypress/screenshots/${path.basename(__filename)}/overwrite-test (1).png`).should('not.exist')

cy.task('check:screenshot:size', {
name: `${path.basename(__filename)}/overwrite-test.png`,
width: 100,
height: 50,
devicePixelRatio,
})
})

context('before hooks', () => {
before(() => {
// failure 2
Expand Down

4 comments on commit bc21574

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bc21574 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.7.0/circle-develop-bc21574d014d965d507c3736ee5d366c3f6dc223/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bc21574 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.7.0/appveyor-develop-bc21574d014d965d507c3736ee5d366c3f6dc223/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bc21574 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.7.0/appveyor-develop-bc21574d014d965d507c3736ee5d366c3f6dc223/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bc21574 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.7.0/circle-develop-bc21574d014d965d507c3736ee5d366c3f6dc223/cypress.tgz

Please sign in to comment.