Class: Selenium::WebDriver::ActionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/common/action_builder.rb

Overview

The ActionBuilder provides the user a way to set up and perform complex user interactions.

This class should not be instantiated directly, but is created by Selenium::WebDriver::DriverExtensions::HasInputDevices#action, which is available on Driver instances that support the user interaction API.

Examples:


driver.action.key_down(:shift).
              click(element).
              click(second_element).
              key_up(:shift).
              drag_and_drop(element, third_element).
              perform

Direct Known Subclasses

TouchActionBuilder

Instance Method Summary collapse

Constructor Details

#initialize(mouse, keyboard) ⇒ ActionBuilder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ActionBuilder.



45
46
47
48
49
50
51
52
# File 'lib/selenium/webdriver/common/action_builder.rb', line 45

def initialize(mouse, keyboard)
  @devices = {
    mouse: mouse,
    keyboard: keyboard
  }

  @actions = []
end

Instance Method Details

#click(element = nil) ⇒ ActionBuilder

Clicks in the middle of the given element. Equivalent to:

driver.action.move_to(element).click

When no element is passed, the current mouse position will be clicked.

Examples:

Clicking on an element


el = driver.find_element(id: "some_id")
driver.action.click(el).perform

Clicking at the current mouse position


driver.action.click.perform

Parameters:

Returns:



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

def click(element = nil)
  @actions << [:mouse, :click, [element]]
  self
end

#click_and_hold(element = nil) ⇒ ActionBuilder

Clicks (without releasing) in the middle of the given element. This is equivalent to:

driver.action.move_to(element).click_and_hold

Examples:

Clicking and holding on some element


el = driver.find_element(id: "some_id")
driver.action.click_and_hold(el).perform

Parameters:

Returns:



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

def click_and_hold(element = nil)
  @actions << [:mouse, :down, [element]]
  self
end

#context_click(element = nil) ⇒ ActionBuilder

Performs a context-click at middle of the given element. First performs a move_to to the location of the element.

Examples:

Context-click at middle of given element


el = driver.find_element(id: "some_id")
driver.action.context_click(el).perform

Parameters:

Returns:



293
294
295
296
# File 'lib/selenium/webdriver/common/action_builder.rb', line 293

def context_click(element = nil)
  @actions << [:mouse, :context_click, [element]]
  self
end

#double_click(element = nil) ⇒ ActionBuilder

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

driver.action.move_to(element).double_click

Examples:

Double click an element


el = driver.find_element(id: "some_id")
driver.action.double_click(el).perform

Parameters:

Returns:



216
217
218
219
# File 'lib/selenium/webdriver/common/action_builder.rb', line 216

def double_click(element = nil)
  @actions << [:mouse, :double_click, [element]]
  self
end

#drag_and_drop(source, target) ⇒ ActionBuilder

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.

Examples:

Drag and drop one element onto another


el1 = driver.find_element(id: "some_id1")
el2 = driver.find_element(id: "some_id2")
driver.action.drag_and_drop(el1, el2).perform

Parameters:

Returns:



315
316
317
318
319
320
321
# File 'lib/selenium/webdriver/common/action_builder.rb', line 315

def drag_and_drop(source, target)
  click_and_hold source
  move_to        target
  release        target

  self
end

#drag_and_drop_by(source, right_by, down_by) ⇒ ActionBuilder

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

Examples:

Drag and drop an element by offset


el = driver.find_element(id: "some_id1")
driver.action.drag_and_drop_by(el, 100, 100).perform

Parameters:

Returns:



340
341
342
343
344
345
346
# File 'lib/selenium/webdriver/common/action_builder.rb', line 340

def drag_and_drop_by(source, right_by, down_by)
  click_and_hold source
  move_by        right_by, down_by
  release

  self
end

#key_down(*args) ⇒ ActionBuilder

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

Equivalent to:

