Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.clear() on an input at maxlength doesn’t work #1002

Closed
KittyGiraudel opened this issue Dec 4, 2017 · 14 comments · Fixed by #2016
Closed

.clear() on an input at maxlength doesn’t work #1002

KittyGiraudel opened this issue Dec 4, 2017 · 14 comments · Fixed by #2016
Assignees
Labels
pkg/driver This is due to an issue in the packages/driver directory type: bug
Milestone

Comments

@KittyGiraudel
Copy link
Contributor

KittyGiraudel commented Dec 4, 2017

  • Operating System: 10.12.6
  • Cypress Version: 1.1.3
  • Browser Version: n/A

Is this a Feature or Bug?

A bug.

Current behavior:

Calling .clear() on an input with a value as long as its maxlength attribute value leaves the value untouched.

Desired behavior:

Calling .clear() on an input with a value as long as its maxlength attribute value empties the field.

How to reproduce:

  1. Create an input.
  2. Set its maxlength to n.
  3. Set its value to a string of n characters.
  4. Call .clear() on it.

Test code:

cy.get('input[maxlength]').clear()

Additional Info (images, stack traces, etc)

I suspect the problem to be here:

if ml is 0 or ml > 0
## check if we should update the value
## and fire the input event
## as long as we're under maxlength
if el.value.length < ml
maybeUpdateValueAndFireInput()
else
maybeUpdateValueAndFireInput()

There is a max length, but the value is already at its max length, so it results on a no-op, when it really should clear the field.

@jennifer-shehane jennifer-shehane added pkg/driver This is due to an issue in the packages/driver directory stage: ready for work The issue is reproducible and in scope good first issue Good for newcomers type: bug labels Dec 4, 2017
@brian-mann
Copy link
Member

Yes this is a known problem.

It was first discussed here: #940 (comment)

I started working on a solution for this and the text-mask problems but have not finished.

@John758364
Copy link

Any update one this?

@verheyenkoen
Copy link
Contributor

verheyenkoen commented Feb 28, 2018

Here's a test (I put this in keyboard_spec.coffee) to prove this bug. I didn't commit this since I didn't know if this is the right place (no other tests found for .clear()) and couldn't fix the bug so I just drop it here:

    it "can clear a field with maxlength", ->
      Cypress.$('input')
        .attr('maxlength', 5)
        .val('abcde')

      cy.get('input')
        .clear()
        .should('have.value', '')

@jennifer-shehane
Copy link
Member

@verheyenkoen there are tests in the type_spec.coffee. If you can, put these tests in there and open a WIP pull request.

@verheyenkoen
Copy link
Contributor

I found there appears to be a lot more bugs related to fields with maxlength, all with the same cause. Special keystrokes like backspace, del, leftarrow, rightarrow,... all seem to be ignored. Don't know about downarrow and uparrow as those only make sense in textareas and don't know if maxlength is a thing there.

@brian-mann
Copy link
Member

Yup these have been noted / documented before in an issue somewhere. I'll dig and find them. We've been experimenting with moving to native events to bypass all of these issues - although we'll likely still give you a simulated .type() for performance reasons, so it's still important to fix.

@kapalkat
Copy link

Seems I have a similar problem. I have reported it under #2056

@kuceb
Copy link
Contributor

kuceb commented Jun 29, 2018

I'm actively working on this issue and more related to cy.type(). Fixes should be in a patch release coming up

@vedmant
Copy link

vedmant commented Jul 4, 2018

Is there a workaround how to actually clear a field with maxlength?

@kapalkat
Copy link

kapalkat commented Jul 4, 2018

So the problem (for me at least) is when the input filled in filled in with the maximum allowed characters. So what I do, I use jQuery to increase the maxlength parameter like so:
cy.get('#coalTypeDescr')
.as('coalTypeDescr')
.then(($coalTypeDescr) => {
Cypress.$($coalTypeDescr).attr('maxlength', 26)
})
cy.get('@coalTypeDescr')
.clear()
.type()
This works fine for me.

@ednailson
Copy link

I have the same problem!

@kuceb kuceb self-assigned this Jul 17, 2018
@kuceb kuceb added this to the 3.0.3 milestone Jul 17, 2018
@kuceb kuceb removed good first issue Good for newcomers stage: ready for work The issue is reproducible and in scope labels Jul 17, 2018
brian-mann pushed a commit that referenced this issue Jul 23, 2018
this grew to a large PR fixing many cy.type issues.

fix #365
fix #420
fix #586 
fix #593 
fix #596 
fix #610 
fix #651
fix #940
fix #1002 
fix #1108
fix #1171
fix #1209 
fix #1234 
fix #1366
fix #1381 
fix #1684 
fix #1686
fix #1926 
fix #2056
fix #2096 
fix #2110 
fix #2173
fix #2187
@jennifer-shehane jennifer-shehane added stage: pending release and removed stage: needs review The PR code is done & tested, needs review stage: needs investigating Someone from Cypress needs to look at this stage: in progress labels Jul 23, 2018
@c32hedge
Copy link

I've been having some flaky tests where cy.clear() sometimes fails, and the original value of the input field was the max length of the field. Looks like a recent regression? I've at least seen it in 3.7.0.

@kuceb
Copy link
Contributor

kuceb commented Dec 24, 2019

@c32hedge can you give an example of a failure, with html and test code?

@jennifer-shehane
Copy link
Member

@c32hedge Please open a new issue with a reproducible example (HTML + test code). This original issue has been addressed and we have tests around it. Likely you have another case that is very similar with a slight different.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Dec 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pkg/driver This is due to an issue in the packages/driver directory type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants