https://cypress.io logo
Hi everyone, I feel like this falls under best pra...
# best-practices
l
Hi everyone, I feel like this falls under best practices: I'm trying to find a solid way to assert that our dashboard has indeed loaded after signUp. I want to make sure enough elements are present/visible so that the test can start interacting with buttons, etc. I'll post the code I currently have in a thread.
Copy code
let count = 0;

cy.get(".bg-blue-300 > .bg-white")
  .should(() => {
    count += 1;
  })
  .and("contain", "Welcome")
  .then(() => {
    cy.log(`retried **${count}** times`);
  })
  .then(() => {
    cy.get(".overflow-y-auto > .px-8")
      .should(() => {
        count += 1;
      })
      .and("contain", "Step 1")
      .then(() => {
        cy.log(`retried **${count}** times`);
      })
      .then(() => {
        cy.get(":nth-child(2) > .bg-white")
          .should(() => {
            count += 1;
          })
          .and("contain", "Publications")
          .then(() => {
            cy.log(`retried **${count}** times`);
          });
      });
  });
I got the idea from https://glebbahmutov.com/cypress-examples/9.5.0/recipes/retry-ability.html#multiple-assertions halfway down on Count Retries. Is there a more concise way to do this? A function? I tried that, but I swear Cypress just doesn't like to do things like that. Maybe I'm mistaken?
Here's a pseudo-code version of what I'm after
Copy code
let count = 0

const retry = (element) => {
  if (tries >= 300) {
    return false
  }

  if (element) {
    return true
  }

  wait(500)

  count += 1

  return retry(element)
}

const isTrueOrFalse = retry(domElement)

// true || false
b
You might be overcomplicating things waiting on your dashboard to fully load. The retryability will work with a simple .should appended to a .get. Are there any api calls you can wait on for your element(s) to load?
l
@bitter-fountain-36713 Thanks for the response! I've actually tried that. This is the code we've been rolling with atm.
Copy code
cy.get(".h-full", { timeout: 10000 }) // TODO: update `datatest-id` to dashboard or main menu
.should((card) => {
    expect(card).to.contain("Welcome");
    expect(card).to.contain("Create new");
    expect(card).to.contain("Publication");
  });
Currently it fails about 1/20, but almost always when it's a cold API start. It's just waiting for the dashboard to load, then checks if a few elements exist. It feels like 10 seconds should be more than enough, but sometimes it isn't. It just needs to run twice though. It will always work on the second retry.
The problem is I need to put this command into a
before
block, because it's a signup+login. But I can't trigger retries from a
before
.
I suppose I could add an API call to wait on as well. If they were coupled maybe that would be better. I know I'm not the first one with this problem.
2 Views