Let's say I have a variable username.
Now, my chainable function wants to check if the username is empty or not.
Before:
if(username !== "") {
cy.get('#username').type(username)
}
After (Expectation):
cy.get('#username').type(username).ifNotEmpty() //ifNotEmpty will be my chainable func
So my question is Is this even possible? If yes, then how?
CodePudding user response:
You can add a custom Cypress command to achieve this. These are called child commands
. More information here.
Cypress.Commands.add('ifNotEmpty', { prevSubject: true }, (subject) => {
// code
})
CodePudding user response:
You probably just want to make a .type()
variation
Cypress.Commands.add('typeIfNotEmpty', { prevSubject: true }, (subject, textToType) => {
if (textToType) {
cy.wrap(subject).type(textToType)
}
return subject // allow further chaining
})
cy.get('#username')
.typeIfNotEmpty(username)
.should(...) // some assertion on '#username' element