-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Cypress screenshotting functionality causes window resize event #9443
Comments
Taking the screenshot does triggers a few window resize events, which I could see creating some unexpected failures depending on the app. Reproducible example
<html>
<body>
<p>Resize the browser window to fire the <code>resize</code> event.</p>
<p>Window height: <span id="height"></span></p>
<p>Window width: <span id="width"></span></p>
<script>
const heightOutput = document.querySelector('#height');
const widthOutput = document.querySelector('#width');
function reportWindowSize() {
heightOutput.textContent = window.innerHeight;
widthOutput.textContent = window.innerWidth;
}
window.onresize = reportWindowSize;
</script>
</body>
</html>
it('resize event', () => {
cy.visit('index.html')
cy.get('#height').should('be.empty')
cy.get('#width').should('be.empty')
cy.screenshot()
cy.get('#height').should('be.empty')
cy.get('#width').should('be.empty')
}) |
is there any update? |
Is there at least any workaround to this problem?? |
this is very frustrated when we use cypress to determine our image has breaking diff, and this issue make a lot of false alarm |
@fboechats The workaround that worked for me was |
I was not able to reproduce the issue using the reproducible example above. I am using the following versions: Cypress: 9.7.0 Is anyone able to reproduce the issue on the latest Cypress and browser versions? |
Closing as I can no longer reproduce on the newest version of Cypress and browsers. |
The same behaviour on Cypress 12.5.1 To workaround it, I written custom command to make a screenshot of element. Cypress.Commands.add('customScreenshot', (element, screenshotName) => {
cy.get(`${element} canvas`).then(([$el]) => ($el.getBoundingClientRect())).then((rect) => {
const iframe = window.parent.document
.querySelector('iframe.aut-iframe');
const parentRect = iframe.getBoundingClientRect();
const scale = parentRect.width / iframe.clientWidth;
cy.screenshot(screenshotName, {
// tricky way to make screenshots to avoid screen resizing
// we do a screenshot of the whole screen including runner and then clip it
// according to iframe coordinates and scale in the runner
overwrite: true,
capture: 'runner',
scale: false,
clip: {
x: parentRect.x + rect.x * scale,
y: parentRect.y + rect.y * scale,
width: rect.width * scale,
height: rect.height * scale,
},
});
});
}); |
Just an idea, the issue might be reproduced only if total browser size is not enough to show runner sidepanel and iframe with tested application at the same time (in this case, |
@bsekachev I think that your assumption is correct. When your viewport is too big to fit the runner browser window, the runner will scale your application, but when you perform the screenshot command, scale it will be removed (I am not sure why it works that way...). So it happens for me only when in the test I set by import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
baseUrl: 'https://localhost:4300',
setupNodeEvents(on) {
on('before:browser:launch', (browser = {} as Cypress.Browser, launchOptions) => {
console.log(
'launching browser %s is headless? %s',
browser.name,
browser.isHeadless,
)
// the browser width and height we want to get
const width = 2560
const height = 1440
console.log('setting the browser window size to %d x %d', width, height)
if (browser.name === 'chrome' && browser.isHeadless) {
launchOptions.args.push(`--window-size=${width},${height}`)
// force screen to be non-retina and just use our given resolution
launchOptions.args.push('--force-device-scale-factor=1')
}
if (browser.name === 'electron' && browser.isHeadless) {
// might not work on CI for some reason
launchOptions.preferences.width = width
launchOptions.preferences.height = height
}
if (browser.name === 'firefox' && browser.isHeadless) {
launchOptions.args.push(`--width=${width}`)
launchOptions.args.push(`--height=${height}`)
}
// IMPORTANT: return the updated browser launch options
return launchOptions;
}),
},
},
}) |
This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided. |
Approved |
Current behavior
Hello,
Our team spotted an issue, that when a cypress is creating a snapshot of a specific element (screenshot), a resize event is triggered in this case, because the panel is hidden by cypress and etc. Could this be eliminated somehow, because this is not normal behavior. In our case, if a resize event is triggered, some of the components are changing their state, in this case, test flakiness appears. Is it possible to get rid of this side effect somehow?
Desired behavior
Window resize event is not triggered
Test code to reproduce
None
Versions
All versions affected
The text was updated successfully, but these errors were encountered: