Class: Selenium::WebDriver::ActionBuilder

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

Instance Attribute Summary collapse

Attributes included from WheelActions

#default_scroll_duration

Attributes included from PointerActions

#default_move_duration

Instance Method Summary collapse

Methods included from WheelActions

#scroll_by, #scroll_from, #scroll_to

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, deprecated_mouse = nil, deprecated_keyboard = nil, deprecated_async = nil, devices: [], async: false, duration: 250) ⇒ ActionBuilder

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:

  • bridge (Selenium::WebDriver::Remote::Bridge)

    the bridge for the current driver instance.

  • deprecated_mouse (Selenium::WebDriver::Interactions::PointerInput) (defaults to: nil)

    PointerInput for the mouse.

  • deprecated_keyboard (Selenium::WebDriver::Interactions::KeyInput) (defaults to: nil)

    KeyInput for the keyboard.

  • deprecated_async (Boolean) (defaults to: nil)

    Whether to perform the actions asynchronously per device. Defaults to false for backwards compatibility.

  • devices (Array<Selenium::WebDriver::Interactions::InputDevices>) (defaults to: [])

    list of valid sources of input.

  • async (Boolean) (defaults to: false)

    Whether to perform the actions asynchronously per device.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 45

def initialize(bridge, deprecated_mouse = nil, deprecated_keyboard = nil, deprecated_async = nil,
               devices: [], async: false, duration: 250)
  @bridge = bridge
  @duration = duration

  @async = if deprecated_async.nil?
             async
           else
             WebDriver.logger.deprecate('initializing ActionBuilder with async parameter',
                                        ':async keyword',
                                        id: :action_async)
             deprecated_async
           end

  @devices = []
  if deprecated_keyboard || deprecated_mouse
    WebDriver.logger.deprecate "initializing ActionBuilder with keyboard and mouse parameters",
                               "devices keyword or, even better, Driver#action",
                               id: :action_devices
    add_input(deprecated_mouse)
    add_input(deprecated_keyboard)
  else
    Array(devices).each { |device| add_input(device) }
  end
end

Instance Attribute Details

#devicesObject (readonly)

Returns the value of attribute devices.



27
28
29
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 27

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:



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

def add_key_input(name)
  add_input(Interactions.key(name))
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:



85
86
87
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 85

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

#add_wheel_input(name) ⇒ Interactions::WheelInput

Adds a WheelInput device

Examples:

Add a wheel input device


builder = device.action
builder.add_wheel_input('wheel2')

Parameters:

  • name (String)

    name for the device

Returns:



117
118
119
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 117

def add_wheel_input(name)
  add_input(Interactions.wheel(name))
end

#clear_all_actionsObject

Clears all actions from the builder.



247
248
249
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 247

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

#device(name: nil, type: nil) ⇒ Selenium::WebDriver::Interactions::InputDevice

Retrieves the input device for the given name or type

Parameters:

  • name (String) (defaults to: nil)

    name of the input device

  • type (String) (defaults to: nil)

    name of the input device

Returns:

Raises:

  • (ArgumentError)


143
144
145
146
147
148
149
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 143

def device(name: nil, type: nil)
  input = @devices.find { |device| (device.name == name.to_s || name.nil?) && (device.type == type || type.nil?) }

  raise(ArgumentError, "Can not find device: #{name}") if name && input.nil?

  input
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:



128
129
130
131
132
133
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 128

def get_device(name)
  WebDriver.logger.deprecate('#get_device with name parameter',
                             '#device with :name or :type keyword',
                             id: :get_device)
  device(name: name)
end

#key_inputsSelenium::WebDriver::Interactions::InputDevice

Retrieves the current KeyInput device

Returns:



167
168
169
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 167

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

#pause(deprecated_device = nil, deprecated_duration = nil, device: nil, duration: 0) ⇒ ActionBuilder

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) (defaults to: nil)

    Input device to pause

  • duration (Float) (defaults to: 0)

    Duration to pause

Returns:



197
198
199
200
201
202
203
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 197

def pause(deprecated_device = nil, deprecated_duration = nil, device: nil, duration: 0)
  deprecate_method(deprecated_device, deprecated_duration)

  device ||= deprecated_device || pointer_input
  device.create_pause(deprecated_duration || duration)
  self
end

#pauses(deprecated_device = nil, deprecated_number = nil, deprecated_duration = nil, device: nil, number: nil, duration: 0) ⇒ ActionBuilder

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) (defaults to: nil)

    Input device to pause

  • number (Integer) (defaults to: nil)

    of pauses to add for the device

  • duration (Float) (defaults to: 0)

    Duration to pause

Returns:



221
222
223
224
225
226
227
228
229
230
231
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 221

def pauses(deprecated_device = nil, deprecated_number = nil, deprecated_duration = nil,
           device: nil, number: nil, duration: 0)
  deprecate_method(deprecated_device, deprecated_duration, deprecated_number, method: :pauses)

  number ||= deprecated_number || 2
  device ||= deprecated_device || pointer_input
  duration ||= deprecated_duration || 0

  number.times { device.create_pause(duration) }
  self
end

#performObject

Executes the actions added to the builder.



237
238
239
240
241
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 237

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

#pointer_inputsArray

Retrieves the current PointerInput devices

Returns:

  • (Array)

    array of current PointerInput devices



157
158
159
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 157

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

#release_actionsObject

Releases all action states from the browser.



255
256
257
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 255

def release_actions
  @bridge.release_actions
end

#wheel_inputsSelenium::WebDriver::Interactions::InputDevice

Retrieves the current WheelInput device

Returns:



177
178
179
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 177

def wheel_inputs
  @devices.select { |device| device.type == Interactions::WHEEL }
end