cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.73k stars 3.16k forks source link

.clear() isn't really an alias for .type('{selectall}{backspace}') #8447

Closed gustaferiksson closed 3 years ago

gustaferiksson commented 4 years ago

The .clear() command isn't an alias to .type('{selectall}{backspace}') since it doesn't behave the same. Since cypress 3.5 it's possible to type in any DOM element and if a typeable element gains focus during .type() then cypress continues in that one. So if you .get() a parent element of a typeable one, then .clear() should work because it's supposed to be an alias to .type('{selectall}{backspace}') as stated in the docs.

cy.clear() failed because it requires a valid clearable element.

The element cleared was:

  > <div id="search-input-wrap" class="on">...</div>

A clearable element matches one of the following selectors:
  'a[href]'
  'area[href]'
  'input'
  'select'
  'textarea'
  'button'
  'iframe'
  '[tabindex]'
  '[contenteditable]'

Current behavior:

.clear() doesn't have the same behavior as .type('{selectall}{backspace}')

Desired behavior:

.clear() to behave exactly like .type('{selectall}{backspace}')

Test code to reproduce

  // Passes
  it('test .type({selectall}{backspace})', () => {
    cy.visit('https://docs.cypress.io/api/commands/clear.html#Syntax')

    cy.get('#search-input-wrap').type('clear this')

    cy.get('#search-input-wrap').type('{selectall}{backspace}').type('test')
  })

  // Fails
  it('test .clear()', () => {
    cy.visit('https://docs.cypress.io/api/commands/clear.html#Syntax')

    cy.get('#search-input-wrap').type('clear this')

    cy.get('#search-input-wrap').clear().type('test')
  })

Versions

Tested on cypress 5.0 but most likely every version since 3.5

sainthkh commented 3 years ago

Confirmed that the given case shows the bug.

cypress-bot[bot] commented 3 years ago

The code for this is done in cypress-io/cypress#14498, but has yet to be released. We'll update this issue and reference the changelog when it's released.

cypress-bot[bot] commented 3 years ago

Released in 6.7.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v6.7.0, please open a new issue.