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.

Example:

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

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



28
29
30
31
32
33
34
35
# File 'lib/selenium/webdriver/common/action_builder.rb', line 28

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

  @actions  = []
end

Instance Method Details

#click(element = nil) ⇒ Object



74
75
76
77
# File 'lib/selenium/webdriver/common/action_builder.rb', line 74

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

#click_and_hold(element) ⇒ Object



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

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

#context_click(element = nil) ⇒ Object



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

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

#double_click(element = nil) ⇒ Object



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

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

#drag_and_drop(source, target) ⇒ Object



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

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) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/selenium/webdriver/common/action_builder.rb', line 112

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) ⇒ Object



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

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

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

#key_up(*args) ⇒ Object



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

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

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

#move_by(right_by, down_by) ⇒ Object



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

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

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



84
85
86
87
88
89
90
91
92
# File 'lib/selenium/webdriver/common/action_builder.rb', line 84

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

  self
end

#performObject



120
121
122
123
124
# File 'lib/selenium/webdriver/common/action_builder.rb', line 120

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

#release(element = nil) ⇒ Object



69
70
71
72
# File 'lib/selenium/webdriver/common/action_builder.rb', line 69

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

#send_keys(*args) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/selenium/webdriver/common/action_builder.rb', line 55

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

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