-1
  cy.wait(20 * 1000);

  cy.getAllByTestId('test-field-one')
    .first()
    .within(() => cy.getByTestId('test-field-two'))
    .should('contain', 'test');

I'm currently using the above code, but I don't like the wait. I'd much rather prefer a timeout value. Does anyone know if this is possible in cypress?

basickarl
  • 37,187
  • 64
  • 214
  • 335
  • Cypress offers timeout options at the global level and the element level. Trust [this](https://docs.cypress.io/guides/core-concepts/introduction-to-cypress#Applying-Timeouts) helps? – Dhamo Aug 26 '21 at 11:06
  • 2
    @Dhamo I tried `cy.getAllByTestId('test-field-one', { timeout: 20 * 1000 })`, doesn't seem to work :( – basickarl Aug 26 '21 at 11:29
  • 2
    If you look into the examples https://testing-library.com/docs/cypress-testing-library/intro#examples the above should work. Just a wild guess, how about you just put `20000` instead of `20 * 1000`. – Alapan Das Aug 26 '21 at 12:52

1 Answers1

1

The getAll... functions can't really use a timeout.

The difference between { timeout: 20 * 1000 } and cy.wait(20 * 1000) is that timeout returns early when the condition it met.

But how many exactly is "all"?

The only way to do an "all" query is to wait the maximum time and return everything at that point in time - which is what you already do with explicit cy.wait().


Since you are using .first(), just switch to cy.getByTestId()

cy.getByTestId('test-field-one', { timeout: 20 * 1000 })
  .within(() => cy.getByTestId('test-field-two'))
  .should('contain', 'test')

Also note that

.should('contain', 'test')

is testing test-field-one not test-field-two because .within() does not pass on the inner value, it passes on the previous subject.

user16695029
  • 3,365
  • 5
  • 21