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

98
Views
.type() method in cypress when string is empty

Sometimes I want to pass empty string to .type() and I'm getting an error, for instance:

data = {
  "test1": "test",
  "test2": "",
};

and when I assigned first case it's work:

cy.get(...).type("test1")

The output:

test1

But when I'm passing the next one, just empty string:

cy.get(...).type("")

I've got an error that I can't provide empty string. How can I fix that?

This snippet of code is a part of a function so it must works for empty string as well as not empty string.

I tried something like this:

cy.get(...).type('{backspace}', variable)

it's working when string is empty but also skipping variable if variable contain characters.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

It's not possible for a user to type "", so neither can your test.

Use cy.get(...).clear() to make it empty.

about 4 years ago · Santiago Trujillo Report

0

From the comments, looks like you have already come to the solution

For example,

for (const key in data) {
  cy.get(key).type(data[key])     // fails when data[key] is empty
}

could be fixed with

for (const key in data) {
  if (data[key]) {
    cy.get(key).type(data[key])   // only type non-empty data values
  }
}
about 4 years ago · Santiago Trujillo Report

0

I found the answer on my own in other resources

let array = ['test1', '', 'test2', 'test3']

for (let item in array) {
  cy.get(...).type(`{backspace}` + `${item}`)
}

and its work, even if string is empty.

about 4 years ago · Santiago Trujillo 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!