Filipm64
Filipm64

Reputation: 47

If table contains something then do something

How can I code: If table contains some text then do something. I tried it with contains but that throws me error if table does not contains.

if(cy.get(tableCode).contains('td', value)){
        cy.get(tableCode).contains('td', value).click()
    }else{
        cy.reload()
    }

Thanks for your time.

Upvotes: 0

Views: 338

Answers (2)

user15239601
user15239601

Reputation:

The proper way to us jQuery :contains() would be

cy.get(tableCode).then($table => {
  const $cell = $table.find('td:contains(value)')
  if ($cell.length) {
    $cell.click()
  } else {
    cy.reload()
  }
})

Upvotes: 3

Alapan Das
Alapan Das

Reputation: 18624

You can use the Jquery length method to check if the element is present in the table or not -

cy.get('tableCode').then(($ele) => {
    if ($ele.find('td').contains('value').length > 0) {
        //Element found
        cy.wrap($ele).find('td').contains('value').click()
    }
    else {
        //Element not found
        cy.reload()
    }
})

Upvotes: 1

Related Questions