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.

cypress.get(element) fails without waiting or retrying

See original GitHub issue

Current behavior:

The cypress.get(element) function fails without waiting or retrying if the element is not visible on the page.

Desired behavior:

The cypress.get(element) function should wait and find the dom element in the page or timeout after the default timeout if the element is not visible in the page.

Steps to reproduce:

Non-working code:

describe('Starts a chat conversation', () => {
  describe('When visiting /chat ', () => {
    it('creates a new chat and the the chat bot starts sending messages', () => {
      cy.loginAs(clientWithMembership); // custom command that execute an http call

      const url = getChatUrl();
      cy.visit(url);
      cy.get('div[data-cy=chat-message-container]').should('have.length', 1); // Fails without waiting
    });
  });
});

Working code: (adding a wait before checking if the element exists)

describe('Starts a chat conversation', () => {
  describe('When visiting /chat ', () => {
    it('creates a new chat and the the chat bot starts sending messages', () => {
      cy.loginAs(clientWithMembership); // custom command that execute an http call

      const url = getChatUrl();
      cy.visit(url);
      cy.wait(3000);
      cy.get('div[data-cy=chat-message-container]').should('have.length', 1);
    });
  });
});

Versions

Cypress: 3.0.1 Mocha: 4.0.1 Browser: Chrome

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

16reactions
jennifer-shehanecommented, Jul 10, 2018

@dan19 It does appear from the information provided that Cypress attempted to retry for the duration of the timeout (4000ms), and did not find the DOM selector to have a length of 1. Could you try increasing the timeout of the actual get to see if this helps?

cy.get('div[data-cy=chat-message-container]', {timeout: 7000}).should('have.length', 1);

Also, make sure you are following some of our recommendations on waiting for you app to load before getting an element in your tests. https://on.cypress.io/using-cypress-faq#How-do-I-wait-for-my-application-to-load

7reactions
jennifer-shehanecommented, Mar 27, 2019

@mikhaildzarasovvineti If you want to purely wait for an element to be present on the page, you can increase the timeout that Cypress waits for an element to be present. cy.get('button', {timeout: 60000}.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retry-ability - Cypress Documentation
What you'll learn How Cypress retries commands and assertions When commands are retried and ... Expect fails the test without waiting for the...
Read more >
Waiting in Cypress and how to avoid it - Filip Hric
Cypress will wait for the element to appear in DOM and will retry while it can. If 4 seconds are not enough, you...
Read more >
Can I prevent Cypress cy.get from failing if no elements are ...
get () has a built-in retry of 5 seconds, whereas Cypress.$(listItemTitle) is synchronous. What happens if you cy.wait(5000) before counting ...
Read more >
3 Practices to Reduce Flakiness in Cypress Tests - Atomic Spin
and the assertion fails, Cypress will only retry the .find() command, instead of starting from the beginning of the chain and retrying .get()...
Read more >
Solving The Element Is Detached From DOM Error In Cypress
And if everything fails, if there is no observable application property to wait for - then add the cy.wait(xxx) command to wait for...
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