Class: RWebUnit::WebTester

Inherits:
Object
  • Object
show all
Defined in:
lib/rwebunit/web_tester.rb

Overview

Wrapping WATIR and provide assertions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url = nil, options = {}) ⇒ WebTester

Returns a new instance of WebTester.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rwebunit/web_tester.rb', line 48

def initialize(base_url = nil, options = {})
  default_options = {:speed => "zippy",
    :visible => true,
    :highlight_colour => 'yellow',
    :close_others => true}
  options = default_options.merge options
  @context = Context.new base_url if base_url

  if (options[:firefox] &&  $firewatir_loaded) || ($firewatir_loaded and !$watir_loaded)
    @@browser = FireWatir::Firefox.start(base_url)
  elsif $watir_loaded
    @@browser = Watir::IE.new

    if ENV['ITEST_EMULATE_TYPING'] == "true" &&  ENV['ITEST_TYPING_SPEED'] then
      @@browser.set_slow_speed if ENV['ITEST_TYPING_SPEED'] == 'slow'
      @@browser.set_fast_speed if ENV['ITEST_TYPING_SPEED'] == 'fast'
    else
      @@browser.speed = :zippy
    end
    @@browser.activeObjectHighLightColor = options[:highlight_colour]
    @@browser.visible = options[:visible] unless $HIDE_IE
    @@browser.close_others if options[:close_others]
  else
    raise "rWebUnit initialiazation error, most likely Watir or Firewatir not present"
  end

end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

Class Method Details

.attach_browser(how, what) ⇒ Object



432
433
434
435
436
437
438
# File 'lib/rwebunit/web_tester.rb', line 432

def self.attach_browser(how, what)
  if @@browser && @@browser.class == Watir::IE
    Watir::IE.attach(how, what)
  else
    raise "not implemented yet"
  end
end

.close_all_browsersObject



244
245
246
247
248
249
250
# File 'lib/rwebunit/web_tester.rb', line 244

def self.close_all_browsers
  if is_firefox? then
    @@browser.close_all
  else
    Watir::IE.close_all
  end
end

Instance Method Details

#area(*args) ⇒ Object

Delegate to Watir



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

def area(*args)
  @@browser.area(*args)
end

#base_url=(new_base_url) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/rwebunit/web_tester.rb', line 216

def base_url=(new_base_url)
  if @context
    @conext.base_url = new_base_url
    return
  end
  @context = Context.new base_url
end

#begin_at(relative_url) ⇒ Object



260
261
262
# File 'lib/rwebunit/web_tester.rb', line 260

def begin_at(relative_url)
  @@browser.goto full_url(relative_url)
end

#browser_opened?Boolean

Returns:

  • (Boolean)


264
265
266
267
268
269
270
# File 'lib/rwebunit/web_tester.rb', line 264

def browser_opened?
  begin
    @@browser != nil
  rescue => e
    return false
  end
end

#button(*args) ⇒ Object



82
83
84
# File 'lib/rwebunit/web_tester.rb', line 82

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

#buttonsObject



195
196
197
# File 'lib/rwebunit/web_tester.rb', line 195

def buttons
  @@browser.buttons;
end

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



85
86
87
# File 'lib/rwebunit/web_tester.rb', line 85

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

#check_checkbox(checkBoxName, values = nil) ⇒ Object

checkbox



368
369
370
371
372
373
374
375
376
377
# File 'lib/rwebunit/web_tester.rb', line 368

def check_checkbox(checkBoxName, values=nil)
  if values
    values.class == Array ? arys = values : arys = [values]
    arys.each {|cbx_value|
      checkbox(:name, checkBoxName, cbx_value).set
    }
  else
    checkbox(:name, checkBoxName).set
  end
end

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



89
90
91
# File 'lib/rwebunit/web_tester.rb', line 89

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

#checkboxesObject



201
202
203
# File 'lib/rwebunit/web_tester.rb', line 201

def checkboxes
  @@browser.checkboxes;
end

#clear_radio_option(radio_group, radio_option) ⇒ Object



396
397
398
# File 'lib/rwebunit/web_tester.rb', line 396

def clear_radio_option(radio_group, radio_option)
  radio(:name, radio_group, radio_option).clear
end

#click_button_with_caption(caption) ⇒ Object



