Skip to content

Commit

Permalink
test: Drop Browser.key_press(), replace with Browser.input_text()
Browse files Browse the repository at this point in the history
`input_text()` is now solely for plain characters, no control ones or
modifiers. That keeps the implementation very simple and browser independent.

Fixes #20344
  • Loading branch information
martinpitt committed Jun 10, 2024
1 parent e62b39e commit cbe46ee
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 89 deletions.
57 changes: 6 additions & 51 deletions test/common/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,56 +516,11 @@ def blur(self, selector: str) -> None:
self.wait_visible(selector + ':not([disabled]):not([aria-disabled=true])')
self.call_js_func('ph_blur', selector)

# TODO: Unify them so we can have only one
def key_press(self, keys: str, modifiers: int = 0, use_ord: bool = False) -> None:
if self.cdp.browser.name == "chromium":
self._key_press_chromium(keys, modifiers, use_ord)
else:
self._key_press_firefox(keys, modifiers, use_ord)

def _key_press_chromium(self, keys: str, modifiers: int = 0, use_ord: bool = False) -> None:
for key in keys:
args = {"type": "keyDown", "modifiers": modifiers}

# If modifiers are used we need to pass windowsVirtualKeyCode which is
# basically the asci decimal representation of the key
args["text"] = key
if use_ord:
args["windowsVirtualKeyCode"] = ord(key)
elif (not key.isalnum() and ord(key) < 32) or modifiers != 0:
args["windowsVirtualKeyCode"] = ord(key.upper())
else:
args["key"] = key

self.cdp.invoke("Input.dispatchKeyEvent", **args)
args["type"] = "keyUp"
self.cdp.invoke("Input.dispatchKeyEvent", **args)

def _key_press_firefox(self, keys: str, modifiers: int = 0, use_ord: bool = False) -> None:
# https://python-reference.readthedocs.io/en/latest/docs/str/ASCII.html
# Both line feed and carriage return are normalized to Enter (https://html.spec.whatwg.org/multipage/form-elements.html)
keyMap = {
8: "Backspace", # Backspace key
9: "Tab", # Tab key
10: "Enter", # Enter key (normalized from line feed)
13: "Enter", # Enter key (normalized from carriage return)
27: "Escape", # Escape key
37: "ArrowLeft", # Arrow key left
40: "ArrowDown", # Arrow key down
45: "Insert", # Insert key
}
for key in keys:
args = {"type": "keyDown", "modifiers": modifiers}

args["key"] = key
if ord(key) < 32 or use_ord:
args["key"] = keyMap[ord(key)]

self.cdp.invoke("Input.dispatchKeyEvent", **args)
args["type"] = "keyUp"
self.cdp.invoke("Input.dispatchKeyEvent", **args)

# FIXME: This should be key_press(), and key_press() should be something else
def input_text(self, text: str) -> None:
for char in text:
self.cdp.invoke("Input.dispatchKeyEvent", type="keyDown", text=char, key=char)
self.cdp.invoke("Input.dispatchKeyEvent", type="keyUp", text=char, key=char)

