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

WebElement.clear() Does Not Update Model #301

Closed
willread opened this issue Nov 26, 2013 · 26 comments
Closed

WebElement.clear() Does Not Update Model #301

willread opened this issue Nov 26, 2013 · 26 comments
Milestone

Comments

@willread
Copy link

Calling WebElement.clear() on an input element does not appear to update the associated model.

View:

<input ng-model="foo">
<div>{{foo}}</div>

Spec:

it("should update the model", function(){
    var input = element(by.css("input"));
    input.sendKeys("bar");
    input.clear();
    expect(element(by.css("div")).getInnerHtml()).toBe(""); // Fails, because it's still "bar"
});
@juliemr
Copy link
Member

juliemr commented Nov 26, 2013

Good catch. FYI as soon as you send further keys, the model updates.

@juliemr
Copy link
Member

juliemr commented Dec 20, 2013

Not sure what to do about this one, whatever the driver does to clear is not sending any trigger for Angular to $digest. Not seeing it as a huge issue so punting to post 1.0.

@nilpath
Copy link

nilpath commented Jan 16, 2014

Hi!

Is there any workaround that could be used until it is fixed?

I've tried to use sendKeys() with an empty string but it did not work.

Edit:
Solved it by sending a String with a single whitespace. I guess I might work because Angular trims the input.

@philiphendry
Copy link

I have this exact same issue. For example, I'm testing my login form validation and sending a clear() to the password field in order to confirm the 'Password required error' message appears. I've tried calling clear() followed by sendKeys('') with an empty string but that doesn't trigger the model binding. Is there any workaround I've missed?

@wlingke
Copy link
Contributor

wlingke commented Jan 17, 2014

I had a similar issue with a directive I was using and sendKeys not updating the model. This solution worked for me though I am not sure if it will work for you all:

field.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a"));
field.sendKeys(protractor.Key.BACK_SPACE);
field.clear();

@philiphendry
Copy link

Genius! That works a treat. Thank you!

@wlingke
Copy link
Contributor

wlingke commented Jan 17, 2014

no problem. glad it worked for you!

@juliemr
Copy link
Member

juliemr commented Jan 31, 2014

@wlingke Just used that - good trick!

@wlingke
Copy link
Contributor

wlingke commented Jan 31, 2014

glad to be of help :)

@ghristov
Copy link

ghristov commented Mar 3, 2014

Hello there guys,

Hm i have probably a little bit different problem. When i click the button the function that is executed must clear the model object (the functionality is unit tested so it's working).
When i use protractor, fill the form , click the button , there is no reason for the model not to be updated but still when i assert the values they are what i have filled out. So if i'm doing e2e testing it's not really acceptable to clear the form with these hacks.

If there is something wrong that i'm doing please tell me.
Best Regards,
George

@juliemr
Copy link
Member

juliemr commented Mar 27, 2014

I'm no longer seeing this issue, and have pushed a test verifying the correct behavior. Please open a new issue if it's still occurring for you!

kbaltrinic pushed a commit to kbaltrinic/protractor that referenced this issue May 2, 2014
@satieh
Copy link

satieh commented Apr 20, 2015

@wlingke thank you,

you are genius

@EugeneSnihovsky
Copy link

@wlingke thanks from 2017. I still have this issue on angular 2.

@kenjiqq
Copy link

kenjiqq commented Mar 16, 2017

@EugeneSnihovsky Are you having problems with clear in a pure angular 2 application? I am having issues in a hybrid app with validation not triggering when using clear but wasn't sure if i have broken something or if it was related to hybrid application.

@vlio20
Copy link

vlio20 commented Apr 11, 2017

me too! on ng2

@martinstark
Copy link

martinstark commented Aug 24, 2017

Still having this issue in 2017. On an input containing 'abc', .clear() followed by .sendKeys('d') on the empty input field and results in 'abcd' in angular's model using reactive forms.

@wlingke's solution works only as long as you don't try to run it on an operating system where CTRL is not set to the modifier key. It's probably safer to run protractor.Key.BACK_SPACE * length of input value for now.

@tiboprea
Copy link

tiboprea commented Feb 7, 2018

@wlingke thanks from 2018, lol. Still having this problem on Angular 5 when I try doing:

field.clear() // works
field.sendKeys() //works
field.clear() // doesn't update my reactive form

@evanjmg
Copy link

evanjmg commented Jun 28, 2018

Ok I've had to resort to a more complex workaround, but it's a lot more reliable. I've had no false test failures with this.


export const visibilityOf = (el: any) =>
  browser.wait(ExpectedConditions.visibilityOf(el) as any, 15000, 'Element taking too long to appear in the DOM')
const mapSerial = (fn: Function, list: any[]) => list.map(x => fn(x)).reduce((p, next) => p.then(next), Promise.resolve())

const clearInput = (name: string) => {
  const el = element(by.css('input[name=' + name + ']'))
  return visibilityOf(el)
   .then(() => el.getAttribute('value'))
   .then((text) => {
      const keysArray = text.replace(/ /g, '').split('')
      keysArray.push('')
      return mapSerial((key: string) => setTimeout(() => el.sendKeys(Key.BACK_SPACE), 0), keysArray)
    })
   .then(() => browser.wait(() => el.getAttribute('value').then(text => text.length === 0), 12000))
   .then(() => el)
}

@maximelafarie
Copy link

I tried your solution @evanjmg and I'm still stuck with the focused input. Not cleared, no new content append.

@sebastianhaas
Copy link
Contributor

I can confirm this is still an issue with Angular 7.2.3 and Protractor 5.4.0 on 73.0.3683.103.

@ghost
Copy link

ghost commented Apr 24, 2019

@wlingke thanks much, your solution worked for me too.

@guillermosnipe
Copy link

guillermosnipe commented May 7, 2019

Hi @juliemr I can confirm, that .clear() is still not working fine. I'm testing a form and the only way of making the test to work is by doing this:

it('should run validations', async function () {
  await password.sendKeys('a');  
  await password.sendKeys(protractor.Key.BACK_SPACE);  
  await username.click()  // this line triggers a blur event on the password field
  expect(await passwordError.getText()).toMatch('Password is required');
});

clear() makes my test to fail by updating the model in the wrong way.

@guillermosnipe
Copy link

guillermosnipe commented May 7, 2019

I had a similar issue with a directive I was using and sendKeys not updating the model. This solution worked for me though I am not sure if it will work for you all:

field.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a"));
field.sendKeys(protractor.Key.BACK_SPACE);
field.clear();

@wlingke Thanks a lot! 2019 and this is still the way to go :)

@vsravuri
Copy link

vsravuri commented May 7, 2019

I had a similar issue recently. The workaround is to select all the existing text, use backspace to clear the text

@BalaSudheer
Copy link

Guys.. If the above solutions does not work for you, then try below. It works..
send empty string with single space.
field.sendKeys(' ');

@Kungfoowiz
Copy link

Noticed this problem when we tried to test some validation. It all works manually but using setKeys and clear doesn't seem to affect Angular to mark the input as touched or dirty.

I wonder if there validation component we are using is loaded too slowly and isn't actually loaded by the time the test has filled out the form, it's only 2 inputs in my case. I'll run a delay test tomorrow and see if that fixes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests