question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to ensure all react components are loaded before cy.screenshot()

See original GitHub issue

Current behavior:

When trying to get screenshots of pages, even after waiting for all xhr requests and checking elements exist, cy.screenshot() will capture the page in an intermediate loading state where some things haven’t rendered yet.

I do not want to use cy.wait(1000) as it is hacky and has caused my problems further down the test chain, such as random timeouts on screenshots.

Desired behavior:

Is there any way to check a component has rendered and is visible before taking the screenshot?

Steps to reproduce: (app code and test code)

Here is an example where it works when I add a wait but not without:

cy.fixture(QA_FIXTURE).as('fixture');
  cy.server();
  cy.route('POST', '/v1.0/blobs/sign-url/').as('signUrl');

  cy.selectLatestActivity('/activity/');

  cy.get('@fixture').then(fixture => {
    if (fixture.BUILD_PREFIX === 'blah') {
      cy.log(fixture.BUILD_PREFIX);
      cy.wait('@signUrl').then(xhrs => {
        cy.wrap(xhrs.status).should('eq', 200);
      });
    }
  });
  cy.wait(2000);
  cy.screenshot();
  cy.get('[data-cy="close_btn"]').click();

the signurl fucntion is the last request to occur, and even if use something like:

cy.get('[data-cy="close_btn"]').then(() => {cy.screenshot();});

To ensure the button is there after the request, it will still be a race to whether the component has rendered and the screenshot saved.

Versions

3.4.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
geebrucommented, Jan 30, 2020

@webnexusmobile’s solution is similar to what I’ve had to implement in some of the slower areas of our application. We’ll do things like:

cy.get('element we know loads "late"').should('not.be.empty'); or cy.get('element that will get focus when loading is done').should('have.focus')

In the rare case where that’s still not quite enough time (for transitions to finish firing or something like that) you could always increase the timeout of those checks instead of using .wait(). That way you can increase how long Cypress will wait for the thing to appear or exist without forcing it to wait the full amount of time.

cy.get('#dialogClose').should('have.focus', { timeout: 5000 });

2reactions
webnexusmobilecommented, Oct 28, 2019

Did you ever try something like

cy.get('some-lazy-element').should('be.visible');
cy.screenshot();

or

cy.get('some-lazy-element').contains('stuff from xhr response that should be rendered')
cy.screenshot();

? cy.get() alone doesn’t care for the visibility of an element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress Screenshots for React Components
A tutorial about replacing Jest Snapshots With Cypress Screenshots for Testing React Components.
Read more >
React Quickstart - Cypress Documentation
Welcome! This tutorial will walk you through creating a React app and using Cypress Component Testing to test it. We assume you are...
Read more >
Cypress: Wait for unpredictable component to render from an ...
The recommendation from cypress is to make your testing deterministic. Meaning, figure out how to ensure your product is always in a known ......
Read more >
How to Test React Components: the Complete Guide
The lines of code that have changed is known as the diff. Here is our basic component we are snapshot testing: import React...
Read more >
Visual testing for React components using open source tools
Note: make sure to inspect every changed snapshot before committing, since all snapshots can be updated during the above command. Continuous ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found