driver.action.click(element).send_keys(key)
# or
driver.action.click.send_keys(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:

Returns:

Raises:

  • (ArgumentError)

    if the given key is not a modifier



80
81
82
83
84
85
# File 'lib/selenium/webdriver/common/action_builder.rb', line 80

def key_down(*args)
  @actions << [:mouse, :click, [args.shift]] if args.first.is_a? Element

  @actions << [:keyboard, :press, args]
  self
end

#key_up(*args) ⇒ ActionBuilder

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:

Returns:

Raises:

  • (ArgumentError)

    if the given key is not a modifier key



106
107
108
109
110
111
# File 'lib/selenium/webdriver/common/action_builder.rb', line 106

def key_up(*args)
  @actions << [:mouse, :click, [args.shift]] if args.first.is_a? Element

  @actions << [:keyboard, :release, args]
  self
end

#move_by(right_by, down_by) ⇒ ActionBuilder

Moves the mouse from its current position (or 0,0) by the given offset. If the coordinates provided are outside the viewport (the mouse will end up outside the browser window) then the viewport is scrolled to match.

Examples:

Move the mouse to a certain offset from its current position


driver.action.move_by(100, 100).perform

Parameters:

  • right_by (Integer)

    horizontal offset. A negative value means moving the mouse left.

  • down_by (Integer)

    vertical offset. A negative value means moving the mouse up.

Returns:

Raises:

  • (MoveTargetOutOfBoundsError)

    if the provided offset is outside the document’s boundaries.



275
276
277
278
# File 'lib/selenium/webdriver/common/action_builder.rb', line 275

def move_by(right_by, down_by)
  @actions << [:mouse, :move_by, [Integer(right_by), Integer(down_by)]]
  self
end

#move_to(element, right_by = nil, down_by = nil) ⇒ ActionBuilder

Moves the mouse to the middle of the given element. The element is scrolled into view and its location is calculated using getBoundingClientRect. Then the mouse is moved to optional offset coordinates from the element.

Note that when using offsets, both coordinates need to be passed.

Examples:

Scroll element into view and move the mouse to it


el = driver.find_element(id: "some_id")
driver.action.move_to(el).perform

el = driver.find_element(id: "some_id")
driver.action.move_to(el, 100, 100).perform

Parameters:

  • element (Selenium::WebDriver::Element)

    to move to.

  • right_by (Integer) (defaults to: nil)

    Optional offset from the top-left corner. A negative value means coordinates right from the element.

  • down_by (Integer) (defaults to: nil)

    Optional offset from the top-left corner. A negative value means coordinates above the element.

Returns:



246
247
248
249
250
251
252
253
254
# File 'lib/selenium/webdriver/common/action_builder.rb', line 246

def move_to(element, right_by = nil, down_by = nil)
  @actions << if right_by && down_by
                [:mouse, :move_to, [element, Integer(right_by), Integer(down_by)]]
              else
                [:mouse, :move_to, [element]]
              end

  self
end

#performObject

Executes the actions added to the builder.



352
353
354
355
356
357
358
# File 'lib/selenium/webdriver/common/action_builder.rb', line 352

def perform
  @actions.each do |receiver, method, args|
    @devices.fetch(receiver).__send__(method, *args)
  end

  nil
end

#release(element = nil) ⇒ ActionBuilder

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

Examples:

Releasing an element after clicking and holding it


el = driver.find_element(id: "some_id")
driver.action.click_and_hold(el).release.perform

Returns:



172
173
174
175
# File 'lib/selenium/webdriver/common/action_builder.rb', line 172

def release(element = nil)
  @actions << [:mouse, :up, [element]]
  self
end

#send_keys(*args) ⇒ ActionBuilder

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:

Returns:



134
135
136
137
138
139
# File 'lib/selenium/webdriver/common/action_builder.rb', line 134

def send_keys(*args)
  @actions << [:mouse, :click, [args.shift]] if args.first.is_a? Element

  @actions << [:keyboard, :send_keys, args]
  self
end