Search code examples
javascripttestingcypress

How to click Copy icon selecting by condition of their Name column


How do click the Copy icon, below I am sharing the HTML code.

Scenario - Click on Copy icon where Name = komal (below is the HTML Code)enter image description here

Cypress code which I used to achieve this scenario - but with no luck.

enter image description here

After execution of this code, the following is the result

enter image description here


Solution

  • It's because the first 10 rows do not contain the word "komal".

    cy.get('td').contains('komal') only succeeds for the row that has that word but fails for all the rest.

    So iterating is a bad strategy here, and totally unnecessary. You can just specify the row directly in the query

    cy.get('table').first().within(() => {
      cy.get('tr').contains('komal')            // gives you <td>
        .parent('tr')
        .within(() => {
          cy.get('td').first().within(() => {
            cy.get('button').first().click()
          })
      })
    })
    

    Slightly better

    cy.get('table').first().within(() => {
      cy.contains('tr', 'komal')                // gives you <tr>
        .within(() => {
          cy.get('td').first().within(() => {
            cy.get('button').first().click()
          })
      })
    })
    

    or using find instead of within/get (equivalent but shorter)

    cy.get('table').first()
      .contains('tr', 'komal')
      .find('td').first()
      .find('button').first().click()