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.



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rwebunit/driver.rb', line 358

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

#area(*args) ⇒ Object

area <area> tags button <input> tags with type=button, submit, image or reset check_box <input> tags with type=checkbox div <div> tags form <form> tags frame frames, including both the <frame> elements and the corresponding pages h1 - h6 <h1>, <h2>, <h3>, <h4>, <h5>, <h6> tags hidden <input> tags with type=hidden image <img> tags label <label> tags (including “for” attribute) li <li> tags link <a> (anchor) tags map <map> tags radio <input> tags with the type=radio; known as radio buttons select_list <select> tags, known as drop-downs or drop-down lists span <span> tags table <table> tags, including row and cell methods for accessing nested elements text_field <input> tags with the type=text (single-line), type=textarea (multi-line), and type=password p <p> (paragraph) tags, because



112
113
114
# File 'lib/rwebunit/driver.rb', line 112

def area(*args)
  @web_tester.area(*args);
end

#attach_browser(how, what) ⇒ Object



68
69
70
# File 'lib/rwebunit/driver.rb', line 68

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

#begin_at(url) ⇒ Object



38
39
40
# File 'lib/rwebunit/driver.rb', line 38

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

#button(*args) ⇒ Object



115
116
117
# File 'lib/rwebunit/driver.rb', line 115

def button(*args)
  @web_tester.button(*args);
end

#buttonsObject



215
216
217
# File 'lib/rwebunit/driver.rb', line 215

def buttons;
  @web_tester.buttons;
end

#cell(*args) ⇒ Object Also known as: td



118
119
120
# File 'lib/rwebunit/driver.rb', line 118

def cell(*args)
  @web_tester.cell(*args);
end

#check_checkbox(name, value = nil) ⇒ Object

Check one or more checkboxes with same name, can accept a string or an array of string as values checkbox, pass array as values will try to set mulitple checkboxes.

page.check_checkbox(‘bad_ones’, ‘Chicken Little’) page.check_checkbox(‘good_ones’, [‘Cars’, ‘Toy Story’])



322
323
324
325
# File 'lib/rwebunit/driver.rb', line 322

def check_checkbox(name, value=nil)
  operation_delay
  @web_tester.check_checkbox(name, value)
end

#checkbox(*args) ⇒ Object Also known as: check_box



123
124
125
# File 'lib/rwebunit/driver.rb', line 123

def checkbox(*args)
  @web_tester.checkbox(*args);
end

#checkboxesObject



221
222
223
# File 'lib/rwebunit/driver.rb', line 221

def checkboxes;
  @web_tester.checkboxes;
end

#clear_radio_option(name, value) ⇒ Object Also known as: clear_radio_button



306
307
308
309
# File 'lib/rwebunit/driver.rb', line 306

def clear_radio_option(name, value)
  operation_delay
  @web_tester.clear_radio_option(name, value)
end

#click_button_with_caption(caption) ⇒ Object Also known as: click_button_with_text, click_button



272
273
274
275
# File 'lib/rwebunit/driver.rb', line 272

def click_button_with_caption(caption)
  operation_delay
  @web_tester.click_button_with_caption(caption)
end

#click_button_with_id(button_id) ⇒ Object



267
268
269
270
# File 'lib/rwebunit/driver.rb', line 267

def click_button_with_id(button_id)
  operation_delay
  @web_tester.click_button_with_id(button_id)
end

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



279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rwebunit/driver.rb', line 279

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_button_with_value(value) ⇒ Object



294
295
296
297
# File 'lib/rwebunit/driver.rb', line 294

def click_button_with_value(value)
  operation_delay
  @web_tester.click_button_with_value(value)
end


247
248
249
250
# File 'lib/rwebunit/driver.rb', line 247

def click_link_with_id(link_id)
  operation_delay
  @web_tester.click_link_with_id(link_id)
end

links



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

def click_link_with_text(link_text)
  operation_delay
  @web_tester.click_link_with_text(link_text)
end

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



409
410
411
# File 'lib/rwebunit/driver.rb', line 409

def click_popup_window(button, waitTime= 9, user_input=nil )
  @web_tester.start_clicker(button, waitTime, user_input)
