yandex-qatools/htmlelements

Form.fill() doesn't clear text inputs before calling sendKeys()

alkedr opened this issue · 5 comments

If input with type 'text' or 'password' already has value Form.fill() will append to it, not replace.

Form.java, lines 71-73

if (inputType == null || inputType.equals(TEXT_INPUT_TYPE) || inputType.equals(PASSWORD_INPUT_TYPE)) {
    // add element.clear(); here
    element.sendKeys(value.toString());
}

Yes, because the sendKeys only types keys and doesn't clear input value.

I'd suggest taking an approach of sending N number of backspace symbols (N is number of symbols currently in that field) and then actual new string. Beware that you need to send both BACKSPACE (for Windows/Linux) and DELETE (for Mac) symbols for that to work.

Working example in PHP: https://github.com/Behat/MinkSelenium2Driver/blob/master/src/Behat/Mink/Driver/Selenium2Driver.php#L667

That's a nice trick, I didn't think of that.
But isn't this a bug and shouldn't it be fixed by adding element.clear(); before sendKeys()?

If you call .clear() it will clear the element value, however it will also trigger onchange event in JavaScript. So for setting field value you'll get 2 onchange JavaScript event fired (one from clearing previous value and one from setting new one).

Since real user will only get onchange event when he leaves the field, then the backspace/delete solution seems to be more appropriate solution.

Trick with backspaces is even nicer than I thought! :)
Will it be implemented?

Trick with backspaces is even nicer than I thought! :) Will it be implemented?

It's up to you I guess. PR are welcome.

By the way in my PHP implementation of HtmlElements I've decided to remove that IF statement by inputType to allow HTML5 elements like <input type="email"/> and other to be fillable as well: https://github.com/qa-tools/qa-tools/blob/master/library/QATools/QATools/HtmlElements/Element/Form.php#L119