Class: Webrat::SeleniumSession

Inherits:
Object
  • Object
show all
Includes:
SaveAndOpenPage, Webrat::Selenium::SilenceStream
Defined in:
lib/webrat/selenium/selenium_session.rb

Constant Summary collapse

WAIT_TIMEOUT =
5

Instance Method Summary collapse

Methods included from Webrat::Selenium::SilenceStream

#silence_stream

Methods included from SaveAndOpenPage

#doc_root, #open_in_browser, #rewrite_css_and_image_references, #save_and_open_page, #saved_page_dir

Constructor Details

#initialize(*args) ⇒ SeleniumSession

:nodoc:



32
33
# File 'lib/webrat/selenium/selenium_session.rb', line 32

def initialize(*args) # :nodoc:
end

Instance Method Details

#automateObject



38
39
40
# File 'lib/webrat/selenium/selenium_session.rb', line 38

def automate
  yield
end

#check(label_text) ⇒ Object Also known as: uncheck



128
129
130
131
132
# File 'lib/webrat/selenium/selenium_session.rb', line 128

def check(label_text)
  locator = "webrat=#{label_text}"
  selenium.wait_for_element locator, :timeout_in_seconds => WAIT_TIMEOUT
  selenium.click locator
end

#choose(label_text) ⇒ Object



120
121
122
123
124
# File 'lib/webrat/selenium/selenium_session.rb', line 120

def choose(label_text)
  locator = "webrat=#{label_text}"
  selenium.wait_for_element locator, :timeout_in_seconds => WAIT_TIMEOUT
  selenium.click locator
end

#click_button(button_text_or_regexp = nil, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/webrat/selenium/selenium_session.rb', line 68

def click_button(button_text_or_regexp = nil, options = {})
  if button_text_or_regexp.is_a?(Hash) && options == {}
    pattern, options = nil, button_text_or_regexp
  elsif button_text_or_regexp
    pattern = adjust_if_regexp(button_text_or_regexp)
  end
  pattern ||= '*'
  locator = "button=#{pattern}"

  selenium.wait_for_element locator, :timeout_in_seconds => WAIT_TIMEOUT
  selenium.click locator
end


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/webrat/selenium/selenium_session.rb', line 83

def click_link(link_text_or_regexp, options = {})
  if link_text_or_regexp.is_a?(Regexp)
    pattern = "evalregex:#{link_text_or_regexp.inspect}"
  else
    pattern = link_text_or_regexp.to_s
  end

  locator = "webratlink=#{pattern}"
  selenium.wait_for_element locator, :timeout_in_seconds => WAIT_TIMEOUT
  selenium.click locator
end


97
98
99
100
101
# File 'lib/webrat/selenium/selenium_session.rb', line 97

def click_link_within(selector, link_text, options = {})
  locator = "webratlinkwithin=#{selector}|#{link_text}"
  selenium.wait_for_element locator, :timeout_in_seconds => WAIT_TIMEOUT
  selenium.click locator
end

#current_urlObject



64
65
66
# File 'lib/webrat/selenium/selenium_session.rb', line 64

def current_url
  selenium.location
end

#fill_in(field_identifier, options) ⇒ Object



48
49
50
51
52
# File 'lib/webrat/selenium/selenium_session.rb', line 48

def fill_in(field_identifier, options)
  locator = "webrat=#{field_identifier}"
  selenium.wait_for_element locator, :timeout_in_seconds => WAIT_TIMEOUT
  selenium.type(locator, "#{options[:with]}")
end

#fire_event(field_identifier, event) ⇒ Object



137
138
139
140
# File 'lib/webrat/selenium/selenium_session.rb', line 137

def fire_event(field_identifier, event)
  locator = "webrat=#{Regexp.escape(field_identifier)}"
  selenium.fire_event(locator, "#{event}")
end

#key_down(field_identifier, key_code) ⇒ Object



142
143
144
145
# File 'lib/webrat/selenium/selenium_session.rb', line 142

def key_down(field_identifier, key_code)
  locator = "webrat=#{Regexp.escape(field_identifier)}"
  selenium.key_down(locator, key_code)
end

#key_up(field_identifier, key_code) ⇒ Object



147
148
149
150
# File 'lib/webrat/selenium/selenium_session.rb', line 147

def key_up(field_identifier, key_code)
  locator = "webrat=#{Regexp.escape(field_identifier)}"
  selenium.key_up(locator, key_code)
end

#responseObject



56
57
58
# File 'lib/webrat/selenium/selenium_session.rb', line 56

def response
  SeleniumResponse.new(self, response_body)
end

#response_bodyObject

:nodoc:



60
61
62
# File 'lib/webrat/selenium/selenium_session.rb', line 60

def response_body #:nodoc:
  selenium.get_html_source
end

#save_and_open_screengrabObject



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/webrat/selenium/selenium_session.rb', line 199

def save_and_open_screengrab
  return unless File.exist?(saved_page_dir)

  filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.png"

  if $browser.chrome_backend?
    $browser.capture_entire_page_screenshot(filename, '')
  else
    $browser.capture_screenshot(filename)
  end
  open_in_browser(filename)

end

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



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/webrat/selenium/selenium_session.rb', line 105

def select(option_text, options = {})
  id_or_name_or_label = options[:from]

  if id_or_name_or_label
    select_locator = "webrat=#{id_or_name_or_label}"
  else
    select_locator = "webratselectwithoption=#{option_text}"
  end

  selenium.wait_for_element select_locator, :timeout_in_seconds => WAIT_TIMEOUT
  selenium.select(select_locator, option_text)
end

#seleniumObject



190
191
192
193
194
# File 'lib/webrat/selenium/selenium_session.rb', line 190

def selenium
  return $browser if $browser
  setup
  $browser
end

#simulateObject



35
36
# File 'lib/webrat/selenium/selenium_session.rb', line 35

def simulate
end

#visit(url) ⇒ Object



42
43
44
# File 'lib/webrat/selenium/selenium_session.rb', line 42

def visit(url)
  selenium.open(url)
end

#wait_for(params = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/webrat/selenium/selenium_session.rb', line 152

def wait_for(params={})
  timeout = params[:timeout] || 5
  message = params[:message] || "Timeout exceeded"

  begin_time = Time.now

  while (Time.now - begin_time) < timeout
    value = nil

    begin
      value = yield
    rescue Exception => e
      unless is_ignorable_wait_for_exception?(e)
        raise e
      end
    end

    return value if value

    sleep 0.25
  end

  error_message = "#{message} (after #{timeout} sec)"

  if $browser
    error_message += <<-EOS


HTML of the page was:

#{selenium.get_html_source}"
EOS
  end

  raise Webrat::TimeoutError.new(error_message)
  true
end