1

I have protractor 2.2.0.

The clear() function does not work on input of type number. I have tried most of the solutions given here. Please help me. Below is my code:

element(by.model('lineup.voltage_limit')).click().clear().sendKeys(value);

I have tried to even resolve the promise from clear() like below

element(by.model('lineup.voltage_limit')).click().clear().then(function(){
   element(by.model('lineup.voltage_limit')).sendKeys(value); 
});

clear() just does not seem to work! It just keeps on appending to the text in the input box.

Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
MonaS
  • 7
  • 1
  • 1
    can you try remove click()? element(by.model('lineup.voltage_limit')).clear().then(function(){ element(by.model('lineup.voltage_limit')).sendKeys(value); }); – Sergey Teplyakov Sep 01 '15 at 08:58
  • I have tried even that but it's still getting appended. – MonaS Sep 01 '15 at 10:36
  • 1
    Can you try using `sendKeys(protractor.Key.BACK_SPACE)` and test if it is working instead? – giri-sh Sep 01 '15 at 11:23
  • I have already tried this also but still getting the same issue. – MonaS Sep 01 '15 at 12:50
  • Can you provide the HTML code of the input you are trying to clear? Or, even better would be to make the example reproducible for us. Thanks. – alecxe Sep 01 '15 at 14:01
  • browser.executeScript("document.getElementById('voltage').value = '';"); This code does not clear the text it inserts 0 instead. The input box is of type = 'number' – MonaS Sep 02 '15 at 10:49

2 Answers2

1

I was not able to reproduce the problem using this demo page. Here is the test I've used.

I suspect your input is somewhat special and has to always have a value. What we can do is to pre-select the existing text with CTRL/COMMAND + A so that once you start sending keys, the existing text is replaced by newly inserted:

var elm = element(by.model('lineup.voltage_limit'));
elm.click().sendKeys(protractor.Key.chord(browser.controlKey, "a"));

elm.sendKeys(value);

where the browser.controlKey is a way to handle CTRL vs COMMAND on different platforms.


As a side note, I would also upgrade Protractor to the latest (currently 3.0) version which would come with new webdrivers that might handle your problem better.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

I was actually able to get mine to work by calling clear twice like this

numInput.clear().clear().sendKeys('7');

Not sure if this will work for everyone but it worked for me. alexce's answer also looks like a good one.

edit: this seems to work only some of the time. i don't know why, maybe my inputs are just weird

cocoa
  • 3,806
  • 7
  • 29
  • 56