Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keydown supports commands #293

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/puppeteer/keyboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(client)

# @param key [String]
# @param text [String]
def down(key, text: nil)
def down(key, text: nil, commands: nil)
description = key_description_for_string(key)

auto_repeat = @pressed_keys.include?(description.code)
Expand All @@ -34,6 +34,7 @@ def down(key, text: nil)
autoRepeat: auto_repeat,
location: description.location,
isKeypad: description.location == 3,
commands: commands,
}.compact
@client.send_message('Input.dispatchKeyEvent', params)
end
Expand Down Expand Up @@ -151,8 +152,8 @@ def type_text(text, delay: nil)
# @param key [String]
# @param text [String]
# @return [Future]
def press(key, delay: nil, text: nil)
down(key, text: text)
def press(key, delay: nil, text: nil, commands: nil)
down(key, text: text, commands: commands)
if delay
sleep(delay.to_i / 1000.0)
end
Expand Down
31 changes: 31 additions & 0 deletions spec/integration/keyboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@
expect(page.evaluate("() => document.querySelector('textarea').value")).to eq('Hello World!')
end

# @see https://github.com/puppeteer/puppeteer/issues/1313
it_fails_firefox 'should trigger commands of keyboard shortcuts' do
cmd_key = Puppeteer.env.darwin? ? 'Control' : 'Meta'

page.type_text('textarea', 'hello')

page.keyboard do
down cmd_key
press 'a', commands: ['SelectAll']
up cmd_key
end

page.keyboard do
down cmd_key
down 'c', commands: ['Copy']
up 'c'
up cmd_key
end

2.times do
page.keyboard do
down cmd_key
press 'v', commands: ['Paste']
up cmd_key
end
end

value = page.evaluate("() => document.querySelector('textarea').value")
expect(value).to eq('hellohello')
end

it 'should send a character with ElementHandle.press' do
textarea = page.query_selector('textarea')
textarea.press('a')
Expand Down