0

I am trying to add else if /switch case in my test , but else if - it goes to only if case, if 'if' fail it doesn't go in else if it's happen in switch case also

it('Deve cadastrar um CNPJ válido', () => {
    cy.get(':nth-child(2) > .kt-header__topbar-wrapper').click()
    cy.get('.kt-header__topbar-item.show > .dropdown-menu > .kt-notification > .kt-notification__custom').should('be.visible')

    cy.visit('/Company/Create')
    cy.contains('Registration of the Establishment').should('be.visible')

    cy.get(':nth-child(2) > .kt-option > .kt-option__control > .kt-radio > span').click()
    cy.contains('Enter the CNPJ of the establishment you want to add below').should('be.visible')

    cy.get('#searchCnpj')
      .type(faker.br.cnpj())
      .should('be.visible')

    cy.get(':nth-child(2) > .form-group > .input-group > .input-group-append > .btn')
      .click()

    cy.contains('Establishment already registered.').then(($message) => {
      if ($message.length > 0) {
        cy.log('Establishment already registered.')
        cy.get('#searchCnpj')
          .clear()
          .type(faker.br.cnpj())

        cy.get(':nth-child(2) > .form-group > .input-group > .input-group-append > .btn').click()
      } else {
        cy.contains('Do you want to register it?').then(($confirmMessage) => {
          if ($confirmMessage.length > 0) {
            cy.log('Do you want to register it?')
            cy.contains('Sim').click()
          }
        })
      }
    })
  })

The error that appears in cypress: Timed out retrying after 4000ms: Expected to find content: 'Estabelecimento já cadastrado.' but never did.

What I want is: Enter CNPJ and click Consult

If the system displays "Establishment already registered", then the cypress must change the cnpj and click on Consult again until the message "Do you want to register" appears and click on "Yes"

If the system displays "Do you want to register it" then you must click "Yes"

Fody
  • 23,754
  • 3
  • 20
  • 37

3 Answers3

3

The way to do this depends if the message elements exist or are just invisible.

Check the page, see if the message is removed, or just made invisible.

If only one message exists in the DOM

In this case use jQuery Multiple Selector to see which message

const msg1 = 'Establishment already registered'
const msg2 = 'Do you want to register it'
const multiSelector = `:contains(${msg1}), :contains(${msg2})`;

cy.get(multiSelector)         // either msg1 or msg2 will exist
  .then(($message) => {

    // Check which message
    if ($message.text().includes(msg1)) {
      ...
    } else {

    }
  })

If both messages exist in the DOM but one is invisible

In this case add the :visible selector

const msg1 = 'Establishment already registered'
const msg2 = 'Do you want to register it'
const multiSelector = `:contains(${msg1}):visible, :contains(${msg2}):visible`;

cy.get(multiSelector)         // either msg1 or msg2 will be visible
  .then(($message) => {

    // Check which message
    if ($message.text().includes(msg1)) {
      ...
    } else {

    }
  })
Fody
  • 23,754
  • 3
  • 20
  • 37
  • I used this code in my test but it still gives the error: Timed out retrying after 4000ms: Expected to find content: 'Deseja cadastrá-lo?' but never did. I use a CNPJ that exists in the database. The message "Establishment already registered" is a toast message, and a message "Do you want to register it" is a modal that it is a modal that has button for Yes or No – Gilberto Vieira Mar 22 '23 at 19:10
  • The code: ``` const msg1 = 'Establishment already registered' const msg2 = 'Do you want to register it' const multiSelector = `:contains(${msg1}):visible, :contains(${msg2}):visible`; cy.get(multiSelector) .then(($message) => { cy.log($message) if ($message.text().includes(msg1)) { cy.get('#searchCnpj') .clear() .type(faker.br.cnpj()) cy.get(':nth-child(2) > .form-group > .input-group > .input-group-append > .btn').click() } else { cy.contains(msg2).should('be.visible') } }) ``` – Gilberto Vieira Mar 22 '23 at 19:13
  • Obviously, make sure you use the correct language. It's not clear from your question if it's English or Portuguese. If the latter, try without the accent `á`. – Fody Mar 22 '23 at 19:31
  • My code is Portuguese, which I translate into English to post here. the error code is Timed out retrying after 4000ms: Expected to find content: 'Do you want to register it' but never did – Gilberto Vieira Mar 23 '23 at 12:05
  • It's probably a mistake to translate Portuguese text to English here. You can ask the question in English, but the the text may be what is causing the problem so you need to show the precise version including accents. The code is neither Portuguese nor English, it's Javacript. – Fody Mar 23 '23 at 19:07
  • 1
    Did you try without `á`? – Fody Mar 23 '23 at 19:08
0

there are several things you can try. You can try using "try/catch" and if the condition fails it will execute your "else" part for example. Another approach you could try, which I consider to be the best one, is to have 2 test cases, one for when it's a 'Establishment already registered.' and another one for when 'Do you want to register it?'. If possible the tests should never have logic associated. Let me know if you need additional assistance.

  • It is associated because I can only create an establishment when the CNPJ does not exist in the database, if it exists then the message "establishment already registered" will appear and I cannot proceed, so it is necessary to insert another CNPJ and perform the query again until the "want to register" appears – Gilberto Vieira Mar 23 '23 at 12:14
  • I would argue that the test environment must be controlled by you and not the other way around. It's not correct for the test to see if it is A or B condition and do X or Y accordingly. This will likely lead to issues down the line. Does it make sense? – SuperZezoide Mar 24 '23 at 10:06
0

In your code, cy.contains('Establishment already registered.') will be executed no matter what, which means if cypress couldn't find element containing 'Establishment already registered.' the test case will fail. As already stated in the first answer, I believe it's best to make two different test cases for 'Establishment already registered' and 'Do you want to register it?'

Miftah
  • 23
  • 6