341
342
343
# File 'lib/rwebunit/web_tester.rb', line 341

def click_button_with_caption(caption)
  wait_before_and_after { button(:caption, caption).click }
end

#click_button_with_id(id) ⇒ Object

buttons



333
334
335
# File 'lib/rwebunit/web_tester.rb', line 333

def click_button_with_id(id)
  wait_before_and_after { button(:id, id).click }
end

#click_button_with_name(name) ⇒ Object



337
338
339
# File 'lib/rwebunit/web_tester.rb', line 337

def click_button_with_name(name)
  wait_before_and_after { button(:name, name).click }
end

#click_button_with_value(value) ⇒ Object



345
346
347
# File 'lib/rwebunit/web_tester.rb', line 345

def click_button_with_value(value)
  wait_before_and_after { button(:value, value).click }
end

links



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

def click_link_with_id(link_id)
  wait_before_and_after { link(:id, link_id).click }
end


326
327
328
# File 'lib/rwebunit/web_tester.rb', line 326

def click_link_with_text(text)
  wait_before_and_after { link(:text, text).click }
end

#click_radio_option(radio_group, radio_option) ⇒ Object

the method is protected in JWebUnit



392
393
394
# File 'lib/rwebunit/web_tester.rb', line 392

def click_radio_option(radio_group, radio_option)
  radio(:name, radio_group, radio_option).set
end

#close_browserObject

Close the browser window. Useful for automated test suites to reduce test interaction.



234
235
236
237
238
239
240
241
242
# File 'lib/rwebunit/web_tester.rb', line 234

def close_browser
  if is_firefox? then
    puts "[debug] about to close firefox"
    @@browser.close
  else
    @@browser.getIE.quit
  end
  sleep 2
end

#close_othersObject



305
306
307
# File 'lib/rwebunit/web_tester.rb', line 305

def close_others;
  @@browser.close_others;
end

#contains_text(text) ⇒ Object



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

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

#div(*args) ⇒ Object



93
94
95
# File 'lib/rwebunit/web_tester.rb', line 93

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

#dump_response(stream = nil) ⇒ Object


For deubgging




461
462
463
464
465
466
467
# File 'lib/rwebunit/web_tester.rb', line 461

def dump_response(stream = nil)
  if stream.nil?
    puts page_source # std out
  else
    stream.puts page_source
  end
end

#element_by_id(elem_id) ⇒ Object



400
401
402
403
404
405
406
# File 'lib/rwebunit/web_tester.rb', line 400

def element_by_id(elem_id)
  if is_firefox?
    elem =  label(:id, elem_id)  || button(:id, elem_id) || span(:id, elem_id) || hidden(:id, elem_id) || link(:id, elem_id) || radio(:id, elem_id)
  else
    elem = @@browser.document.getElementById(elem_id)
  end
end

#element_source(elementId) ⇒ Object



418
419
420
421
422
# File 'lib/rwebunit/web_tester.rb', line 418

def element_source(elementId)
  elem = element_by_id(elementId)
  assert_not_nil(elem, "HTML element: #{elementId} not exists")
  elem.innerHTML
end

#element_value(elementId) ⇒ Object



408
409
410
411
412
413
414
415
416
# File 'lib/rwebunit/web_tester.rb', line 408

def element_value(elementId)
  if is_firefox? then
    elem = element_by_id(elementId)
    elem ? elem.invoke('innerText') : nil
  else
    elem = element_by_id(elementId)
    elem ? elem.invoke('innerText') : nil
  end
end

#enter_text_into_field_with_name(name, text) ⇒ Object Also known as: set_form_element, enter_text

text fields



310
311
312
313
314
315
316
317
# File 'lib/rwebunit/web_tester.rb', line 310

def enter_text_into_field_with_name(name, text)
  if is_firefox?
    wait_before_and_after { text_field(:name, name).value = text }
    sleep 0.5
  else
    wait_before_and_after { text_field(:name, name).set(text) }
  end
end

#file_field(*args) ⇒ Object



163
164
165
# File 'lib/rwebunit/web_tester.rb', line 163

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

#firefoxObject



497
498
499
500
# File 'lib/rwebunit/web_tester.rb', line 497

def firefox
  raise "can't call this as it is configured to use IE" unless is_firefox?
  @@browser
end

#focusObject



302
303
304
# File 'lib/rwebunit/web_tester.rb', line 302

def focus;
  @@browser.focus;
end

#form(*args) ⇒ Object



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

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

#frame(*args) ⇒ Object



99
100
101
# File 'lib/rwebunit/web_tester.rb', line 99

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

#full_url(relative_url) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/rwebunit/web_tester.rb', line 252

def full_url(relative_url)
  if @context && @context.base_url
    @context.base_url + relative_url
  else
    relative_url
  end
end

#go_backObject



289
290
291
# File 'lib/rwebunit/web_tester.rb', line 289

def go_back;
  @@browser.back;
end

#go_forwardObject



292
293
294
# File 'lib/rwebunit/web_tester.rb', line 292

def go_forward;
  @@browser.forward;
end

#goto_page(page) ⇒ Object



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

def goto_page(page)
  @@browser.goto full_url(page);
end

#h1(*args) ⇒ Object



102
103
104
# File 'lib/rwebunit/web_tester.rb', line 102

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

#h2(*args) ⇒ Object



105
106
107
# File 'lib/rwebunit/web_tester.rb', line 105

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

#h3(*args) ⇒ Object



108
109
110
# File 'lib/rwebunit/web_tester.rb', line 108

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

#h4(*args) ⇒ Object



111
112
113
# File 'lib/rwebunit/web_tester.rb', line 111

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

#h5(*args) ⇒ Object



114
115
116
# File 'lib/rwebunit/web_tester.rb', line 114

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

#h6(*args) ⇒ Object



117
118
119
# File 'lib/rwebunit/web_tester.rb', line 117

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

#hidden(*args) ⇒ Object



120
121
122
# File 'lib/rwebunit/web_tester.rb', line 120

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

#ieObject

return underlying browser



492
493
494
495
# File 'lib/rwebunit/web_tester.rb', line 492

def ie
  raise "can't call this as it is configured to use Firefox" if is_firefox?
  @@browser
end

#image(*args) ⇒ Object



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

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

#imagesObject



188
189
190
# File 'lib/rwebunit/web_tester.rb', line 188

def images
  @@browser.images
end

#is_firefox?Boolean

Returns:

  • (Boolean)


224
225
226
227
228
229
230
# File 'lib/rwebunit/web_tester.rb', line 224

def is_firefox?
  begin
    @@browser.class == FireWatir::Firefox
  rescue => e
    return false
  end
end

#label(*args) ⇒ Object



166
167
168
# File 'lib/rwebunit/web_tester.rb', line 166

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

#li(*args) ⇒ Object



126
127
128
# File 'lib/rwebunit/web_tester.rb', line 126

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


129
130
131
# File 'lib/rwebunit/web_tester.rb', line 129

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


192
193
194
# File 'lib/rwebunit/web_tester.rb', line 192

def links
  @@browser.links;
end

#map(*args) ⇒ Object



132
133
134
# File 'lib/rwebunit/web_tester.rb', line 132

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

#new_popup_window(options, browser = "ie") ⇒ Object

Attach a Watir::IE instance to a popup window.

Typical usage

new_popup_window(:url => "http://www.google.com/a.pdf")


444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/rwebunit/web_tester.rb', line 444

def new_popup_window(options, browser = "ie")
  if is_firefox?
    raise "not implemented"
  else
    if options[:url]
      Watir::IE.attach(:url, options[:url])
    elsif options[:title]
      Watir::IE.attach(:title, options[:title])
    else
      raise 'Please specify title or url of new pop up window'
    end
  end
end

#page_sourceObject Also known as: html_body



174
175
176
177
# File 'lib/rwebunit/web_tester.rb', line 174

def page_source
  @@browser.html()
  #@@browser.document.body
end

#page_titleObject



180
181
182
183
184
185
186
# File 'lib/rwebunit/web_tester.rb', line 180

def page_title
  if is_firefox?
    @@browser.title
  else
    @@browser.document.title
  end
end

#paragraph(*args) ⇒ Object



160
161
162
# File 'lib/rwebunit/web_tester.rb', line 160

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

#pre(*args) ⇒ Object



135
136
137
# File 'lib/rwebunit/web_tester.rb', line 135

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

#radio(*args) ⇒ Object



138
139
140
# File 'lib/rwebunit/web_tester.rb', line 138

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

#radiosObject



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

def radios
  @@browser.radios;
end

#refreshObject



298
299
300
# File 'lib/rwebunit/web_tester.rb', line 298

def refresh;
  @@browser.refresh;
end

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



141
142
143
# File 'lib/rwebunit/web_tester.rb', line 141

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

#save_page(file_name = nil) ⇒ Object



502
503
504
505
506
# File 'lib/rwebunit/web_tester.rb', line 502

def save_page(file_name = nil)
  file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
  puts "about to save page: #{File.expand_path(file_name)}"
  File.open(file_name, "w").puts page_source
end

#select_file_for_upload(file_field, file_path) ⇒ Object



424
425
426
# File 'lib/rwebunit/web_tester.rb', line 424

def select_file_for_upload(file_field, file_path)
  file_field(:name, file_field).set(file_path)
end

#select_list(*args) ⇒ Object



145
146
147
# File 'lib/rwebunit/web_tester.rb', line 145

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

#select_listsObject



198
199
200
# File 'lib/rwebunit/web_tester.rb', line 198

def select_lists
  @@browser.select_lists;
end

#select_option(selectName, option) ⇒ Object



349
350
351
352
# File 'lib/rwebunit/web_tester.rb', line 349

def select_option(selectName, option)
  #      @@browser.selectBox(:name, selectName).select(option)
  select_list(:name, selectName).select(option)
end

#span(*args) ⇒ Object



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

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

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

A Better Popup Handler using the latest Watir version. Posted by [email protected]

wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/rwebunit/web_tester.rb', line 473

def start_clicker( button, waitTime= 9, user_input=nil)
  # get a handle if one exists
  hwnd = @@browser.enabled_popup(waitTime)
  if (hwnd)  # yes there is a popup
    w = WinClicker.new
    if ( user_input )
      w.setTextValueForFileNameField( hwnd, "#{user_input}" )
    end
    # I put this in to see the text being input it is not necessary to work
    sleep 3
    # "OK" or whatever the name on the button is
    w.clickWindowsButton_hwnd( hwnd, "#{button}" )
    #
    # this is just cleanup
    w = nil
  end
end

#start_window(url = nil) ⇒ Object



428
429
430
# File 'lib/rwebunit/web_tester.rb', line 428

def start_window(url = nil)
  @@browser.start_window(url);
end

#submit(buttonName = nil) ⇒ Object

submit first submit button



355
356
357
358
359
360
361
362
363
364
365
# File 'lib/rwebunit/web_tester.rb', line 355

def submit(buttonName = nil)
  if (buttonName.nil?) then
    buttons.each { |button|
      next if button.type != 'submit'
      button.click
      return
    }
  else
    click_button_with_name(buttonName)
  end
end

#table(*args) ⇒ Object



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

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

#text_field(*args) ⇒ Object



156
157
158
# File 'lib/rwebunit/web_tester.rb', line 156

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

#text_fieldsObject



207
208
209
# File 'lib/rwebunit/web_tester.rb', line 207

def text_fields
  @@browser.text_fields;
end

#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object



379
380
381
382
383
384
385
386
387
388
# File 'lib/rwebunit/web_tester.rb', line 379

def uncheck_checkbox(checkBoxName, values = nil)
  if values
    values.class == Array ? arys = values : arys = [values]
    arys.each {|cbx_value|
      checkbox(:name, checkBoxName, cbx_value).clear
    }
  else
    checkbox(:name, checkBoxName).clear
  end
end

#urlObject

current url



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

def url
  @@browser.url
end

#wait_before_and_afterObject

A convenience method to wait at both ends of an operation for the browser to catch up.



282
283
284
285
286
# File 'lib/rwebunit/web_tester.rb', line 282

def wait_before_and_after
  wait_for_browser
  yield
  wait_for_browser
end

#wait_for_browserObject

Some browsers (i.e. IE) need to be waited on before more actions can be performed. Most action methods in Watir::Simple already call this before and after.



275
276
277
# File 'lib/rwebunit/web_tester.rb', line 275

def wait_for_browser
  @@browser.waitForIE unless is_firefox?
end