How to write condition for locator is present in the page using Cypress

Viewed 78

I am looking for the condition in cypress to verify and click locator is present or not. If element is present go to condition and click on the element else leave and continue with the execution. Below code i have tried,

    cy.get(".paginationPanel.ng-star-inserted").then(($test)=> {
    if($test.text().includes('Show:')){
        //do something
    }else{

    }
})

Unfortunately, I am unable to success. Can anyone please help me. enter image description here enter image description here

1 Answers

That code works, what is wrong?

What do you want to click? The "Show" dropdown would be

cy.get(".paginationPanel").then(($panel) => {

  if($panel.text().includes('Show:')) {

    cy.contains('label', 'Show').prev().click()  // click the Show dropdown

  } else {
    
  }
})
 
Related