JordTheQA
JordTheQA

Reputation: 25

Cypress - Error message doesn't match code

Error shows contains() instead of expect

As you can see - the error message is about the cy.contains() command, pointing on the expect() code line. Cypress doesn't let me know what the true error is - why the test fails. My test is based on a page class which is added below. The relevant class functions -

class NewAppointment {
    
    selectSeverity(value) {
        cy.get("#severities").select(value)
    }

    chooseAppointmentDate(value) {
        cy.get("[data-handler='selectDay']").contains().click()

    }
}
export default NewAppointment

And the code section which fails -

       cy.get("#severities").should(($severitiesArray) => {
           expect($severitiesArray.get(0)).to.have.property('childElementCount', 6) 
       })
       .then(($severitiesArray) => { let optionVal = new Array()
       optionVal = $severitiesArray.children()
       let optionalValue = optionVal[Math.floor(Math.random() * optionVal.length) + 1]
       addAppointment.selectSeverity(optionalValue.text)
       })

Does anyone know why this happens and/or how to solve it?

edited - As asked - the DOM structure: enter image description here

and also the doctors section -

cy.get("#doctors").should(($doctorSelect) => {
            expect($doctorSelect.get(0)).to.have.property('childElementCount', 7)
       })
       .then(($doctorSelect) => {
        let optionArray = new Array()
        optionArray = $doctorSelect.children()
        let optionalValue = optionArray[Math.floor(Math.random() * optionArray.length) + 1]
        addAppointment.selectDoctor(optionalValue.text)
       })
        addAppointment.chooseAppointmentDate(dateValue)
        cy.get(("#schedule"), { timeout: 30000 }).should(($appointments) => { 
           expect($appointments.children()).to.have.length.of.at.least(1)
        })

Upvotes: 1

Views: 340

Answers (1)

Rosen Mihaylov
Rosen Mihaylov

Reputation: 1427

cy.contains Requires argument as string. Seems like you need to pass the argument to the ,contains() in the function chooseAppointmentDate(value)

class NewAppointment {
    
    selectSeverity(value) {
        cy.get("#severities").select(value)
    }

    chooseAppointmentDate(value) {
        cy.get("[data-handler='selectDay']").contains(value).click()

    }
}
export default NewAppointment

Upvotes: 2

Related Questions