cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.71k stars 3.16k forks source link

Screenshot of element is not properly captured with `scroll-behavior: smooth` #6018

Closed Workito closed 1 year ago

Workito commented 4 years ago

Current behavior:

I want to screenshots some elements, but screenshot command for some reason scroll when picture is taken. So picture doesn't contain the element.

There is video from testing... https://images.dvanakoncisveta.cz/nav.spec.js.mp4

There is taken picture... https://images.dvanakoncisveta.cz/actual.png

Desired behavior:

Screenshot the set element...

Steps to reproduce: (app code and test code)

I'm using cypress-plugin-snapshots plugin with default settings. But I havesame issue with cypress screenshot() too. it('nav', () => { cy.get('nav').toMatchImage(); });

Working demo with this issue is here https://github.com/Workito/cypress-test-tiny

Versions

cypress: 3.8.0 cypress-plugin-snapshots: 1.2.9 npm: 6.7.0 node: 11.15.0 Browser: Electron 78 (headless) or Chrome 79 (same issue) OS: MacOs 10.15.2 (19C57)

jennifer-shehane commented 4 years ago

Yeah, this is weird. I am not sure why it's doing this scroll behavior to capture an element that is already visible on the screen.

I narrowed it down from the original HTML to the example below. If you remove the scroll-behavior: smooth, the screenshot is taken correctly.

<html style="scroll-behavior: smooth;">
<body>
  <div style="height: 100px;"></div>
  <nav style="height: 60px; background-color: red;"></nav>
  <div style="height: 1000px;"></div>
</body>
</html>
it('works', () => {
  cy.visit('index.html');
  cy.get('nav').screenshot()
})

The screenshot should take a screenshot of the element with background-color: red, but it takes a screenshot slightly below it.

Screenshot is completely white bg:

works (56)

Workaround

Disabled scroll-behavior: smooth in tests when taking screenshots as explained in these comments:

Workito commented 4 years ago

Thank you! This fix the issue. 👍

jennifer-shehane commented 4 years ago

@Workito I'm reopening this issue as this is a bug in Cypress. I've only given a temporary workaround but this should not be required to get the screenshots properly working.

azaeng04 commented 4 years ago

@jennifer-shehane any update on this since? I am seeing screenshots being chopped off when executing them in Chromium browser.

lukesdm commented 3 years ago

+1. Perhaps consider adding this issue and workaround to Notes here? https://docs.cypress.io/api/commands/screenshot

virginiacini commented 2 years ago

We are experiencing this scroll-behavior issue, running cypress 8.5.0

cypress-app-bot commented 1 year ago

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.

cypress-app-bot commented 1 year ago

This issue has been closed due to inactivity.