Module: Selenium::WebDriver::KeyActions

Included in:
W3CActionBuilder
Defined in:
lib/selenium/webdriver/common/interactions/key_actions.rb

Instance Method Summary collapse

Instance Method Details

#key_down(*args, device: nil) ⇒ W3CActionBuilder

Performs a key press. Does not release the key - subsequent interactions may assume it’s kept pressed. Note that the modifier key is never released implicitly - either #key_up(key) or #release_actions must be called to release the key.

Examples:

Press a key


driver.action.key_down(:control).perform

Press a key on an element


el = driver.find_element(id: "some_id")
driver.action.key_down(el, :shift).perform

Parameters:

  • element (Selenium::WebDriver::Element)

    An optional element

  • key (:shift, :alt, :control, :command, :meta, 'a')

    The key to press.

  • device (Symbol || String) (defaults to: nil)

    optional name of the KeyInput device to press the key on

Returns:



43
44
45
# File 'lib/selenium/webdriver/common/interactions/key_actions.rb', line 43

def key_down(*args, device: nil)
  key_action(*args, action: :create_key_down, device: device)
end

#key_up(*args, device: nil) ⇒ W3CActionBuilder

Performs a modifier key release. Releasing a non-depressed modifier key will yield undefined behaviour.

Examples:

Release a key


driver.action.key_up(:shift).perform

Release a key from an element


el = driver.find_element(id: "some_id")
driver.action.key_up(el, :alt).perform

Parameters:

  • element (Selenium::WebDriver::Element)

    An optional element

  • key (:shift, :alt, :control, :command, :meta, 'a')

    The modifier key to release.

  • device (Symbol || String) (defaults to: nil)

    optional name of the KeyInput device to release the key on

Returns:



66
67
68
# File 'lib/selenium/webdriver/common/interactions/key_actions.rb', line 66

def key_up(*args, device: nil)
  key_action(*args, action: :create_key_up, device: device)
end

#send_keys(*args, device: nil) ⇒ W3CActionBuilder

Sends keys to the active element. This differs from calling Element#send_keys(keys) on the active element in two ways:

  • The modifier keys included in this call are not released.

  • There is no attempt to re-focus the element - so send_keys(:tab) for switching elements should work.

Examples:

Send the text “help” to an element


el = driver.find_element(id: "some_id")
driver.action.send_keys(el, "help").perform

Send the text “help” to the currently focused element


driver.action.send_keys("help").perform

Parameters:

  • element (Selenium::WebDriver::Element)

    An optional element

  • keys (String)

    The keys to be sent.

  • device (Symbol || String) (defaults to: nil)

    optional name of the KeyInput device to send keys with

Returns:



92
93
94
95
96
97
98
99
# File 'lib/selenium/webdriver/common/interactions/key_actions.rb', line 92

def send_keys(*args, device: nil)
  click(args.shift) if args.first.is_a? Element
  args.map { |x| x.is_a?(String) ? x.chars : x }.flatten.each do |arg|
    key_down(arg, device: device)
    key_up(arg, device: device)
  end
  self
end