Class: Selenium::WebDriver::W3CActionBuilder

Inherits:
Object
  • Object
show all
Includes:
KeyActions, PointerActions
Defined in:
lib/selenium/webdriver/common/w3c_action_builder.rb

Constant Summary

Constants included from PointerActions

PointerActions::DEFAULT_MOVE_DURATION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PointerActions

#click, #click_and_hold, #context_click, #double_click, #drag_and_drop, #drag_and_drop_by, #move_by, #move_to, #move_to_location, #pointer_down, #pointer_up, #release

Methods included from KeyActions

#key_down, #key_up, #send_keys

Constructor Details

#initialize(bridge, mouse, keyboard, async = false) ⇒ W3CActionBuilder

Initialize a W3C Action Builder. Differs from previous by requiring a bridge and allowing asynchronous actions. The W3C implementation allows asynchronous actions per device. e.g. A key can be pressed at the same time that the mouse is moving. Keep in mind that pauses must be added for other devices in order to line up the actions correctly when using asynchronous.

Parameters:



39
40
41
42
43
44
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 39

def initialize(bridge, mouse, keyboard, async = false)
  # For backwards compatibility, automatically include mouse & keyboard
  @bridge = bridge
  @devices = [mouse, keyboard]
  @async = async
end

Instance Attribute Details

#devicesObject (readonly)

Returns the value of attribute devices.



23
24
25
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 23

def devices
  @devices
end

Instance Method Details

#add_key_input(name) ⇒ Interactions::KeyInput

Adds a KeyInput device

Examples:

Add a key input device


builder = device.action
builder.add_key_input('keyboard2')

Parameters:

  • name (String)

    name for the device

Returns:



78
79
80
81
82
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 78

def add_key_input(name)
  new_input = Interactions.key(name)
  add_input(new_input)
  new_input
end

#add_pointer_input(kind, name) ⇒ Interactions::PointerInput

Adds a PointerInput device of the given kind

Examples:

Add a touch pointer input device


builder = device.action
builder.add_pointer_input('touch', :touch)

Parameters:

  • name (String)

    name for the device

  • kind (Symbol)

    kind of pointer device to create

Returns:



60
61
62
63
64
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 60

def add_pointer_input(kind, name)
  new_input = Interactions.pointer(kind, name: name)
  add_input(new_input)
  new_input
end

#clear_all_actionsObject

Clears all actions from the builder.



171
172
173
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 171

def clear_all_actions
  @devices.each(&:clear_actions)
end

#get_device(name) ⇒ Selenium::WebDriver::Interactions::InputDevice

Retrieves the input device for the given name

Parameters:

  • name (String)

    name of the input device

Returns:



91
92
93
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 91

def get_device(name)
  @devices.find { |device| device.name == name.to_s }
end

#key_inputsSelenium::WebDriver::Interactions::InputDevice

Retrieves the current KeyInput device

Returns:



111
112
113
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 111

def key_inputs
  @devices.select { |device| device.type == Interactions::KEY }
end

#pause(device, duration = nil) ⇒ W3CActionBuilder

Creates a pause for the given device of the given duration. If no duration is given, the pause will only wait for all actions to complete in that tick.

Examples:

Send keys to an element


action_builder = driver.action
keyboard = action_builder.key_input
el = driver.find_element(id: "some_id")
driver.action.click(el).pause(keyboard).pause(keyboard).pause(keyboard).send_keys('keys').perform

Parameters:

  • device (InputDevice)

    Input device to pause

  • duration (Float) (defaults to: nil)

    Duration to pause

Returns:



131
132
133
134
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 131

def pause(device, duration = nil)
  device.create_pause(duration)
  self
end

#pauses(device, number, duration = nil) ⇒ W3CActionBuilder

Creates multiple pauses for the given device of the given duration.

Examples:

Send keys to an element


action_builder = driver.action
keyboard = action_builder.key_input
el = driver.find_element(id: "some_id")
driver.action.click(el).pauses(keyboard, 3).send_keys('keys').perform

Parameters:

  • device (InputDevice)

    Input device to pause

  • number (Integer)

    of pauses to add for the device

  • duration (Float) (defaults to: nil)

    Duration to pause

Returns:



152
153
154
155
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 152

def pauses(device, number, duration = nil)
  number.times { device.create_pause(duration) }
  self
end

#performObject

Executes the actions added to the builder.



161
162
163
164
165
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 161

def perform
  @bridge.send_actions @devices.map(&:encode).compact
  clear_all_actions
  nil
end

#pointer_inputsArray

Retrieves the current PointerInput devices

Returns:

  • (Array)

    array of current PointerInput devices



101
102
103
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 101

def pointer_inputs
  @devices.select { |device| device.type == Interactions::POINTER }
end

#release_actionsObject

Releases all action states from the browser.



179
180
181
# File 'lib/selenium/webdriver/common/w3c_action_builder.rb', line 179

def release_actions
  @bridge.release_actions
end