end

#click_radio_option(name, value) ⇒ Object Also known as: click_radio_button

Radios



300
301
302
303
# File 'lib/rwebunit/driver.rb', line 300

def click_radio_option(name, value)
  operation_delay
  @web_tester.click_radio_option(name, value)
end

#close_browserObject Also known as: close_ie



46
47
48
# File 'lib/rwebunit/driver.rb', line 46

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

#contains_text(text) ⇒ Object



204
205
206
# File 'lib/rwebunit/driver.rb', line 204

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

#div(*args) ⇒ Object



128
129
130
# File 'lib/rwebunit/driver.rb', line 128

def div(*args)
  @web_tester.div(*args);
end

#dump_response(stream = nil) ⇒ Object


For debugging




405
406
407
# File 'lib/rwebunit/driver.rb', line 405

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

#element_text(elem_id) ⇒ Object



385
386
387
# File 'lib/rwebunit/driver.rb', line 385

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

#enter_text(elementName, elementValue) ⇒ Object Also known as: set_form_element

enter text into a text field



233
234
235
236
# File 'lib/rwebunit/driver.rb', line 233

def enter_text(elementName, elementValue)
  operation_delay
  @web_tester.set_form_element(elementName, elementValue)
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

#file_field(*args) ⇒ Object



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

def file_field(*args)
  @web_tester.file_field(*args);
end

#form(*args) ⇒ Object



131
132
133
# File 'lib/rwebunit/driver.rb', line 131

def form(*args)
  @web_tester.form(*args);
end

#frame(*args) ⇒ Object



134
135
136
# File 'lib/rwebunit/driver.rb', line 134

def frame(*args)
  @web_tester.frame(*args);
end

#go_backObject

browser navigation



52
53
54
# File 'lib/rwebunit/driver.rb', line 52

def go_back;
  @web_tester.go_back;
end

#go_forwardObject



55
56
57
# File 'lib/rwebunit/driver.rb', line 55

def go_forward;
  @web_tester.go_forward;
end

#goto_page(page) ⇒ Object



59
60
61
62
# File 'lib/rwebunit/driver.rb', line 59

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

#h1(*args) ⇒ Object



137
138
139
# File 'lib/rwebunit/driver.rb', line 137

def h1(*args)
  @web_tester.h1(*args);
end

#h2(*args) ⇒ Object



140
141
142
# File 'lib/rwebunit/driver.rb', line 140

def h2(*args)
  @web_tester.h2(*args);
end

#h3(*args) ⇒ Object



143
144
145
# File 'lib/rwebunit/driver.rb', line 143

def h3(*args)
  @web_tester.h3(*args);
end

#h4(*args) ⇒ Object



146
147
148
# File 'lib/rwebunit/driver.rb', line 146

def h4(*args)
  @web_tester.h4(*args);
end

#h5(*args) ⇒ Object



149
150
151
# File 'lib/rwebunit/driver.rb', line 149

def h5(*args)
  @web_tester.h5(*args);
end

#h6(*args) ⇒ Object



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

def h6(*args)
  @web_tester.h6(*args);
end

#hidden(*args) ⇒ Object



155
156
157
# File 'lib/rwebunit/driver.rb', line 155

def hidden(*args)
  @web_tester.hidden(*args);
end

#ieObject



42
43
44
# File 'lib/rwebunit/driver.rb', line 42

def ie;
  @web_tester.ie;
end

#image(*args) ⇒ Object



158
159
160
# File 'lib/rwebunit/driver.rb', line 158

def image(*args)
  @web_tester.image(*args);
end

#imagesObject



209
210
211
# File 'lib/rwebunit/driver.rb', line 209

def images;
  @web_tester.images;
end

#label(*args) ⇒ Object



200
201
202
# File 'lib/rwebunit/driver.rb', line 200

def label(*args)
  @web_tester.label(*args);
end

#li(*args) ⇒ Object



161
162
163
# File 'lib/rwebunit/driver.rb', line 161

def li(*args)
  @web_tester.li(*args);
end


164
165
166
# File 'lib/rwebunit/driver.rb', line 164

def link(*args)
  @web_tester.link(*args);
end


212
213
214
# File 'lib/rwebunit/driver.rb', line 212

def links;
  @web_tester.links;
end

#map(*args) ⇒ Object



167
168
169
# File 'lib/rwebunit/driver.rb', line 167

def map(*args)
  @web_tester.map(*args);
end

#new_popup_window(options) ⇒ Object



339
340
341
# File 'lib/rwebunit/driver.rb', line 339

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



413
414
415
416
417
418
419
420
421
# File 'lib/rwebunit/driver.rb', line 413

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

#paragraph(*args) ⇒ Object



194
195
196
# File 'lib/rwebunit/driver.rb', line 194

def paragraph(*args)
  @web_tester.paragraph(*args);
end

#pre(*args) ⇒ Object



170
171
172
# File 'lib/rwebunit/driver.rb', line 170

def pre(*args)
  @web_tester.pre(*args);
end

#radio(*args) ⇒ Object



178
179
180
# File 'lib/rwebunit/driver.rb', line 178

def radio(*args)
  @web_tester.radio(*args);
end

#radiosObject



224
225
226
# File 'lib/rwebunit/driver.rb', line 224

def radios;
  @web_tester.radios;
end

#refreshObject Also known as: refresh_page



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

def refresh;
  @web_tester.refresh;
end

#row(*args) ⇒ Object Also known as: tr



173
174
175
# File 'lib/rwebunit/driver.rb', line 173

def row(*args)
  @web_tester.row(*args);
end

#select_file_for_upload(file_field, file_path) ⇒ Object

Filefield



313
314
315
316
# File 'lib/rwebunit/driver.rb', line 313

def select_file_for_upload(file_field, file_path)
  operation_delay
  @web_tester.select_file_for_upload(file_field, file_path)
end

#select_list(*args) ⇒ Object



181
182
183
# File 'lib/rwebunit/driver.rb', line 181

def select_list(*args)
  @web_tester.select_list(*args);
end

#select_listsObject



218
219
220
# File 'lib/rwebunit/driver.rb', line 218

def select_lists;
  @web_tester.select_lists;
end

#select_option(selectName, option) ⇒ Object

combo box



334
335
336
337
# File 'lib/rwebunit/driver.rb', line 334

def select_option(selectName, option)
  operation_delay
  @web_tester.select_option(selectName, option)
end

#shall_not_allowObject Also known as: do_not_allow

fail the test if user can perform the operation



390
391
392
393
394
395
396
397
398
399
# File 'lib/rwebunit/driver.rb', line 390

def shall_not_allow
  operation_performed_ok = false
  begin
    yield
    operation_performed_ok  = true
  rescue
  end

  raise "Operation shall not be allowed" if operation_performed_ok
end

#span(*args) ⇒ Object



184
185
186
# File 'lib/rwebunit/driver.rb', line 184

def span(*args)
  @web_tester.span(*args);
end

#submit(button_id) ⇒ Object

click a form submit button with specified button id



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

def submit()
  operation_delay
  @web_tester.submit()
end

#table(*args) ⇒ Object



187
188
189
# File 'lib/rwebunit/driver.rb', line 187

def table(*args)
  @web_tester.table(*args);
end

#test_contextObject



34
35
36
# File 'lib/rwebunit/driver.rb', line 34

def test_context
  @web_tester.test_context
end

#text_field(*args) ⇒ Object



190
191
192
# File 'lib/rwebunit/driver.rb', line 190

def text_field(*args)
  @web_tester.text_field(*args);
end

#text_fieldsObject



227
228
229
# File 'lib/rwebunit/driver.rb', line 227

def text_fields;
  @web_tester.text_fields;
end

#uncheck_checkbox(name, value = nil) ⇒ Object

Uncheck one or more checkboxes with same name



328
329
330
331
# File 'lib/rwebunit/driver.rb', line 328

def uncheck_checkbox(name, value=nil)
  operation_delay
  @web_tester.uncheck_checkbox(name, value)
end

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



375
376
377
378
379
380
381
382
383
# File 'lib/rwebunit/driver.rb', line 375

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