123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::ActionBuilder

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: rb/lib/selenium/webdriver/common/action_builder.rb

Class Method Summary

Instance Attribute Summary

WheelActions - Included

#default_scroll_duration

By default this is set to 250ms in the ActionBuilder constructor It can be overridden with default_scroll_duration=.

#default_scroll_duration=

PointerActions - Included

#default_move_duration

By default this is set to 250ms in the ActionBuilder constructor It can be overridden with default_move_duration=.

#default_move_duration=

Instance Method Summary

WheelActions - Included

#scroll_by

Scrolls by provided amounts with the origin in the top left corner of the viewport.

#scroll_from

Scrolls by provided amount based on a provided origin.

#scroll_to

If the element is outside the viewport, scrolls the bottom of the element to the bottom of the viewport.

#scroll, #wheel_input

PointerActions - Included

#click

Clicks in the middle of the given element.

#click_and_hold

Clicks (without releasing) in the middle of the given element.

#context_click

Performs a context-click at middle of the given element.

#double_click

Performs a double-click at middle of the given element.

#drag_and_drop

A convenience method that performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.

#drag_and_drop_by

A convenience method that performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.

#move_by

Moves the pointer from its current position by the given offset.

#move_to

Moves the pointer to the in-view center point of the given element.

#move_to_location

Moves the pointer to a given location in the viewport.

#pointer_down

Presses (without releasing) at the current location of the PointerInput device.

#pointer_up

Releases the pressed mouse button at the current mouse location of the PointerInput device.

#release

Releases the depressed left mouse button at the current mouse location.

#button_action, #pointer_input

KeyActions - Included

#key_down

Performs a key press.

#key_up

Performs a key release.

#send_keys

Sends keys to the active element.

#key_input, #key_action

Constructor Details

.new(bridge, 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.

  • devices (Array<Selenium::WebDriver::Interactions::InputDevices>)

    list of valid sources of input.

  • async (Boolean)

    Whether to perform the actions asynchronously per device.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 41

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

  Array(devices).each { |device| add_input(device) }
end

Instance Attribute Details

#devices (readonly)

[ GitHub ]

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

attr_reader :devices

Instance Method Details

#add_input(device) (private)

Adds an InputDevice

Raises:

  • (TypeError)
[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 237

def add_input(device)
  device = Interactions.send(device) if device.is_a?(Symbol) && Interactions.respond_to?(device)

  raise TypeError, "#{device.inspect} is not a valid InputDevice" unless device.is_a?(Interactions::InputDevice)

  unless @async
    max_device = @devices.max { |a, b| a.actions.length <=> b.actions.length }
    pauses(device: device, number: max_device.actions.length) if max_device
  end
  @devices << device
  device
end

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

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 80

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:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 64

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:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 96

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

#clear_all_actions

Clears all actions from the builder.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 207

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)

    name of the input device

  • type (String)

    name of the input device

Returns:

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 108

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

#key_inputsSelenium::WebDriver::Interactions::InputDevice

Retrieves the current KeyInput device

Returns:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 132

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

#pause(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)

    Input device to pause

  • duration (Float)

    Duration to pause

Returns:

  • (ActionBuilder)

    A self reference.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 162

def pause(device: nil, duration: 0)
  device ||= pointer_input
  device.create_pause(duration)
  self
end

#pauses(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)

    Input device to pause

  • number (Integer)

    of pauses to add for the device

  • duration (Float)

    Duration to pause

Returns:

  • (ActionBuilder)

    A self reference.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 184

def pauses(device: nil, number: nil, duration: 0)
  number ||= 2
  device ||= pointer_input
  duration ||= 0

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

#perform

Executes the actions added to the builder.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 197

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

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 122

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

#release_actions

Releases all action states from the browser.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 215

def release_actions
  @bridge.release_actions
end

#tick(*action_devices) (private)

Adds pauses for all devices but the given devices

Parameters:

  • action_devices (Array[InputDevice])

    Array of Input Devices performing an action in this tick.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 227

def tick(*action_devices)
  return if @async

  @devices.each { |device| device.create_pause unless action_devices.include? device }
end

#wheel_inputsSelenium::WebDriver::Interactions::InputDevice

Retrieves the current WheelInput device

Returns:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/action_builder.rb', line 142

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