1

I have a simple table e.g.:

<table>
<tr>
<td>name 1</td>
<td>name 2</td>
<tr>
<tr>
<td>name 3</td>
<td>name 4</td>
<tr>
</table>

With cypress I want to verify, that name 1 and name 2 are not present in the table (but only if they are next to each). If name 1 is alone in any td, then it is ok, so only if name 1 and name 2 are in one table row.

I tried to iterate through table via:

verifyTable(child, parent) {
    cy.get("table tr").each(($el, index, $list) => {

          cy.get("tr").eq(index).find("td:nth(0)").should('not.contain',child);
          cy.get("tr").eq(index).find("td:nth(1)").should('not.contain',parent);
    
        })
}

This is working fine, but if it finds child in first column, it fails and doesn't check the next column. The same if the element doesn't exists in first column but exists only in second column, it fails.

It should fail only if child and parent exists next to each other. I am not sure how to connect both conditions.

Even if I used chaning with AND, if it finds the first element, it fails:

cy.get("tr").eq(index).should('not.contain',child).and('not.contain',parent);

1 Answer 1

2

You can substitute jQuery expressions when checking the cells, they won't fail the test if not found.

Then test the combination - I think I have the right logic, but you may need to adjust if I misunderstood.

verifyTable(child, parent) {
  cy.get("table tr").each(($tr, index, $list) => {

    const childExists = $tr.find(`td:nth(0):contains(${child})`).length;
    const parentExists = $tr.find(`td:nth(1):contains(${parent})`).length;

    expect(!childExists || !parentExists).to.eq(true)

    // or this is the same, but may be closer to your specified requirement
    expect(!(childExists && parentExists)).to.eq(true)

  })
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much, I missed somehow that I can assign the commands to const. I just replaced contains(${child}) to contains('+child+') and this way it seems working fine. Thank you!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.