Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
How to make a chainable command in cypress?

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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
about 4 years ago · Juan Pablo Isaza Report

0

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
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!