class Selenium::WebDriver::ActionBuilder
Attributes
Public Class Methods
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.
@param [Selenium::WebDriver::Remote::Bridge] bridge the bridge for the current driver instance. @param [Selenium::WebDriver::Interactions::PointerInput] deprecated_mouse PointerInput for the mouse. @param [Selenium::WebDriver::Interactions::KeyInput] deprecated_keyboard KeyInput for the keyboard. @param [Boolean] deprecated_async Whether to perform the actions asynchronously per device.
Defaults to false for backwards compatibility.
@param [Array<Selenium::WebDriver::Interactions::InputDevices>] devices list of valid sources of input. @param [Boolean] async Whether to perform the actions asynchronously per device. @return [ActionBuilder] A self reference.
# File 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
Public Instance Methods
Adds a KeyInput device
@example Add a key input device
builder = device.action builder.add_key_input('keyboard2')
@param [String] name name for the device @return [Interactions::KeyInput] The key input added
# File lib/selenium/webdriver/common/action_builder.rb, line 101 def add_key_input(name) add_input(Interactions.key(name)) end
Adds a PointerInput device of the given kind
@example Add a touch pointer input device
builder = device.action builder.add_pointer_input('touch', :touch)
@param [String] name name for the device @param [Symbol] kind kind of pointer device to create @return [Interactions::PointerInput] The pointer input added
# File lib/selenium/webdriver/common/action_builder.rb, line 85 def add_pointer_input(kind, name) add_input(Interactions.pointer(kind, name: name)) end
Adds a WheelInput device
@example Add a wheel input device
builder = device.action builder.add_wheel_input('wheel2')
@param [String] name name for the device @return [Interactions::WheelInput] The wheel input added
# File lib/selenium/webdriver/common/action_builder.rb, line 117 def add_wheel_input(name) add_input(Interactions.wheel(name)) end
Clears all actions from the builder.
# File lib/selenium/webdriver/common/action_builder.rb, line 247 def clear_all_actions @devices.each(&:clear_actions) end
Retrieves the input device for the given name or type
@param [String] name name of the input device @param [String] type name of the input device @return [Selenium::WebDriver::Interactions::InputDevice] input device with given name or type
# File 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
Retrieves the input device for the given name
@param [String] name name of the input device @return [Selenium::WebDriver::Interactions::InputDevice] input device with given name
# File 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
Retrieves the current KeyInput device
@return [Selenium::WebDriver::Interactions::InputDevice] current KeyInput device
# File lib/selenium/webdriver/common/action_builder.rb, line 167 def key_inputs @devices.select { |device| device.type == Interactions::KEY } end
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.
@example 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
@param [InputDevice] device Input device to pause @param [Float] duration Duration to pause @return [ActionBuilder] A self reference.
# File 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
Creates multiple pauses for the given device of the given duration.
@example 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
@param [InputDevice] device Input device to pause @param [Integer] number of pauses to add for the device @param [Float] duration Duration to pause @return [ActionBuilder] A self reference.
# File 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
Executes the actions added to the builder.
# File lib/selenium/webdriver/common/action_builder.rb, line 237 def perform @bridge.send_actions @devices.filter_map(&:encode) clear_all_actions nil end
Retrieves the current PointerInput devices
@return [Array] array of current PointerInput devices
# File lib/selenium/webdriver/common/action_builder.rb, line 157 def pointer_inputs @devices.select { |device| device.type == Interactions::POINTER } end
Releases all action states from the browser.
# File lib/selenium/webdriver/common/action_builder.rb, line 255 def release_actions @bridge.release_actions end
Retrieves the current WheelInput device
@return [Selenium::WebDriver::Interactions::InputDevice] current WheelInput devices
# File lib/selenium/webdriver/common/action_builder.rb, line 177 def wheel_inputs @devices.select { |device| device.type == Interactions::WHEEL } end
Private Instance Methods
Adds an InputDevice
# File lib/selenium/webdriver/common/action_builder.rb, line 277 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
# File lib/selenium/webdriver/common/action_builder.rb, line 290 def deprecate_method(device = nil, duration = nil, number = nil, method: :pause) return unless device || number || duration WebDriver.logger.deprecate "ActionBuilder##{method} with ordered parameters", ':device, :duration, :number keywords', id: method end
Adds pauses for all devices but the given devices
@param [Array] action_devices Array of Input Devices performing an action in this tick.
# File lib/selenium/webdriver/common/action_builder.rb, line 267 def tick(*action_devices) return if @async @devices.each { |device| device.create_pause unless action_devices.include? device } end