Search code examples
testingcypress

How to differ double entries in a table in a test with cypress?


How to find the right value to a title if there is a title mentioned more often than once in a table in a test with cypress? How to differ double entries?

enter image description here

Table:

<table border="1">
<tr>
    <td>fix headline 1</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr>
    <td>title 1</td>
    <td>subtitle 1</td>
    <td>value 1</td>
</tr>
<tr>
    <td>fix headline 2</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr>
    <td>title 2</td>
    <td>subtitle 2</td>
    <td>value 2</td>
</tr>
<tr>
    <td>title 3</td>
    <td>subtitle 3</td>
    <td>value 3</td>
</tr>
<tr>
    <td>fix headline 3</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr>
    <td>title 1</td>
    <td>&nbsp;</td>
    <td>value 4</td>
</tr>
<tr>
    <td>title 4</td>
    <td>subtitle 4</td>
    <td>value 5</td>
</tr>
<tr>
    <td>fix headline 4</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
</tr>
<tr>
    <td>title 5</td>
    <td>&nbsp;</td>
    <td>value 6</td>
</tr>

I want to control / verify if "value 1" is correct and also if "value 3" is correct. The table is dynamic, so there can be different amounts of rows per category. The headlines are not changing, so the categories are fix by name.

In my function I can not differ between "title 1" in 2nd row and "title 1" in 7th row.

Is there a chance to differ by the "fix headlines"? Something like start searching for "title 1" after "headline 3" and verify the "value x"?

CYPRESS CODE START

cy.get('.table1 > tbody > tr:nth-child(2) > td:nth-child(1)').each(($e1, index, $list) => {
        var titeltext=$e1.text()
        if(titeltext.includes('title 1')) {
            cy.get('.table1 > tbody > tr:nth-child(2) > td:nth-child(3)').eq(index).then(function(aValue) {
                var theValue = aValue.text()
                expect (theValue.trim()).to.equal('value1')
            })
        }
    })

CYPRESS CODE END

With what I have I can not differ between the rows, it simply picks the first row with "title 1" in the routine.

With what I have I can find the row with unique "title 3" for instance but not a double entry like with "title 1" in two different categories.

I need the "title 1" or "title n" to a specific "headline" (category) and read the matching "value".

Is it possible? Any idea?

Thank you very much.


Solution

  • To check two columns text values match what you need, use the :contains() selector

    cy.get('table tbody tr:contains("title 1"):contains("value 1")')
      .then($tr => {
    
        // Only one row has both text values
        expect($tr.length).to.eq(1)
    
        // Now confirm it's the second row
        cy.get('table tbody tr:nth-child(2)').should($row2 => {
          expect($tr[0]).to.eq($row2[0])                        
        })
      })
    

    Same thing with different Cypress selectors

    cy.contains('table tbody tr', 'title 1')
      .should('contain', 'value 1')            // filter + assert
      .and('have.length', 1)                   // check only one
      .filter(':is(:nth-child(2))')            // another check, is 2nd row