cy.type('') should be treated as no-op instead of throwing an error
See original GitHub issueCurrent behavior:
Calling cy.type
with the empty string throws the error
CypressError: cy.type() cannot accept an empty String. You need to actually type something.
I saw https://github.com/cypress-io/cypress/issues/3587 that was closed, but the request there seemed to imply that passing the empty string to cy.type()
should implicitly clear the input.
Instead, I propose that cy.type('')
should simply be a no-op. The rationale is that it sometimes makes sense to include cy.type()
in a chain of commands even when the input could be the empty string. In these situations, instead of being able to include the cy.type()
even when “typing” the empty string would be a no-op, we have to special-case the empty string to prevent the error above.
For example, I would like the ability to write a helper function for testing many different inputs to a field with the following pattern:
testInput(inputValue) {
cy.get(<inputField>)
.clear()
.type(inputValue);
}
But instead, I have to do the following, just to avoid passing the empty string to cy.type()
. In this example, I want to run the rest of the commands in the chain, but I can’t chain cy.type()
due to the current behavior:
testInput(inputValue) {
cy.get(<inputField>)
.clear()
.then(e => { if (inputValue !== '') cy.wrap(e).type(inputValue) });
}
Desired behavior:
Calling cy.type('')
should be a no-op, and return the same subject it was called on. It should behave as if the customer literally typed nothing.
Test code to reproduce
describe("type", () => {
testSearch(inputValue) {
cy.get('input')
.clear()
.type(inputValue)
.type('{Enter}');
}
before(() => {
cy.visit('https://www.google.com');
});
it('Searches for documentation on cy.type', () => {
testSearch('cy.type');
});
it('Tries an empty search', () => {
testSearch(''); // Currently explodes
});
Versions
Cypress 3.8.1
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:8 (3 by maintainers)
I have the same need. It would be nice if it could be activated from options
We have the same need in the test setup for our app and used your proposed workaround. But personally, I would also prefer to have this as an option in Cypress directly rather than overwriting the command.