def key(self, name: str, repeat: int = 1, modifiers: int = 0) -> None:
"""Press and release a named keyboard key.
Expand Down Expand Up @@ -627,7 +582,7 @@ def set_input_text(
if val == "":
self.key("Backspace")
else:
self.key_press(val)
self.input_text(val)
if blur:
self.blur(selector)

Expand Down
28 changes: 14 additions & 14 deletions test/verify/check-pages
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ OnCalendar=daily

b.wait_visible(".terminal .xterm-accessibility-tree")
b.wait_in_text(line_sel(1), "admin")
b.key_press("echo $LANG")
b.input_text("echo $LANG")
b.key("Enter")
b.wait_in_text(line_sel(2), locale)

Expand Down Expand Up @@ -447,7 +447,7 @@ OnCalendar=daily

# Check that some page disappears and some stay
b.focus(filter_sel)
b.key_press("se")
b.input_text("se")
b.wait_not_present("#host-apps li a:contains('Logs')")
b.wait_visible("#host-apps li a:contains('Services')")
b.wait_text("#host-apps li a:contains('Services') mark", "Se")
Expand All @@ -459,15 +459,15 @@ OnCalendar=daily

# Check that any substring work
b.focus(filter_sel)
b.key_press("CoUN")
b.input_text("CoUN")
b.wait_not_present("#host-apps li a:contains('Overview')")
b.wait_visible("#host-apps li a:contains('Accounts')")
b.wait_text("#host-apps li a:contains('Accounts') mark", "coun")

# Check it can also search by keywords
b.focus(filter_sel)
b.key("Backspace", 4)
b.key_press("systemd")
b.input_text("systemd")
b.wait_visible("#host-apps li a:contains('Services')")
b.wait_text("#host-apps li a:contains('Services')", "ServicesContains: systemd")
b.wait_text("#host-apps li a:contains('Services') mark", "systemd")
Expand All @@ -483,7 +483,7 @@ OnCalendar=daily
# Check that enter activates first result
b.focus(filter_sel)
b.key("Backspace", 7)
b.key_press("logs")
b.input_text("logs")
b.wait_not_present("#host-apps li a:contains('Services')")
b.wait_visible("#host-apps li a:contains('Logs')")
b.focus(filter_sel)
Expand All @@ -496,7 +496,7 @@ OnCalendar=daily
b.wait_val(filter_sel, "")

# Check that escape cleans the search
b.key_press("logs")
b.input_text("logs")
b.wait_not_present("#host-apps li a:contains('Services')")
b.wait_visible("#host-apps li a:contains('Logs')")
b.focus(filter_sel)
Expand All @@ -505,7 +505,7 @@ OnCalendar=daily
b.wait_visible("#host-apps li a:contains('Services')")

# Check that clicking on `Clear search` cleans the search
b.key_press("logs")
b.input_text("logs")
b.wait_not_present("#host-apps li a:contains('Services')")
b.wait_visible("#host-apps li a:contains('Logs')")
b.click("button:contains('Clear search')")
Expand All @@ -515,7 +515,7 @@ OnCalendar=daily

# Check that arrows navigate the menu
b.focus(filter_sel)
b.key_press("s")
b.input_text("s")
b.wait_not_present("#host-apps li a:contains('Logs')")
b.key("ArrowDown", 2)
b.key("Enter")
Expand All @@ -527,7 +527,7 @@ OnCalendar=daily
# Check we jump into subpage when defined in manifest
b.switch_to_top()
b.focus(filter_sel)
b.key_press("firew")
b.input_text("firew")
b.wait_visible("#host-apps li a:contains('Networking')")
b.wait_not_present("#host-apps li a:contains('Overview')")
b.click("#host-apps li a:contains('Networking')")
Expand All @@ -553,7 +553,7 @@ OnCalendar=daily
b.wait_visible("#host-apps li a:contains('Dienste')")
b.wait_visible("#host-apps li a:contains('Protokolle')")
b.focus(filter_sel)
b.key_press("dien")
b.input_text("dien")
b.wait_not_present("#host-apps li a:contains('Protokolle')")
b.wait_visible("#host-apps li a:contains('Dienste')")
b.wait_text("#host-apps li a:contains('Dienste') mark", "Dien")
Expand Down Expand Up @@ -594,7 +594,7 @@ OnCalendar=daily

# test file completion widget
b.focus("#demo-file-ac input[type=text]")
b.key_press(stuff + "/")
b.input_text(stuff + "/")
# need to wait for the widget's "fast typing" inhibition delay to trigger the completion popup
b.wait_in_text("#file-autocomplete-widget li:nth-of-type(1) button", stuff + "/")
b.wait_in_text("#file-autocomplete-widget li:nth-of-type(2) button", "dir/")
Expand All @@ -606,15 +606,15 @@ OnCalendar=daily
b.click("#demo-file-ac div:first-of-type div:first-of-type button:nth-of-type(1)")
# test if input matches one entry, but is the prefix of other entry, widget should not descend into directory
b.focus("#demo-file-ac input[type=text]")
b.key_press(stuff + "/dir")
b.input_text(stuff + "/dir")
b.wait_in_text("#file-autocomplete-widget li:nth-of-type(1) button", stuff + "/dir")
b.wait_in_text("#file-autocomplete-widget li:nth-of-type(2) button", stuff + "/dir1")

# clear the file completion widget
b.click("#demo-file-ac div:first-of-type div:first-of-type button:nth-of-type(1)")
b.wait_not_present("#file-autocomplete-widget li")
b.focus("#demo-file-ac input[type=text]")
b.key_press(stuff + "/")
b.input_text(stuff + "/")
b.wait_in_text("#file-autocomplete-widget li:nth-of-type(1) button", stuff + "/")
b.wait_in_text("#file-autocomplete-widget li:nth-of-type(4) button", "file1.txt")
b.click("#file-autocomplete-widget li:nth-of-type(4) button")
Expand All @@ -640,7 +640,7 @@ OnCalendar=daily
# content with every user input change, which is definitely a performance cost
b.key("Backspace", 6)
time.sleep(1)
b.key_press("stuff/")
b.input_text("stuff/")
b.wait_in_text("#file-autocomplete-widget li:nth-of-type(5) button", "other")

# Create test folder with known files
Expand Down
2 changes: 1 addition & 1 deletion test/verify/check-selinux
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class TestSelinux(testlib.MachineCase):
b.go("/system/terminal")
b.enter_page("/system/terminal")
b.focus('.terminal')
b.key_press('"')
b.input_text('"')
b.key("Enter")
# Right click and paste
b.mouse(".terminal", "contextmenu", btn=2)
Expand Down
2 changes: 1 addition & 1 deletion test/verify/check-shell-active-pages
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TestActivePages(testlib.MachineCase):
b.wait_in_text(line_sel(1), '$')

# run a command that we can easily identify, and which will die with the terminal
b.key_press("bash -c 'exec -a kitten cat'")
b.input_text("bash -c 'exec -a kitten cat'")
b.key("Enter")

# wait for command to run
Expand Down
6 changes: 3 additions & 3 deletions test/verify/check-shell-host-switching
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,11 @@ class TestHostSwitching(testlib.MachineCase, HostSwitcherHelpers):
b.click("input[type=color]")
time.sleep(1) # We cannot wait until the popup opens up, so just little of waiting
b.key("Tab", repeat=3)
b.key_press("0")
b.input_text("0")
b.key("Tab")
b.key_press("0")
b.input_text("0")
b.key("Tab")
b.key_press("0")
b.input_text("0")
b.key("Enter")

b.click('#hosts_setup_server_dialog .pf-m-primary')
Expand Down
12 changes: 6 additions & 6 deletions test/verify/check-system-journal
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ ExecStart=/bin/sh -c 'sleep 5; for s in $(seq 10); do echo SLOW; sleep 0.1; done
# Test message filtering

b.focus("#journal-grep input")
b.key_press("1")
b.input_text("1")
b.click("#journal-grep button[aria-label=Search]")

b.wait_js_cond('window.location.hash === "#/?priority=notice&tag=test-stream&grep=1"')
Expand All @@ -269,7 +269,7 @@ ExecStart=/bin/sh -c 'sleep 5; for s in $(seq 10); do echo SLOW; sleep 0.1; done

b.focus("#journal-grep input")
b.key("Backspace")
b.key_press("m.*[2-5]")
b.input_text("m.*[2-5]")
b.click("#journal-grep button[aria-label=Search]")
b.wait_not_present("#journal-box .cockpit-logline .cockpit-log-message:contains('Message 1')")
b.wait_visible("#journal-box .cockpit-logline .cockpit-log-message:contains('Message 2')")
Expand All @@ -286,7 +286,7 @@ ExecStart=/bin/sh -c 'sleep 5; for s in $(seq 10); do echo SLOW; sleep 0.1; done
b.wait_visible("#journal-box .cockpit-logline .cockpit-log-message:contains('Message 6')")

b.focus("#journal-grep input")
b.key_press("FOO=bar [5-6]")
b.input_text("FOO=bar [5-6]")
b.click("#journal-grep button[aria-label=Search]")
b.wait_val("#journal-grep input", "priority:err identifier:logger FOO=bar [5-6]")

Expand All @@ -296,7 +296,7 @@ ExecStart=/bin/sh -c 'sleep 5; for s in $(seq 10); do echo SLOW; sleep 0.1; done

b.focus("#journal-grep input")
b.key("Backspace", 13)
b.key_press("[5-6]")
b.input_text("[5-6]")
b.click("#journal-grep button[aria-label=Search]")
b.wait_not_present("#journal-box .cockpit-logline .cockpit-log-message:contains('Message 4')")
b.wait_visible("#journal-box .cockpit-logline .cockpit-log-message:contains('Message 6')")
Expand All @@ -318,7 +318,7 @@ ExecStart=/bin/sh -c 'sleep 5; for s in $(seq 10); do echo SLOW; sleep 0.1; done
# Check that 'until' qualifier keeps streaming until given time
b.focus("#journal-grep input")
b.key("Backspace", 4)
b.key_press("6-9] until:+15s")
b.input_text("6-9] until:+15s")
b.click("#journal-grep button[aria-label=Search]")
b.wait_not_present("#journal-box .cockpit-logline .cockpit-log-message:contains('Message 5')")
b.wait_visible("#journal-box .cockpit-logline .cockpit-log-message:contains('Message 6')")
Expand Down Expand Up @@ -482,7 +482,7 @@ ExecStart=/bin/sh -c 'sleep 5; for s in $(seq 10); do echo SLOW; sleep 0.1; done
])

b.focus("#journal-grep input")
b.key_press("until:2037-01-01\\ 00:03")
b.input_text("until:2037-01-01\\ 00:03")
b.click("#journal-grep button[aria-label=Search]")
wait_log_lines([expected_date,
["check-journal", "BEFORE BOOT", ""],
Expand Down
6 changes: 3 additions & 3 deletions test/verify/check-system-realms
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ ipa-advise enable-admins-sudo | sh -ex
b.open("/system/terminal")
b.enter_page("/system/terminal")
b.wait_in_text(".terminal .xterm-accessibility-tree", "alice")
b.key_press("klist")
b.input_text("klist")
b.key("Enter")
b.wait_in_text(".terminal .xterm-accessibility-tree", "Ticket cache: FILE:/run/user")
b.wait_in_text(".terminal .xterm-accessibility-tree", "Default principal: [email protected]")
Expand Down Expand Up @@ -831,7 +831,7 @@ ipa-advise enable-admins-sudo | sh -ex
b.open("/@x0.cockpit.lan/system/terminal")
b.enter_page("/system/terminal", host="x0.cockpit.lan")
b.wait_in_text(".terminal .xterm-accessibility-tree", "alice")
b.key_press(f"{ccache_env} sudo whoami")
b.input_text(f"{ccache_env} sudo whoami")
b.key("Enter")
b.wait_in_text(".terminal .xterm-accessibility-tree", "root")

Expand Down Expand Up @@ -1074,7 +1074,7 @@ class TestKerberos(testlib.MachineCase):
b.enter_page("/system/terminal")
# wait for prompt
b.wait_in_text(".terminal .xterm-accessibility-tree", "admin")
b.key_press("klist")
b.input_text("klist")
b.key("Enter")
b.wait_in_text(".terminal .xterm-accessibility-tree", "Ticket cache")
b.wait_in_text(".terminal .xterm-accessibility-tree", "Default principal: [email protected]")
Expand Down
20 changes: 10 additions & 10 deletions test/verify/check-system-terminal
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/
b.wait_js_func(function_str, line_sel(n))

# clear any messages (for example, instructions about sudo) and wait for prompt
b.key_press("clear")
b.input_text("clear")
b.wait_js_cond("ph_text('.terminal').indexOf('clear') >= 0")
# now wait for clear to take effect
b.key("Enter")
Expand All @@ -84,21 +84,21 @@ PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/
self.assertIn("~]$", prompt)

# Run some commands
b.key_press("whoami")
b.input_text("whoami")
b.key("Enter")
wait_line(n + 1, "admin")

wait_line(n + 2, prompt)

b.key_press('echo -e "1\\u0041"')
b.input_text('echo -e "1\\u0041"')
b.key("Enter")
wait_line(n + 3, '1A')

# The '@' sign is in the default prompt
b.wait_in_text(".terminal-title", '@')

# output flooding
b.key_press("seq 1000000")
b.input_text("seq 1000000")
b.key("Enter")
with b.wait_timeout(300):
b.wait_in_text(".terminal .xterm-accessibility-tree", "9999989999991000000admin@")
Expand All @@ -114,7 +114,7 @@ PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/
self.assertNotIn('admin', b.text(line_sel(n + 1)))

# Check that when we `exit` we can still reconnect with the 'Reset' button
b.key_press("exit")
b.input_text("exit")
b.key("Enter")
b.wait_in_text(".terminal .xterm-accessibility-tree", "disconnected")
b.click('button:contains("Reset")')
Expand All @@ -139,7 +139,7 @@ PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/

# Execute command
wait_line(n, prompt)
b.key_press('echo "XYZ"')
b.input_text('echo "XYZ"')
b.key("Enter")
echo_result_line = n + 1

Expand Down Expand Up @@ -173,7 +173,7 @@ PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/

# Execute another command
wait_line(n, prompt)
b.key_press('echo "foo"')
b.input_text('echo "foo"')
b.key("Enter")
echo_result_line = n + 1

Expand All @@ -192,9 +192,9 @@ PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/

# check that we get a sensible $PATH; this varies across OSes, so don't be too strict about it
b.key("Enter")
b.key_press('clear')
b.input_text('clear')
b.key("Enter")
b.key_press("echo $PATH > /tmp/path")
b.input_text("echo $PATH > /tmp/path")
b.key("Enter")
# don't use wait_line() for the full match here, as line breaks get in the way; just wait until command has run
wait_line(echo_result_line, prompt)
Expand Down Expand Up @@ -266,7 +266,7 @@ PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/

# execute a command we can see the effect of
b.wait_js_cond("ph_text('.terminal').indexOf('uid=') == -1")
b.key_press("id")
b.input_text("id")
b.key("Enter")
b.wait_js_cond("ph_text('.terminal').indexOf('uid=') >= 0")

Expand Down

0 comments on commit cbe46ee

Please sign in to comment.