0

I have some front-end test written in Groovy and I need to rewrite them in Cypress. I am currently having problem with rewriting an if condition to Cypress.If the popin window is displayed click on it, otherwise do nothing.

if ($(".PDialog").displayed) {
            $(".p-submit-btn.confirm").click()
            waitFor(2) {!$(".PDialog").displayed}
}

I have tried using a then clause. The current version that I have is this one ```

cy.get('body').then($res => {
            if ($res.find('PDialog').length) {
                cy.get('.p-submit-btn.confirm').click();
            }
        });

but it is still not working as the upper one.

van Huet
  • 139
  • 8

2 Answers2

2

Use Cypress.$ for $, and .should('be.visible') for displayed

const $dialog = Cypress.$(".PDialog")

if ($dialog.length > 0) {
  // it exists
  cy.wrap($dialog).should('be.visible')  // is displayed
  cy.get('.p-submit-btn.confirm').click()
  cy.wrap($dialog).should('not.be.visible')
}

When using .should(<some assertion>) you automatically get 4 seconds timeout, so a wait is not needed here.

van Huet
  • 139
  • 8
0

In your code, you are a missing a point the class PDialog:

cy.get('body').then($res => {
   if ($res.find('.PDialog').length) {
      cy.get('.p-submit-btn.confirm').click();
      cy.get('.PDialog').should('not.exist')
   }
});
Wandrille
  • 6,267
  • 3
  • 20
  • 43