Oleh Dymych
Oleh Dymych

Reputation: 325

If, if else, else doesn't work in cypress

I have the following code, but block with else if doesn't work, how can I change this code, to make the if else block work?

cy.get('#cards-list').then((list) => {
    const valueFormList = list.find(`a:contains('${nameForSite}')`)
    const nextArrow = list.find(`li.pagination-next`)
    if (valueFormList.length > 0 && nextArrow.length > 0){
        cy.get(`a:contains('${nameForSite}')`).as('card')
    } else if (valueFormList.length < 0 && nextArrow.length > 0) {
        cy.get('li.pagination-next').click()
        cy.wait(200)
        cy.findCardOnFacebook(nameForSite)
    } else {
        cy.get('h4.learning-opportunities-title').should('contain', 'Learning')
    }
})

Upvotes: 0

Views: 361

Answers (1)

Alapan Das
Alapan Das

Reputation: 18576

The error is because of the condition valueFormList.length < 0. The length can never be less than 0. Either it can be zero or more than zero.

cy.get('#cards-list').then((list) => {
  const valueFormList = list.find(`a:contains('${nameForSite}')`)
  const nextArrow = list.find(`li.pagination-next`)

  if (valueFormList.length == 0 && nextArrow.length > 0) {
    cy.get('li.pagination-next').click()
    cy.wait(200)
    cy.findCardOnFacebook(nameForSite)
  } else if (valueFormList.length > 0 && nextArrow.length > 0) {
    cy.get(`a:contains('${nameForSite}')`).as('card')
  } else {
    cy.get('h4.learning-opportunities-title').should('contain', 'Learning')
  }
})

Upvotes: 2

Related Questions