Skip to content

Commit 0ed6df9

Browse files
committed
implemented delay to Keyboard Key press
1 parent 826cc2b commit 0ed6df9

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

Browser/keywords/interaction.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,19 +1318,23 @@ def mouse_wheel(self, deltaX: int, deltaY: int):
13181318
logger.info(response.log)
13191319

13201320
@keyword(tags=("Setter", "PageContent"))
1321-
def keyboard_key(self, action: KeyAction, key: str):
1321+
def keyboard_key(
1322+
self, action: KeyAction, key: str, *, delay: timedelta = timedelta(0)
1323+
):
13221324
"""Press a keyboard key on the virtual keyboard or set a key up or down.
13231325
13241326
| =Arguments= | =Description= |
13251327
| ``action`` | Determines whether the key should be released (``up``), held down (``down``) or pressed once (``press``). ``down`` and ``up`` are useful for combinations, i.e. with Shift. |
13261328
| ``key`` | The key to be pressed. Examples of valid keys are: ``F1`` - ``F12``, ``Digit0`` - ``Digit9``, ``KeyA`` - ``KeyZ``, ``Backquote``, ``Minus``, ``Equal``, ``Backslash``, ``Backspace``, ``Tab``, ``Delete``, ``Escape``, ``ArrowDown``, ``End``, ``Enter``, ``Home``, ``Insert``, ``PageDown``, ``PageUp``, ``ArrowRight``, ``ArrowUp`` , etc. |
1329+
| ``delay`` | Time the key is held down between keydown and keyup, in Robot Framework's time format. Only valid with action ``press``, other actions raise an error. Defaults to ``0 s``. Example: ``50 ms`` |
13271330
13281331
13291332
Useful keys for ``down`` and ``up`` for example are:
13301333
``Shift``, ``Control``, ``Alt``, ``Meta``, ``ShiftLeft``
13311334
13321335
Example execution:
13331336
| `Keyboard Key` press S
1337+
| `Keyboard Key` press S delay=500 ms
13341338
| `Keyboard Key` down Shift
13351339
| `Keyboard Key` press ArrowLeft
13361340
| `Keyboard Key` press Delete
@@ -1340,9 +1344,15 @@ def keyboard_key(self, action: KeyAction, key: str):
13401344
13411345
[https://forum.robotframework.org/t//4298|Comment >>]
13421346
"""
1347+
if delay and action is not KeyAction.press:
1348+
raise ValueError("delay is only valid if action is 'press'")
13431349
with self.playwright.grpc_channel() as stub:
13441350
response = stub.KeyboardKey(
1345-
Request().KeyboardKeypress(action=action.name, key=key)
1351+
Request().KeyboardKeypress(
1352+
action=action.name,
1353+
key=key,
1354+
delay=int(delay.total_seconds() * 1000),
1355+
)
13461356
)
13471357
logger.debug(response.log)
13481358

atest/test/02_Content_Keywords/virtual_keyboard.robot

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,27 @@ Select List Options
2020
Keyboard Key press ArrowDown
2121
Keyboard Key press ArrowDown
2222
Get Selected Options select[name="possible_channels"] value == email phone directmail
23+
24+
Keyboard Key Press Holds The Key For The Given Delay
25+
[Setup] New Page ${EVENTS_URL}
26+
Click id=event_log_clear
27+
Click id=event_test_input
28+
Keyboard Key press a delay=200 ms
29+
${log} = Get Text id=event_log_text
30+
Assert Key Timings ${log} a expected_press_duration_ms=200ms
31+
32+
Keyboard Key Press Without Delay Does Not Hold The Key
33+
[Setup] New Page ${EVENTS_URL}
34+
Click id=event_log_clear
35+
Click id=event_test_input
36+
Keyboard Key press b
37+
${log} = Get Text id=event_log_text
38+
Assert Key Timings ${log} b expected_press_duration_ms=0
39+
40+
Keyboard Key Delay Is Only Valid For Press
41+
Run Keyword And Expect Error
42+
... ValueError: delay is only valid if action is 'press'
43+
... Keyboard Key down Shift delay=100 ms
44+
Run Keyword And Expect Error
45+
... ValueError: delay is only valid if action is 'press'
46+
... Keyboard Key up Shift delay=100 ms

node/playwright-wrapper/interaction.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ export async function mouseWheel(request: pb.Request_MouseWheel, page?: Page): P
302302
export async function keyboardKey(request: pb.Request_KeyboardKeypress, page: Page): Promise<pb.Response_Empty> {
303303
const action = request.action as 'down' | 'up' | 'press';
304304
const key = request.key;
305+
const delay = request.delay;
306+
if (action === 'press' && delay > 0) {
307+
await invokeOnKeyboard(page, action, key, { delay: delay });
308+
return emptyWithLog(`Successfully did ${action} for ${key} with delay ${delay}ms`);
309+
}
305310
await invokeOnKeyboard(page, action, key);
306311
return emptyWithLog(`Successfully did ${action} for ${key}`);
307312
}

protobuf/playwright.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ message Request {
178178
message KeyboardKeypress {
179179
string action = 1;
180180
string key = 2;
181+
int32 delay = 3;
181182
}
182183

183184
message KeyboardInputOptions {

0 commit comments

Comments
 (0)