0

I want to take the last row in the third column in my dynamic table.This cell has the last email randomly created and stored in the variable storedEmail. Those are what I tried:

cy.fixture('email.json').then((fixture) => {
  const storedEmail = fixture.email;

  cy.get('table')
    .find('tr')
    .last()
    .find('td:nth-child(3)')
    .invoke('text')
    .then((cellText) => {
      expect(cellText.trim()).to.contain(storedEmail);
    });
});
});

I also tried:

cy.get('table')
  .find('tbody > tr')
  .last()
  .within(() => {
    cy.get('td:nth-child(3)')
      .invoke('text')
      .then((cellText) => {
        const trimmedText = cellText.trim();
        cy.log('Cell Value:', trimmedText);
        expect(cellText.trim()).to.contain(storedEmail);

      });
  });

But both snippets gave me the first row instead the last one.

1 Answer 1

0

Since your table is dynamic, is there a possibility the test is running before all the rows are present?

You can try asserting the number of rows before testing the content

cy.get('table')
  .find('tbody > tr')        // definitely use tbody, since thead also has child tr
  .should('have.length', 10) // test the expected rows have loaded
  .last()
  .find('td:nth-child(3)')
  ... // etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.