CypressError: cy.type() cannot accept an empty String. You need to actually type something.
An empty string is a valid an often necessary test on an input field. The type method should support this.
cy.get('input element').type('')
Cypress: 3.1.5
MacOs Mojave 10.14.3
Chrome 72
Hey @DarthOpto, what behavior are you expecting within an input when passing an empty string into cy.type()? What behavior are you trying to test in the input with this? Wouldn't this essentially be Cypress typing nothing?
@jennifer-shehane I am basically trying to test that the sign in button is disabled when I leave the email field blank. I think I have a way around this. I am going to close this.
Can we re-open this?
I'm trying to write dynamic tests that cleanup after themselves.
Take the value of input A and store it for later.
Type something, click save and assert on reload that it's there.
Now in the cleanup phase I want to write back whatever value was there before. This is where cy.type is breaking my tests if the original value was an empty string.
I have to add an if statement that basically checks to see if the original value was an empty string, and if it was, then don't call cy.type. This isn't ideal, cy.type feels like any string value should be a valid argument - even an empty string.
@jennifer-shehane 鈽濓笍
If you want to set the value of an input as an empty string, you can do this programatically. A person cannot type an empty string, so this will not be supported in .type().
To do this programmatically:
```js
cy.get('input').invoke('val', '')
This is unfortunate--it would be nice if cy.type('') was treated as a no-op instead of throwing an error. The current behavior means that I can't have a helper function for testing many different inputs to a field with the following pattern:
testInput(inputValue) {
cy.get(<inputField>)
.clear()
.type(inputValue);
}
Instead I have to do the following, which feels unnecessarily gross:
testInput(inputValue) {
cy.get(<inputField>)
.clear()
.then(e => { if (inputValue !== '') cy.wrap(e).type(inputValue) });
}
@jennifer-shehane I am basically trying to test that the sign in button is disabled when I leave the email field blank. I think I have a way around this. I am going to close this.
You can try .clear() to achieve the same result.
Most helpful comment
Can we re-open this?
I'm trying to write dynamic tests that cleanup after themselves.
Take the value of input A and store it for later.
Type something, click save and assert on reload that it's there.
Now in the cleanup phase I want to write back whatever value was there before. This is where cy.type is breaking my tests if the original value was an empty string.
I have to add an if statement that basically checks to see if the original value was an empty string, and if it was, then don't call cy.type. This isn't ideal, cy.type feels like any string value should be a valid argument - even an empty string.