Module: RWebUnit::Driver

Included in:
AbstractWebPage, RSpecHelper, WebTestCase
Defined in:
lib/rwebunit/driver.rb

Instance Method Summary collapse

Instance Method Details

#ajax_wait_for_element(element_id, seconds, status = 'show', check_interval = 2) ⇒ Object

Wait for specific seconds for an Ajax update finish. Trick: In your Rails application,

  :loading 	=> "Element.show('search_indicator');
  :complete	=> "Element.hide('search_indicator');

<%= image_tag("indicator.gif", :id => 'search_indicator', :style => 'display:none') %>

Typical usage:

ajax_wait_for_element("search_indicator", 30)
ajax_wait_for_element("search_indicator", 30, "show")
ajax_wait_for_element("search_indicator", 30, "hide")
ajax_wait_for_element("search_indicator", 30, "show", 5) # check every 5 seconds

Warning: this method has not been fully tested, if you are not using Rails, change your parameter accordingly.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rwebunit/driver.rb', line 215

def ajax_wait_for_element(element_id, seconds, status='show', check_interval=2)
  count = 0
  check_interval = 2 if check_interval < 1 or check_interval > seconds
  while count < (seconds / check_interval) do
    search_indicator = @web_tester.element_by_id(element_id)
    search_indicator_outer_html = search_indicator.outerHtml if search_indicator
    if status == 'hide'
      return true if search_indicator_outer_html and !search_indicator_outer_html.include?('style="DISPLAY: none"')
    else
      return true if search_indicator_outer_html and search_indicator_outer_html.include?('style="DISPLAY: none"')
    end
    sleep check_interval if check_interval > 0 and check_interval < 5 * 60 # wait max 5 minutes
    count += 1
  end
  return false
end

#allow(&block) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/rwebunit/driver.rb', line 49

def allow(&block)
  operation_performed_ok = false
  begin
    yield
    operation_performed_ok  = true
  rescue
  end
  operation_performed_ok
end

#attach_browser(how, what) ⇒ Object



100
101
102
# File 'lib/rwebunit/driver.rb', line 100

def attach_browser(how, what)
  WebTester.attach_browser(how, what)
end

#begin_at(url) ⇒ Object



75
76
77
# File 'lib/rwebunit/driver.rb', line 75

def begin_at(url)
  @web_tester.begin_at(url)
end

#click_button_with_image_src_contains(image_filename) ⇒ Object Also known as: click_button_with_image



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rwebunit/driver.rb', line 181

def click_button_with_image_src_contains(image_filename)
  operation_delay
  found = nil
  raise "no buttons in this page" if buttons.length <= 0
  buttons.each { |btn|
    if btn && btn.src  && btn.src.include?(image_filename) then
      found = btn
      break
    end
  }
  raise "not image button with src: #{image_filename} found" if found.nil?
  found.click
end

#click_popup_window(button, wait_time = 9, user_input = nil) ⇒ Object



262
263
264
265
# File 'lib/rwebunit/driver.rb', line 262

def click_popup_window(button, wait_time= 9, user_input=nil )
  @web_tester.start_clicker(button, wait_time, user_input)
  sleep 0.5
end

#close_browserObject Also known as: close_ie



83
84
85
# File 'lib/rwebunit/driver.rb', line 83

def close_browser
  @web_tester.close_browser unless ENV['ITEST_LEAVE_BROWSER_OPEN_AFTER_RUN'] == "true"
end

#contains_text(text) ⇒ Object



153
154
155
# File 'lib/rwebunit/driver.rb', line 153

def contains_text(text)
  @web_tester.contains_text(text);
end

#contextObject



71
72
73
# File 'lib/rwebunit/driver.rb', line 71

def context
  @web_tester.context
end

#dump_response(stream = nil) ⇒ Object


For debugging




251
252
253
# File 'lib/rwebunit/driver.rb', line 251

def dump_response(stream = nil)
  @web_tester.dump_response(stream)
end

#element_text(elem_id) ⇒ Object

Warning: this does not work well with Firefox yet.



243
244
245
# File 'lib/rwebunit/driver.rb', line 243

def element_text(elem_id)
  @web_tester.element_value(elem_id)
end

#expect_page(page_clazz) ⇒ Object

Verify the next page following an operation.

Typical usage:

.
expect_page HomePage


18
19
20
# File 'lib/rwebunit/driver.rb', line 18

def expect_page(page_clazz)
  page_clazz.new(@web_tester)
end

#failsafe(&block) ⇒ Object Also known as: fail_safe

try operation, ignore if errors occur

Example:

failsafe { click_link("Logout") }  # try logout, but it still OK if not being able to (already logout))


63
64
65
66
67
68
# File 'lib/rwebunit/driver.rb', line 63

def failsafe(&block)
  begin
    yield
  rescue =>e
  end
end

#goto_page(page) ⇒ Object



95
96
97
98
# File 'lib/rwebunit/driver.rb', line 95

def goto_page(page)
  operation_delay
  @web_tester.goto_page(page);
end

#ieObject



79
80
81
# File 'lib/rwebunit/driver.rb', line 79

def ie
  @web_tester.ie
end

#new_popup_window(options) ⇒ Object



196
197
198
# File 'lib/rwebunit/driver.rb', line 196

def new_popup_window(options)
  @web_tester.new_popup_window(options)
end

#on(page) {|page| ... } ⇒ Object

Example:

on @page do |i|
  i.enter_text('btn1')
  i.click_button('btn1')
end

Yields:

  • (page)


30
31
32
# File 'lib/rwebunit/driver.rb', line 30

def on(page, &block)
  yield page
end

#operation_delayObject

Support of iTest to ajust the intervals between keystroke/mouse operations



268
269
270
271
272
273
274
275
276
# File 'lib/rwebunit/driver.rb', line 268

def operation_delay
  begin
    if ENV['ITEST_OPERATION_DELAY']  && ENV['ITEST_OPERATION_DELAY'].to_i > 0 && ENV['ITEST_OPERATION_DELAY'].to_f < 30000  then # max 30 seconds
      sleep(ENV['ITEST_OPERATION_DELAY'].to_f / 1000)
    end
  rescue => e
    # ignore
  end
end

#save_current_page(to_dir = ENV['TEMP_DIR'] || "C:\\temp") ⇒ Object



255
256
257
258
259
260
# File 'lib/rwebunit/driver.rb', line 255

def save_current_page(to_dir = ENV['TEMP_DIR'] || "C:\\temp")
  Dir.mkdir(to_dir) unless File.exists?(to_dir)
  file_name = Time.now.strftime("%m%d%H%M%S") + ".html"
  file = File.join(to_dir, file_name)
  File.new(file, "w").puts page_source
end

#shall_not_allow(&block) ⇒ Object Also known as: do_not_allow

fail the test if user can perform the operation

Example:

shall_not_allow { 1/0 }


38
39
40
41
42
43
44
45
46
# File 'lib/rwebunit/driver.rb', line 38

def shall_not_allow(&block)
  operation_performed_ok = false
  begin
    yield
    operation_performed_ok  = true
  rescue
  end
  raise "Operation shall not be allowed" if operation_performed_ok
end

#wait_for_element(element_id, timeout = 30, interval = 0.5) ⇒ Object



232
233
234
235
236
237
238
239
240
# File 'lib/rwebunit/driver.rb', line 232

def wait_for_element(element_id, timeout = 30, interval = 0.5)
  start_time = Time.now
  until @web_tester.element_by_id(element_id) do
    sleep(interval)
    if (Time.now - start_time) > timeout
      raise RuntimeError, "failed to find element: #{element_id} for max #{timeout}"
    end
  end
end