How to handle responsive DOM changes #392
Replies: 5 comments 5 replies
-
Hey @jarretmoses, thanks for the issue! I suspect something else is going on. It sounds like your apps DOM changes between widths? The way Percy works is by capturing the DOM of the page (and its state) when If your applications DOM changes when the browser is resized, this current method doesn't work. We render that same DOM at different widths with JS disabled (to prevent JS from altering any captured state). This method assumes the DOM stays the same and you're using CSS media queries entirely to alter the page. To fix this you'll have to resize the test browser before calling // A helper to make this easy, feel free to edit
Cypress.Commands.add('percyResponsiveSnapshot', (name, width, options = {}) => {
delete options.widths // we never want to use those in this helper
cy
// https://docs.cypress.io/api/commands/viewport.html#Syntax
.viewport(width)
.percySnapshot(`${name} - ${width}`, { widths: [width], ...options })
// Set back the orignal width if you'd like
//.viewport()
});
// usage:
cy.percyResponsiveSnapshot('Homepage', 320) |
Beta Was this translation helpful? Give feedback.
-
@Robdel12 thanks a lot for the response. You are exactly correct and what you suggest makes a lot of sense and I will give it a shot. |
Beta Was this translation helpful? Give feedback.
-
Hey, just wanted to say thanks for the answer, it pinpointed my issue perfectly. |
Beta Was this translation helpful? Give feedback.
-
I tried this on 4.11.0 (cypress) + 2.3.1 (@percy/cypress) and ran into an issue where the cy.viewport command expects either a width and height integer, or a preset string like 'macbook-13' while puppeteer, which is used by percy still expects a width integer. So the above did not work for me. I ended up having to pass the preset string to the cy.viewport command and an integer to the percySnapshot command. |
Beta Was this translation helpful? Give feedback.
-
I have been playing around with this library and I noticed something querky in terms of my screen shots. It seems when I set the
widths
option withincy.percySnapshot
the screenshot is taken during the resize of the screen which causes layout issues. By this I mean the screenshot is happening while the layout is still repositioning itself based on the response styles. Is it possible to delay the snapshot a little bit after the width has been set?Packages:
@percy/cypress: 2.3.1
cypress: 4.3.0
Beta Was this translation helpful? Give feedback.
All reactions