Skip to content

Commit

Permalink
Add basic tests for BiDi key input
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D157974

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1792090
gecko-commit: 90a1606bc5b8bf317ade1dee4dae849a583cdad3
gecko-reviewers: webdriver-reviewers, whimboo
  • Loading branch information
jgraham committed Apr 11, 2023
1 parent 8f9d71e commit 6b3460f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
14 changes: 14 additions & 0 deletions webdriver/tests/bidi/input/perform_actions/key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest

from webdriver.bidi.modules.input import Actions

pytestmark = pytest.mark.asyncio


async def test_null_response_value(bidi_session, top_context):
actions = Actions()
actions.add_key().key_down("a").key_up("a")
value = await bidi_session.input.perform_actions(actions=actions,
context=top_context["context"])
assert value == {}

92 changes: 92 additions & 0 deletions webdriver/tests/bidi/input/perform_actions/key_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import json

import pytest

from webdriver.bidi.modules.input import Actions
from webdriver.bidi.modules.script import ContextTarget

from tests.support.helpers import filter_dict

pytestmark = pytest.mark.asyncio


async def get_events(context, bidi_session):
"""Return list of key events recorded in the test_keys_page fixture."""
events_str = await bidi_session.script.evaluate(expression="JSON.stringify(allEvents.events)",
target=ContextTarget(context),
await_promise=False)
events = json.loads(events_str["value"])

# `key` values in `allEvents` may be escaped (see `escapeSurrogateHalf` in
# test_actions.html), so this converts them back into unicode literals.
for e in events:
# example: turn "U+d83d" (6 chars) into u"\ud83d" (1 char)
if "key" in e and e["key"].startswith(u"U+"):
key = e["key"]
hex_suffix = key[key.index("+") + 1:]
e["key"] = chr(int(hex_suffix, 16))

# WebKit sets code as 'Unidentified' for unidentified key codes, but
# tests expect ''.
if "code" in e and e["code"] == "Unidentified":
e["code"] = ""
return events


@pytest.mark.parametrize("value,code", [
(u"a", "KeyA",),
("a", "KeyA",),
(u"\"", "Quote"),
(u",", "Comma"),
(u"\u00E0", ""),
(u"\u0416", ""),
(u"@", "Digit2"),
(u"\u2603", ""),
(u"\uF6C2", ""), # PUA
])
async def test_printable_key_sends_correct_events(bidi_session, top_context, url, value, code):
await bidi_session.browsing_context.navigate(
context=top_context["context"],
url=url("/webdriver/tests/support/html/test_actions.html"),
wait="complete"
)
await bidi_session.script.call_function(function_declaration="""
() => {
let elem = document.getElementById("keys");
elem.focus();
resetEvents();
}""",
target=ContextTarget(top_context["context"]),
await_promise=False)

actions = Actions()
(actions.add_key()
.key_down(value)
.key_up(value))
await bidi_session.input.perform_actions(actions=actions,
context=top_context["context"])

all_events = await get_events(top_context["context"], bidi_session)

expected = [
{"code": code, "key": value, "type": "keydown"},
{"code": code, "key": value, "type": "keypress"},
{"code": code, "key": value, "type": "keyup"},
]

events = [filter_dict(e, expected[0]) for e in all_events]
if len(events) > 0 and events[0]["code"] is None:
# Remove 'code' entry if browser doesn't support it
expected = [filter_dict(e, {"key": "", "type": ""}) for e in expected]
events = [filter_dict(e, expected[0]) for e in events]
assert events == expected

keys_value = await bidi_session.script.call_function(function_declaration="""
() => {
let elem = document.getElementById("keys");
return elem.value
}""",
target=ContextTarget(top_context["context"]),
await_promise=False)

assert keys_value["value"] == value
2 changes: 1 addition & 1 deletion webdriver/tests/perform_actions/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def modifier_key(session):

@pytest.fixture
def test_actions_page(session, url):
session.url = url("/webdriver/tests/perform_actions/support/test_actions_wdspec.html")
session.url = url("/webdriver/tests/support/html/test_actions.html")


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion webdriver/tests/perform_actions/pointer_mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_click_element_center(session, test_actions_page, mouse_chain):


def test_click_navigation(session, url, inline):
destination = url("/webdriver/tests/actions/support/test_actions_wdspec.html")
destination = url("/webdriver/tests/support/html/test_actions.html")
start = inline("<a href=\"{}\" id=\"link\">destination</a>".format(destination))

def click(link):
Expand Down
2 changes: 1 addition & 1 deletion webdriver/tests/release_actions/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ def key_reporter(session, test_actions_page, request):

@pytest.fixture
def test_actions_page(session, url):
session.url = url("/webdriver/tests/release_actions/support/test_actions_wdspec.html")
session.url = url("/webdriver/tests/support/html/test_actions.html")

0 comments on commit 6b3460f

Please sign in to comment.