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.
It's not possible for a user to type "", so neither can your test.
Use cy.get(...).clear() to make it empty.
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
}
}
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.