Skip to content

Commit

Permalink
Move the caret to the end for send_keys
Browse files Browse the repository at this point in the history
Only if the element isn't already focused. If it is already focused,
don't alter the focus.

Differential Revision: https://phabricator.services.mozilla.com/D158084

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1791736
gecko-commit: 041c4a14b5ef4b3cd808523379a20c7c43540a8c
gecko-reviewers: webdriver-reviewers, jdescottes, whimboo
  • Loading branch information
jgraham authored and moz-wptsync-bot committed Oct 4, 2022
1 parent 92952f8 commit 25fcad4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 43 deletions.
24 changes: 18 additions & 6 deletions webdriver/tests/element_send_keys/content_editable.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
from tests.support.asserts import assert_element_has_focus


def test_sets_insertion_point_to_end(session, inline):
session.url = inline('<div contenteditable=true>Hello,</div>')
body = session.find.css("body", all=False)
assert_element_has_focus(body)

input = session.find.css("div", all=False)
input.send_keys(' world!')
text = session.execute_script('return arguments[0].innerText', args=[input])
text = session.execute_script('return arguments[0].textContent', args=[input])
assert "Hello, world!" == text.strip()
assert_element_has_focus(input)


# 12. Let current text length be the element's length.
#
# 13. Set the text insertion caret using set selection range using current
# text length for both the start and end parameters.
def test_sets_insertion_point_to_after_last_text_node(session, inline):
session.url = inline('<div contenteditable=true>Hel<span>lo</span>,</div>')
input = session.find.css("div", all=False)
input.send_keys(" world!")
text = session.execute_script("return arguments[0].innerText", args=[input])
text = session.execute_script("return arguments[0].textContent", args=[input])
assert "Hello, world!" == text.strip()


def test_no_move_caret_if_focused(session, inline):
session.url = inline("""<div contenteditable=true>Hel<span>lo</span>,</div>
<script>document.getElementsByTagName("div")[0].focus()</script>""")
input = session.find.css("div", all=False)
input.send_keys("world!")
text = session.execute_script("return arguments[0].textContent", args=[input])
assert "world!Hello," == text.strip()
37 changes: 36 additions & 1 deletion webdriver/tests/element_send_keys/file_upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from tests.support.asserts import assert_error, assert_files_uploaded, assert_success
from tests.support.asserts import (assert_element_has_focus,
assert_error,
assert_files_uploaded,
assert_success)

from . import map_files_to_multiline_text

Expand Down Expand Up @@ -207,6 +210,38 @@ def test_display_none(session, create_files, inline):
assert_files_uploaded(session, element, files)


@pytest.mark.capabilities({"strictFileInteractability": False})
def test_not_focused(session, create_files, inline):
files = create_files(["foo"])

session.url = inline("<input type=file>")
body = session.find.css("body", all=False)
element = session.find.css("input", all=False)
assert_element_has_focus(body)

response = element_send_keys(session, element, str(files[0]))
assert_success(response)
assert_element_has_focus(body)

assert_files_uploaded(session, element, files)


@pytest.mark.capabilities({"strictFileInteractability": True})
def test_focused(session, create_files, inline):
files = create_files(["foo"])

session.url = inline("<input type=file>")
body = session.find.css("body", all=False)
element = session.find.css("input", all=False)
assert_element_has_focus(body)

response = element_send_keys(session, element, str(files[0]))
assert_success(response)
assert_element_has_focus(element)

assert_files_uploaded(session, element, files)


@pytest.mark.capabilities({"strictFileInteractability": True})
def test_strict_hidden(session, create_files, inline):
files = create_files(["foo"])
Expand Down
49 changes: 49 additions & 0 deletions webdriver/tests/element_send_keys/form_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ def test_textarea(session, inline):

def test_input_append(session, inline):
session.url = inline("<input value=a>")
body = session.find.css("body", all=False)
assert_element_has_focus(body)
element = session.find.css("input", all=False)
assert element.property("value") == "a"

element_send_keys(session, element, "b")
assert_element_has_focus(element)
assert element.property("value") == "ab"

element_send_keys(session, element, "c")
Expand All @@ -43,11 +46,57 @@ def test_input_append(session, inline):

def test_textarea_append(session, inline):
session.url = inline("<textarea>a</textarea>")
body = session.find.css("body", all=False)
assert_element_has_focus(body)
element = session.find.css("textarea", all=False)
assert element.property("value") == "a"

element_send_keys(session, element, "b")
assert_element_has_focus(element)
assert element.property("value") == "ab"

element_send_keys(session, element, "c")
assert element.property("value") == "abc"


def test_input_insert_when_focused(session, inline):
session.url = inline("""<input value=a>
<script>
let elem = document.getElementsByTagName("input")[0];
elem.focus();
elem.setSelectionRange(0, 0);
</script>""")
element = session.find.css("input", all=False)
assert element.property("value") == "a"

element_send_keys(session, element, "b")
assert element.property("value") == "ba"

element_send_keys(session, element, "c")
assert element.property("value") == "bca"


def test_textarea_insert_when_focused(session, inline):
session.url = inline("""<textarea>a</textarea>
<script>
let elem = document.getElementsByTagName("textarea")[0];
elem.focus();
elem.setSelectionRange(0, 0);
</script>""")
element = session.find.css("textarea", all=False)
assert element.property("value") == "a"

element_send_keys(session, element, "b")
assert element.property("value") == "ba"

element_send_keys(session, element, "c")
assert element.property("value") == "bca"


def test_date(session, inline):
session.url = inline("<input type=date>")
element = session.find.css("input", all=False)

element_send_keys(session, element, "2000-01-01")
assert element.property("value") == "2000-01-01"
assert_element_has_focus(element)
36 changes: 0 additions & 36 deletions webdriver/tests/element_send_keys/scroll_into_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,6 @@ def test_element_outside_of_scrollable_viewport(session, inline):
assert is_element_in_viewport(session, element)


def test_option_select_container_outside_of_scrollable_viewport(session, inline):
session.url = inline("""
<select style="margin-top: 102vh;">
<option value="foo">foo</option>
<option value="bar" id="bar">bar</option>
</select>
""")
element = session.find.css("option#bar", all=False)
select = session.find.css("select", all=False)

response = element_send_keys(session, element, "bar")
assert_success(response)

assert is_element_in_viewport(session, select)
assert is_element_in_viewport(session, element)


def test_option_stays_outside_of_scrollable_viewport(session, inline):
session.url = inline("""
<select multiple style="height: 105vh; margin-top: 100vh;">
<option value="foo" id="foo" style="height: 100vh;">foo</option>
<option value="bar" id="bar" style="background-color: yellow;">bar</option>
</select>
""")
select = session.find.css("select", all=False)
option_foo = session.find.css("option#foo", all=False)
option_bar = session.find.css("option#bar", all=False)

response = element_send_keys(session, option_bar, "bar")
assert_success(response)

assert is_element_in_viewport(session, select)
assert is_element_in_viewport(session, option_foo)
assert not is_element_in_viewport(session, option_bar)


def test_contenteditable_element_outside_of_scrollable_viewport(session, inline):
session.url = inline("<div contenteditable style=\"margin-top: 102vh;\"></div>")
element = session.find.css("div", all=False)
Expand Down

0 comments on commit 25fcad4

Please sign in to comment.