mozilla / geckodriver

WebDriver for Firefox
https://firefox-source-docs.mozilla.org/testing/geckodriver/
Mozilla Public License 2.0
7.14k stars 1.52k forks source link

SendKeys Not Respecting Caret Location #2015

Open titusfortner opened 2 years ago

titusfortner commented 2 years ago

I suspect that focus is always called for sendKeys, and that it is always inserting the caret at the end, when it should only get called if it doesn't already have focus.

Per spec:

If element does not currently have focus, set the text insertion caret after any child content.

System

Testcase

element.sendKeys("A");
element.sendKeys(Keys.LEFT);
element.sendKeys("B");

I expect this to be BA and instead it is AB

This works:

element.sendKeys("A");
element.sendKeys("" + Keys.LEFT + "B");

But this does not:

element.sendKeys("A" + Keys.COMMAND + Keys.LEFT + Keys.NULL + "B");

This last one is probably a separate issue?

I'm going through old Selenium tests that have been guarded; looks like Chrome w/Mac still respects the navigation keys even if they aren't present on mac keyboards, so you don't need to use modifiers to jump to beginning/end of a text field, etc. I think modifier + non-typeable key + null is supposed to do the same?

Also, the behavior for text fields and divs with contenteditable="true" is different here. Looks like fix in https://bugzilla.mozilla.org/show_bug.cgi?id=1203074 was not applied to these.

Let me know if you need separate bug reports for any of this.

juliandescottes commented 2 years ago

Thanks for filing!

Per spec:

If element does not currently have focus, set the text insertion caret after any child content.

FWIW, I think this only applies to content editable, for regular inputs we fall into the otherwise branch (from https://www.w3.org/TR/webdriver/#element-send-keys)

Otherwise

  1. If element does not currently have focus, let current text length be the length of element’s API value.
  2. Set the text insertion caret using set selection range using current text length for both the start and end parameters.

The spec feels a bit confusing to me but I think step 2 here should only be done if the current element does not have focus? In any case I can't see anything in Bugzilla tracking this issue, so I filed https://bugzilla.mozilla.org/show_bug.cgi?id=1791736

element.sendKeys("A" + Keys.COMMAND + Keys.LEFT + Keys.NULL + "B");

Yes I think that's a separate issue, and Keys.NULL does not seem properly handled at the moment for Firefox (see https://bugzilla.mozilla.org/show_bug.cgi?id=1494661)

the behavior for text fields and divs with contenteditable="true" is different here.

I could find several bugs already tracking this issue: