Skip to content

Instantly share code, notes, and snippets.

@syntapy
Created January 18, 2023 22:40
Show Gist options
  • Save syntapy/a2ef617cb4ecb441eb7bc9510fa1ee09 to your computer and use it in GitHub Desktop.
Save syntapy/a2ef617cb4ecb441eb7bc9510fa1ee09 to your computer and use it in GitHub Desktop.
test file
const { defineConfig } = require('cypress')
module.exports = defineConfig({
fixturesFolder: 'cypress/fixtures',
screenshotsFolder: 'cypress/screenshots',
videosFolder: 'cypress/videos',
e2e: {
setupNodeEvents(on, config) {
},
baseUrl: 'https://the-internet.herokuapp.com/',
},
})
function setViewport(width, height) {
Cypress.config('viewportWidth', width)
Cypress.config('viewportHeight', height)
}
describe('Sample Tests', () => {
it('viewport change', () => {
cy.visit('/')
const vpWidth = Cypress.config('viewportWidth')
const vpHeight = Cypress.config('viewportHeight')
cy.wrap(vpWidth).should('not.be.undefined')
cy.wrap(vpHeight).should('not.be.undefined')
cy.setViewport(vpWidth+100, vpHeight+100) // custom command: does NOT work
cy.viewport(vpWidth+100, vpHeight+100) // does NOT work
setViewport(vpWidth+100, vpHeight+100) // works
const vpWidthNew = Cypress.config('viewportWidth')
const vpHeightNew = Cypress.config('viewportHeight')
cy.wrap(vpWidthNew).should('equal', vpWidth+100)
cy.wrap(vpHeightNew).should('equal', vpHeight+100)
cy.setViewport(vpWidth, vpHeight) // custom command: does NOT work
cy.viewport(vpWidth, vpHeight) // does NOT work
setViewport(vpWidth, vpHeight) // works
const vpWidthOriginal = Cypress.config('viewportWidth')
const vpHeightOriginal = Cypress.config('viewportHeight')
cy.wrap(vpWidthOriginal).should('equal', vpWidth)
cy.wrap(vpHeightOriginal).should('equal', vpHeight)
})
})
Cypress.on('viewport:changed', (newValue) => {
Cypress.config('viewportWidth', newValue.viewportWidth)
Cypress.config('viewportHeight', newValue.viewportHeight)
})
Cypress.Commands.add('setViewport', (viewportWidth, viewportHeight) => {
cy.viewport(viewportWidth, viewportHeight)
Cypress.config('viewportWidth', viewportWidth)
Cypress.config('viewportHeight', viewportHeight)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment