Module: Steam::Browser::HtmlUnit::Actions

Included in:
Steam::Browser::HtmlUnit
Defined in:
lib/steam/browser/html_unit/actions.rb

Constant Summary collapse

DATE_TIME_CODE =

Rails specific respond_tos

{
  :year   => '1i',
  :month  => '2i',
  :day    => '3i',
  :hour   => '4i',
  :minute => '5i',
  :second => '6i'
}

Instance Method Summary collapse

Instance Method Details

#attach_file(element, path, options = {}) ⇒ Object

TODO implement a way to supply content_type



61
62
63
64
65
66
# File 'lib/steam/browser/html_unit/actions.rb', line 61

def attach_file(element, path, options = {})
  respond_to do
    element = locate_in_browser(:file, element, options)
    element.setValueAttribute(path)
  end
end

#blur(element, options = {}) ⇒ Object



107
108
109
110
111
112
# File 'lib/steam/browser/html_unit/actions.rb', line 107

def blur(element, options = {})
  respond_to do
    locate_in_browser(element, options).blur
    @page # blur seems to return nil
  end
end

#check(element, options = {}) ⇒ Object



36
37
38
# File 'lib/steam/browser/html_unit/actions.rb', line 36

def check(element, options = {})
  respond_to { locate_in_browser(:check_box, element, options).setChecked(true) }
end

#choose(element, options = {}) ⇒ Object



44
45
46
# File 'lib/steam/browser/html_unit/actions.rb', line 44

def choose(element, options = {})
  respond_to { locate_in_browser(:radio_button, element, options).setChecked(true) }
end

#click_area(element, options = {}) ⇒ Object



22
23
24
# File 'lib/steam/browser/html_unit/actions.rb', line 22

def click_area(element, options = {})
  respond_to { locate_in_browser(:area, element, options).click }
end

#click_button(element, options = {}) ⇒ Object



18
19
20
# File 'lib/steam/browser/html_unit/actions.rb', line 18

def click_button(element, options = {})
  respond_to { locate_in_browser(:button, element, options).click }
end


14
15
16
# File 'lib/steam/browser/html_unit/actions.rb', line 14

def click_link(element, options = {})
  respond_to { locate_in_browser(:link, element, options).click }
end

#click_on(*args) ⇒ Object



10
11
12
# File 'lib/steam/browser/html_unit/actions.rb', line 10

def click_on(*args)
  respond_to { locate_in_browser(*args).click }
end

#double_click(element, options = {}) ⇒ Object



121
122
123
# File 'lib/steam/browser/html_unit/actions.rb', line 121

def double_click(element, options = {})
  respond_to { locate_in_browser(element, options).dblClick }
end

#drag(element, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/steam/browser/html_unit/actions.rb', line 80

def drag(element, options = {})
  respond_to do
    target  = extract_drop_target!(options, [:to, :onto, :over, :target])
    element = locate_in_browser(element, options)
    if target
      element.mouseDown
      @_drop_target = locate_in_browser(target)
      @_drop_target.mouseMove
    else
      element.mouseDown
    end
  end
end

#drag_and_drop(element, options = {}) ⇒ Object



75
76
77
78
# File 'lib/steam/browser/html_unit/actions.rb', line 75

def drag_and_drop(element, options = {})
  drag(element, options)
  drop
end

#drop(element = nil, options = {}) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/steam/browser/html_unit/actions.rb', line 94

def drop(element = nil, options = {})
  respond_to do
    element = @_drop_target || locate_in_browser(element, options)
    element.mouseMove unless @_drop_target
    @_drop_target = nil
    element.mouseUp
  end
end

#fill_in(element, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/steam/browser/html_unit/actions.rb', line 26

def fill_in(element, options = {})
  respond_to do
    value = options.delete(:with)
    element = locate_in_browser(:field, element, options)
    result = element.setText(value) rescue element.setValueAttribute(value)
    # TODO - submit a bug: element.setText returns nil, textarea.setValueAttribute returns a page
    result || page
  end
end

#focus(element, options = {}) ⇒ Object



114
115
116
117
118
119
# File 'lib/steam/browser/html_unit/actions.rb', line 114

def focus(element, options = {})
  respond_to do
    locate_in_browser(element, options).focus
    @page # focus seems to return nil
  end
end

#hover(element, options = {}) ⇒ Object



103
104
105
# File 'lib/steam/browser/html_unit/actions.rb', line 103

def hover(element, options = {})
  respond_to { locate_in_browser(element, options).mouseOver }
end

#select(element, options = {}) ⇒ Object



48
49
50
51
# File 'lib/steam/browser/html_unit/actions.rb', line 48

def select(element, options = {})
  options.update(:within => [:select, options.delete(:from)])
  respond_to { locate_in_browser(:select_option, element, options).setSelected(true) }
end

#select_date(date, options = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/steam/browser/html_unit/actions.rb', line 135

def select_date(date, options = {})
  date = date.respond_to?(:strftime) ? date : Date.parse(date)
  prefix = locate_id_prefix(options)

  # FIXME .to_s chould be done somewhere inside the locator
  select(date.year.to_s,      :from => "#{prefix}_#{DATE_TIME_CODE[:year]}")
  select(date.strftime('%B'), :from => "#{prefix}_#{DATE_TIME_CODE[:month]}")
  select(date.day.to_s,       :from => "#{prefix}_#{DATE_TIME_CODE[:day]}")
end

#select_datetime(datetime, options = {}) ⇒ Object



145
146
147
148
# File 'lib/steam/browser/html_unit/actions.rb', line 145

def select_datetime(datetime, options = {})
  select_date(datetime)
  select_time(datetime)
end

#select_time(time, options = {}) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/steam/browser/html_unit/actions.rb', line 150

def select_time(time, options = {})
  time = time.respond_to?(:strftime) ? time : DateTime.parse(time)
  prefix = locate_id_prefix(options)

  select(time.hour.to_s.rjust(2,'0'), :from => "#{prefix}_#{DATE_TIME_CODE[:hour]}")
  select(time.min.to_s.rjust(2,'0'),  :from => "#{prefix}_#{DATE_TIME_CODE[:minute]}")
  # second?
end

#set_hidden_field(element, options = {}) ⇒ Object



53
54
55
56
57
58
# File 'lib/steam/browser/html_unit/actions.rb', line 53

def set_hidden_field(element, options = {})
  respond_to do
    value = options.delete(:to)
    locate_in_browser(:hidden_field, element, options).setValueAttribute(value)
  end
end

#submit_form(element, options = {}) ⇒ Object



68
69
70
71
72
73
# File 'lib/steam/browser/html_unit/actions.rb', line 68

def submit_form(element, options = {})
  respond_to do
    scope = [:form, element, options]
    locate_in_browser(:input, :type => 'submit', :within => scope).click
  end
end

#uncheck(element, options = {}) ⇒ Object



40
41
42
# File 'lib/steam/browser/html_unit/actions.rb', line 40

def uncheck(element, options = {})
  respond_to { locate_in_browser(:check_box, element, options).setChecked(false) }
end