Class: Playwright::Keyboard

Inherits:
PlaywrightApi show all
Defined in:
lib/playwright_api/keyboard.rb

Overview

Keyboard provides an api for managing a virtual keyboard. The high level api is [‘method: Keyboard.type`], which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page.

For finer control, you can use [‘method: Keyboard.down`], [`method: Keyboard.up`], and [`method: Keyboard.insertText`] to manually fire events as if they were generated from a real keyboard.

An example of holding down ‘Shift` in order to select and delete some text:

“‘js await page.keyboard.type(’Hello World!‘); await page.keyboard.press(’ArrowLeft’);

await page.keyboard.down(‘Shift’); for (let i = 0; i < ‘ World’.length; i++)

await page.keyboard.press('ArrowLeft');

await page.keyboard.up(‘Shift’);

await page.keyboard.press(‘Backspace’); // Result text will end up saying ‘Hello!’ “‘

An example of pressing uppercase ‘A`

“‘js await page.keyboard.press(’Shift+KeyA’); // or await page.keyboard.press(‘Shift+A’); “‘

An example to trigger select-all with the keyboard

“‘js // on Windows and Linux await page.keyboard.press(’Control+A’); // on macOS await page.keyboard.press(‘Meta+A’); “‘

Instance Method Summary collapse

Methods inherited from PlaywrightApi

from_channel_owner

Instance Method Details

#down(key) ⇒ Object

Dispatches a ‘keydown` event.

‘key` can specify the intended [keyboardEvent.key](developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) value or a single character to generate the text for. A superset of the `key` values can be found [here](developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the 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.

Following modification shortcuts are also supported: ‘Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`.

Holding down ‘Shift` will type the text that corresponds to the `key` in the upper case.

If ‘key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different respective texts.

If ‘key` is a modifier key, `Shift`, `Meta`, `Control`, or `Alt`, subsequent key presses will be sent with that modifier active. To release the modifier key, use [`method: Keyboard.up`].

After the key is pressed once, subsequent calls to [‘method: Keyboard.down`] will have [repeat](developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat) set to true. To release the key, use [`method: Keyboard.up`].

> NOTE Modifier keys DO influence ‘keyboard.down`. Holding down `Shift` will type the text in upper case.

Raises:

  • (NotImplementedError)


68
69
70
# File 'lib/playwright_api/keyboard.rb', line 68

def down(key)
  raise NotImplementedError.new('down is not implemented yet.')
end

#insert_text(text) ⇒ Object

Dispatches only ‘input` event, does not emit the `keydown`, `keyup` or `keypress` events.

“‘js page.keyboard.insertText(’嗨‘); “`

> NOTE Modifier keys DO NOT effect ‘keyboard.insertText`. Holding down `Shift` will not type the text in upper case.

Raises:

  • (NotImplementedError)


80
81
82
# File 'lib/playwright_api/keyboard.rb', line 80

def insert_text(text)
  raise NotImplementedError.new('insert_text is not implemented yet.')
end

#press(key, delay: nil) ⇒ Object

‘key` can specify the intended [keyboardEvent.key](developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) value or a single character to generate the text for. A superset of the `key` values can be found [here](developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values). Examples of the 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.

Following modification shortcuts are also supported: ‘Shift`, `Control`, `Alt`, `Meta`, `ShiftLeft`.

Holding down ‘Shift` will type the text that corresponds to the `key` in the upper case.

If ‘key` is a single character, it is case-sensitive, so the values `a` and `A` will generate different respective texts.

Shortcuts such as ‘key: “Control+o”` or `key: “Control+Shift+T”` are supported as well. When speficied with the modifier, modifier is pressed and being held while the subsequent key is being pressed.

“‘js const page = await browser.newPage(); await page.goto(’keycode.info’); await page.keyboard.press(‘A’); await page.screenshot({ path: ‘A.png’ }); await page.keyboard.press(‘ArrowLeft’); await page.screenshot({ path: ‘ArrowLeft.png’ }); await page.keyboard.press(‘Shift+O’); await page.screenshot({ path: ‘O.png’ }); await browser.close(); “‘

Shortcut for [‘method: Keyboard.down`] and [`method: Keyboard.up`].

Raises:

  • (NotImplementedError)


115
116
117
# File 'lib/playwright_api/keyboard.rb', line 115

def press(key, delay: nil)
  raise NotImplementedError.new('press is not implemented yet.')
end

#type_text(text, delay: nil) ⇒ Object

Sends a ‘keydown`, `keypress`/`input`, and `keyup` event for each character in the text.

To press a special key, like ‘Control` or `ArrowDown`, use [`method: Keyboard.press`].

“‘js await page.keyboard.type(’Hello’); // Types instantly await page.keyboard.type(‘World’, 100); // Types slower, like a user “‘

> NOTE Modifier keys DO NOT effect ‘keyboard.type`. Holding down `Shift` will not type the text in upper case.

Raises:

  • (NotImplementedError)


130
131
132
# File 'lib/playwright_api/keyboard.rb', line 130

def type_text(text, delay: nil)
  raise NotImplementedError.new('type_text is not implemented yet.')
end

#up(key) ⇒ Object

Dispatches a ‘keyup` event.

Raises:

  • (NotImplementedError)


135
136
137
# File 'lib/playwright_api/keyboard.rb', line 135

def up(key)
  raise NotImplementedError.new('up is not implemented yet.')
end