rwebunit 0.9.4 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,510 +1,433 @@
1
- #***********************************************************
2
- #* Copyright (c) 2006, Zhimin Zhan.
3
- #* Distributed open-source, see full license in MIT-LICENSE
4
- #***********************************************************
5
-
6
- begin
7
- require 'watir'
8
- require 'watir/contrib/enabled_popup'
9
- require 'watir/close_all'
10
- $watir_loaded = true
11
- rescue LoadError => e
12
- $watir_loaded = false
13
- end
14
-
15
- begin
16
- require 'rubygems'
17
- require "firewatir";
18
- $firewatir_loaded = true
19
- rescue LoadError => e
20
- puts e
21
- $firewatir_loaded = false
22
- end
23
-
24
- raise "You have must at least Watir or Firewatir installed" unless $watir_loaded || $firewatir_loaded
25
-
26
- # Patch: fix typo in Watir 1.5.3
27
- module Watir
28
- class IE
29
- def self.close_all_but(except=nil)
30
- Watir::IE.each do |ie|
31
- ie.close_modal
32
- ie.close unless except and except.hwnd == ie.hwnd
33
- end
34
- sleep 1.0 # replace with polling for window count to be zero?
35
- end
36
- end
37
- end
38
-
39
- module RWebUnit
40
-
41
- ##
42
- # Wrapping WATIR and provide *assertions*
43
- #
44
- class WebTester
45
-
46
- attr_accessor :context
47
-
48
- def initialize(base_url = nil, options = {})
49
- default_options = {:speed => "zippy",
50
- :visible => true,
51
- :highlight_colour => 'yellow',
52
- :close_others => true}
53
- options = default_options.merge options
54
- @context = Context.new base_url if base_url
55
-
56
- if (options[:firefox] && $firewatir_loaded) || ($firewatir_loaded and !$watir_loaded)
57
- @@browser = FireWatir::Firefox.start(base_url)
58
- elsif $watir_loaded
59
- @@browser = Watir::IE.new
60
-
61
- if ENV['ITEST_EMULATE_TYPING'] == "true" && ENV['ITEST_TYPING_SPEED'] then
62
- @@browser.set_slow_speed if ENV['ITEST_TYPING_SPEED'] == 'slow'
63
- @@browser.set_fast_speed if ENV['ITEST_TYPING_SPEED'] == 'fast'
64
- else
65
- @@browser.speed = :zippy
66
- end
67
- @@browser.activeObjectHighLightColor = options[:highlight_colour]
68
- @@browser.visible = options[:visible] unless $HIDE_IE
69
- @@browser.close_others if options[:close_others]
70
- else
71
- raise "rWebUnit initialiazation error, most likely Watir or Firewatir not present"
72
- end
73
-
74
- end
75
-
76
- ##
77
- # Delegate to Watir
78
- #
79
- def area(*args)
80
- @@browser.area(*args)
81
- end
82
- def button(*args)
83
- @@browser.button(*args);
84
- end
85
- def cell(*args)
86
- @@browser.cell(*args);
87
- end
88
- alias td cell
89
- def checkbox(*args)
90
- @@browser.checkbox(*args);
91
- end
92
- alias check_box checkbox # seems watir doc is wrong, checkbox not check_box
93
- def div(*args)
94
- @@browser.div(*args);
95
- end
96
- def form(*args)
97
- @@browser.form(*args);
98
- end
99
- def frame(*args)
100
- @@browser.frame(*args);
101
- end
102
- def h1(*args)
103
- @@browser.h1(*args);
104
- end
105
- def h2(*args)
106
- @@browser.h2(*args);
107
- end
108
- def h3(*args)
109
- @@browser.h3(*args);
110
- end
111
- def h4(*args)
112
- @@browser.h4(*args);
113
- end
114
- def h5(*args)
115
- @@browser.h5(*args);
116
- end
117
- def h6(*args)
118
- @@browser.h6(*args);
119
- end
120
- def hidden(*args)
121
- @@browser.hidden(*args);
122
- end
123
- def image(*args)
124
- @@browser.image(*args);
125
- end
126
- def li(*args)
127
- @@browser.li(*args);
128
- end
129
- def link(*args)
130
- @@browser.link(*args);
131
- end
132
- def map(*args)
133
- @@browser.map(*args);
134
- end
135
- def pre(*args)
136
- @@browser.pre(*args);
137
- end
138
- def radio(*args)
139
- @@browser.radio(*args);
140
- end
141
- def row(*args)
142
- @@browser.row(*args);
143
- end
144
- alias tr row
145
- def select_list(*args)
146
- @@browser.select_list(*args);
147
- end
148
-
149
- def span(*args)
150
- @@browser.span(*args);
151
- end
152
-
153
- def table(*args)
154
- @@browser.table(*args);
155
- end
156
- def text_field(*args)
157
- @@browser.text_field(*args);
158
- end
159
-
160
- def paragraph(*args)
161
- @@browser.paragraph(*args);
162
- end
163
- def file_field(*args)
164
- @@browser.file_field(*args);
165
- end
166
- def label(*args)
167
- @@browser.label(*args);
168
- end
169
-
170
- def contains_text(text)
171
- @@browser.contains_text(text);
172
- end
173
-
174
- def page_source
175
- @@browser.html()
176
- #@@browser.document.body
177
- end
178
- alias html_body page_source
179
-
180
- def page_title
181
- if is_firefox?
182
- @@browser.title
183
- else
184
- @@browser.document.title
185
- end
186
- end
187
-
188
- def images
189
- @@browser.images
190
- end
191
-
192
- def links
193
- @@browser.links;
194
- end
195
- def buttons
196
- @@browser.buttons;
197
- end
198
- def select_lists
199
- @@browser.select_lists;
200
- end
201
- def checkboxes
202
- @@browser.checkboxes;
203
- end
204
- def radios
205
- @@browser.radios;
206
- end
207
- def text_fields
208
- @@browser.text_fields;
209
- end
210
-
211
- # current url
212
- def url
213
- @@browser.url
214
- end
215
-
216
- def base_url=(new_base_url)
217
- if @context
218
- @conext.base_url = new_base_url
219
- return
220
- end
221
- @context = Context.new base_url
222
- end
223
-
224
- def is_firefox?
225
- begin
226
- @@browser.class == FireWatir::Firefox
227
- rescue => e
228
- return false
229
- end
230
- end
231
-
232
- # Close the browser window. Useful for automated test suites to reduce
233
- # test interaction.
234
- def close_browser
235
- if is_firefox? then
236
- puts "[debug] about to close firefox"
237
- @@browser.close
238
- else
239
- @@browser.getIE.quit
240
- end
241
- sleep 2
242
- end
243
-
244
- def self.close_all_browsers
245
- if is_firefox? then
246
- @@browser.close_all
247
- else
248
- Watir::IE.close_all
249
- end
250
- end
251
-
252
- def full_url(relative_url)
253
- if @context && @context.base_url
254
- @context.base_url + relative_url
255
- else
256
- relative_url
257
- end
258
- end
259
-
260
- def begin_at(relative_url)
261
- @@browser.goto full_url(relative_url)
262
- end
263
-
264
- def browser_opened?
265
- begin
266
- @@browser != nil
267
- rescue => e
268
- return false
269
- end
270
- end
271
-
272
- # Some browsers (i.e. IE) need to be waited on before more actions can be
273
- # performed. Most action methods in Watir::Simple already call this before
274
- # and after.
275
- def wait_for_browser
276
- @@browser.waitForIE unless is_firefox?
277
- end
278
-
279
-
280
- # A convenience method to wait at both ends of an operation for the browser
281
- # to catch up.
282
- def wait_before_and_after
283
- wait_for_browser
284
- yield
285
- wait_for_browser
286
- end
287
-
288
-
289
- def go_back;
290
- @@browser.back;
291
- end
292
- def go_forward;
293
- @@browser.forward;
294
- end
295
- def goto_page(page)
296
- @@browser.goto full_url(page);
297
- end
298
- def refresh;
299
- @@browser.refresh;
300
- end
301
-
302
- def focus;
303
- @@browser.focus;
304
- end
305
- def close_others;
306
- @@browser.close_others;
307
- end
308
-
309
- # text fields
310
- def enter_text_into_field_with_name(name, text)
311
- if is_firefox?
312
- wait_before_and_after { text_field(:name, name).value = text }
313
- sleep 0.5
314
- else
315
- wait_before_and_after { text_field(:name, name).set(text) }
316
- end
317
- end
318
- alias set_form_element enter_text_into_field_with_name
319
- alias enter_text enter_text_into_field_with_name
320
-
321
- #links
322
- def click_link_with_id(link_id)
323
- wait_before_and_after { link(:id, link_id).click }
324
- end
325
-
326
- def click_link_with_text(text)
327
- wait_before_and_after { link(:text, text).click }
328
- end
329
-
330
- ##
331
- # buttons
332
-
333
- def click_button_with_id(id)
334
- wait_before_and_after { button(:id, id).click }
335
- end
336
-
337
- def click_button_with_name(name)
338
- wait_before_and_after { button(:name, name).click }
339
- end
340
-
341
- def click_button_with_caption(caption)
342
- wait_before_and_after { button(:caption, caption).click }
343
- end
344
-
345
- def click_button_with_value(value)
346
- wait_before_and_after { button(:value, value).click }
347
- end
348
-
349
- def select_option(selectName, option)
350
- # @@browser.selectBox(:name, selectName).select(option)
351
- select_list(:name, selectName).select(option)
352
- end
353
-
354
- # submit first submit button
355
- def submit(buttonName = nil)
356
- if (buttonName.nil?) then
357
- buttons.each { |button|
358
- next if button.type != 'submit'
359
- button.click
360
- return
361
- }
362
- else
363
- click_button_with_name(buttonName)
364
- end
365
- end
366
-
367
- # checkbox
368
- def check_checkbox(checkBoxName, values=nil)
369
- if values
370
- values.class == Array ? arys = values : arys = [values]
371
- arys.each {|cbx_value|
372
- checkbox(:name, checkBoxName, cbx_value).set
373
- }
374
- else
375
- checkbox(:name, checkBoxName).set
376
- end
377
- end
378
-
379
- def uncheck_checkbox(checkBoxName, values = nil)
380
- if values
381
- values.class == Array ? arys = values : arys = [values]
382
- arys.each {|cbx_value|
383
- checkbox(:name, checkBoxName, cbx_value).clear
384
- }
385
- else
386
- checkbox(:name, checkBoxName).clear
387
- end
388
- end
389
-
390
-
391
- # the method is protected in JWebUnit
392
- def click_radio_option(radio_group, radio_option)
393
- radio(:name, radio_group, radio_option).set
394
- end
395
-
396
- def clear_radio_option(radio_group, radio_option)
397
- radio(:name, radio_group, radio_option).clear
398
- end
399
-
400
- def element_by_id(elem_id)
401
- if is_firefox?
402
- 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)
403
- else
404
- elem = @@browser.document.getElementById(elem_id)
405
- end
406
- end
407
-
408
- def element_value(elementId)
409
- if is_firefox? then
410
- elem = element_by_id(elementId)
411
- elem ? elem.invoke('innerText') : nil
412
- else
413
- elem = element_by_id(elementId)
414
- elem ? elem.invoke('innerText') : nil
415
- end
416
- end
417
-
418
- def element_source(elementId)
419
- elem = element_by_id(elementId)
420
- assert_not_nil(elem, "HTML element: #{elementId} not exists")
421
- elem.innerHTML
422
- end
423
-
424
- def select_file_for_upload(file_field, file_path)
425
- file_field(:name, file_field).set(file_path)
426
- end
427
-
428
- def start_window(url = nil)
429
- @@browser.start_window(url);
430
- end
431
-
432
- def self.attach_browser(how, what)
433
- if @@browser && @@browser.class == Watir::IE
434
- Watir::IE.attach(how, what)
435
- else
436
- raise "not implemented yet"
437
- end
438
- end
439
-
440
- # Attach a Watir::IE instance to a popup window.
441
- #
442
- # Typical usage
443
- # new_popup_window(:url => "http://www.google.com/a.pdf")
444
- def new_popup_window(options, browser = "ie")
445
- if is_firefox?
446
- raise "not implemented"
447
- else
448
- if options[:url]
449
- Watir::IE.attach(:url, options[:url])
450
- elsif options[:title]
451
- Watir::IE.attach(:title, options[:title])
452
- else
453
- raise 'Please specify title or url of new pop up window'
454
- end
455
- end
456
- end
457
-
458
- # ---
459
- # For deubgging
460
- # ---
461
- def dump_response(stream = nil)
462
- if stream.nil?
463
- puts page_source # std out
464
- else
465
- stream.puts page_source
466
- end
467
- end
468
-
469
- # A Better Popup Handler using the latest Watir version. Posted by Mark_cain@rl.gov
470
- #
471
- # http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F
472
- #
473
- def start_clicker( button, waitTime= 9, user_input=nil)
474
- # get a handle if one exists
475
- hwnd = @@browser.enabled_popup(waitTime)
476
- if (hwnd) # yes there is a popup
477
- w = WinClicker.new
478
- if ( user_input )
479
- w.setTextValueForFileNameField( hwnd, "#{user_input}" )
480
- end
481
- # I put this in to see the text being input it is not necessary to work
482
- sleep 3
483
- # "OK" or whatever the name on the button is
484
- w.clickWindowsButton_hwnd( hwnd, "#{button}" )
485
- #
486
- # this is just cleanup
487
- w = nil
488
- end
489
- end
490
-
491
- # return underlying browser
492
- def ie
493
- raise "can't call this as it is configured to use Firefox" if is_firefox?
494
- @@browser
495
- end
496
-
497
- def firefox
498
- raise "can't call this as it is configured to use IE" unless is_firefox?
499
- @@browser
500
- end
501
-
502
- def save_page(file_name = nil)
503
- file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
504
- puts "about to save page: #{File.expand_path(file_name)}"
505
- File.open(file_name, "w").puts page_source
506
- end
507
-
508
- end
509
-
510
- end
1
+ #***********************************************************
2
+ #* Copyright (c) 2006, Zhimin Zhan.
3
+ #* Distributed open-source, see full license in MIT-LICENSE
4
+ #***********************************************************
5
+
6
+ begin
7
+ require 'watir'
8
+ require 'watir/ie'
9
+ require 'watir/contrib/enabled_popup'
10
+ require 'watir/contrib/visible'
11
+ require 'watir/close_all'
12
+ $watir_loaded = true
13
+ rescue LoadError => e
14
+ $watir_loaded = false
15
+ end
16
+
17
+ begin
18
+ require "rubygems";
19
+ require "firewatir";
20
+ $firewatir_loaded = true
21
+ rescue LoadError => e
22
+ $firewatir_loaded = false
23
+ end
24
+
25
+ raise "You have must at least Watir or Firewatir installed" unless $watir_loaded || $firewatir_loaded
26
+
27
+ module RWebUnit
28
+
29
+ ##
30
+ # Wrapping WATIR IE and FireWatir Firefox
31
+ #
32
+ class WebBrowser
33
+
34
+ attr_accessor :context
35
+
36
+ def initialize(base_url = nil, existing_browser = nil, options = {})
37
+ default_options = {:speed => "zippy", :visible => true,
38
+ :highlight_colour => 'yellow', :close_others => true}
39
+ options = default_options.merge options
40
+ @context = Context.new base_url if base_url
41
+
42
+ if (existing_browser) then
43
+ @browser = existing_browser
44
+ else
45
+ if (options[:firefox] && $firewatir_loaded) || ($firewatir_loaded and !$watir_loaded)
46
+ @browser = FireWatir::Firefox.start(base_url)
47
+ elsif $watir_loaded
48
+ @browser = Watir::IE.new
49
+ end
50
+ end
51
+
52
+ raise "rWebUnit initialiazation error, most likely Watir or Firewatir not present" if @browser.nil?
53
+ if $watir_loaded && @browser.class == Watir::IE
54
+ if $ITEST2_EMULATE_TYPING && $ITEST2_TYPING_SPEED then
55
+ @browser.set_slow_speed if $ITEST2_TYPING_SPEED == 'slow'
56
+ @browser.set_fast_speed if $ITEST2_TYPING_SPEED == 'fast'
57
+ else
58
+ @browser.speed = :zippy
59
+ end
60
+ @browser.activeObjectHighLightColor = options[:highlight_colour]
61
+ @browser.visible = options[:visible] unless $HIDE_IE
62
+ @browser.close_others if options[:close_others]
63
+ end
64
+
65
+ end
66
+
67
+ def self.reuse(base_url, options)
68
+ if RUBY_PLATFORM.downcase.include?("mswin") && $ITEST2_BROWSER != "Firefox"
69
+ Watir::IE.each do |browser_window|
70
+ return WebBrowser.new(base_url, browser_window, options)
71
+ end
72
+ puts "no browser instance found"
73
+ WebBrowser.new(base_url, nil, options)
74
+ else
75
+ WebBrowser.new(base_url, nil, options)
76
+ end
77
+ end
78
+
79
+ # for popup windows
80
+ def self.new_from_existing(underlying_browser, web_context = nil)
81
+ return WebBrowser.new(web_context ? web_context.base_url : nil, underlying_browser, {:close_others => false})
82
+ end
83
+
84
+
85
+ ##
86
+ # Delegate to Watir
87
+ #
88
+ [:button, :cell, :checkbox, :div, :form, :frame, :h1, :h2, :h3, :h4, :h5, :h6, :hidden, :image, :li, :link, :map, :pre, :row, :radio, :select_list, :span, :table, :text_field, :paragraph, :file_field, :label].each do |method|
89
+ define_method method do |*args|
90
+ @browser.send(method, *args)
91
+ end
92
+ end
93
+ alias td cell
94
+ alias check_box checkbox # seems watir doc is wrong, checkbox not check_box
95
+ alias tr row
96
+
97
+ # FireWatir does not support area directly, treat it as text_field
98
+ def area(*args)
99
+ if is_firefox?
100
+ text_field(*args)
101
+ else
102
+ @browser.send("area", *args)
103
+ end
104
+ end
105
+
106
+ def contains_text(text)
107
+ @browser.contains_text(text);
108
+ end
109
+
110
+ def page_source
111
+ @browser.html()
112
+ #@browser.document.body
113
+ end
114
+ alias html_body page_source
115
+
116
+ def page_title
117
+ if is_firefox?
118
+ @browser.title
119
+ else
120
+ @browser.document.title
121
+ end
122
+ end
123
+
124
+ [:images, :links, :buttons, :select_lists, :checkboxes, :radios, :text_fields].each do |method|
125
+ define_method method do
126
+ @browser.send(method)
127
+ end
128
+ end
129
+
130
+ # current url
131
+ def url
132
+ @browser.url
133
+ end
134
+
135
+ def base_url=(new_base_url)
136
+ if @context
137
+ @conext.base_url = new_base_url
138
+ return
139
+ end
140
+ @context = Context.new base_url
141
+ end
142
+
143
+ def is_firefox?
144
+ begin
145
+ @browser.class == FireWatir::Firefox
146
+ rescue => e
147
+ return false
148
+ end
149
+ end
150
+
151
+ # Close the browser window. Useful for automated test suites to reduce
152
+ # test interaction.
153
+ def close_browser
154
+ if is_firefox? then
155
+ @browser.close
156
+ else
157
+ @browser.getIE.quit
158
+ end
159
+ sleep 2
160
+ end
161
+ alias close close_browser
162
+
163
+ def self.close_all_browsers
164
+ if is_firefox? then
165
+ @browser.close_all
166
+ else
167
+ Watir::IE.close_all
168
+ end
169
+ end
170
+
171
+ def full_url(relative_url)
172
+ if @context && @context.base_url
173
+ @context.base_url + relative_url
174
+ else
175
+ relative_url
176
+ end
177
+ end
178
+
179
+ def begin_at(relative_url)
180
+ @browser.goto full_url(relative_url)
181
+ end
182
+
183
+ def browser_opened?
184
+ begin
185
+ @browser != nil
186
+ rescue => e
187
+ return false
188
+ end
189
+ end
190
+
191
+ # Some browsers (i.e. IE) need to be waited on before more actions can be
192
+ # performed. Most action methods in Watir::Simple already call this before
193
+ # and after.
194
+ def wait_for_browser
195
+ @browser.waitForIE unless is_firefox?
196
+ end
197
+
198
+
199
+ # A convenience method to wait at both ends of an operation for the browser
200
+ # to catch up.
201
+ def wait_before_and_after
202
+ wait_for_browser
203
+ yield
204
+ wait_for_browser
205
+ end
206
+
207
+
208
+ [:back, :forward, :refresh, :focus, :close_others].each do |method|
209
+ define_method(method) do
210
+ @browser.send(method)
211
+ end
212
+ end
213
+ alias refresh_page refresh
214
+ alias go_back back
215
+ alias go_forward forward
216
+
217
+ def goto_page(page)
218
+ @browser.goto full_url(page);
219
+ end
220
+
221
+ # text fields
222
+ def enter_text_into_field_with_name(name, text)
223
+ if is_firefox?
224
+ wait_before_and_after { text_field(:name, name).value = text }
225
+ sleep 0.3
226
+ else
227
+ wait_before_and_after { text_field(:name, name).set(text) }
228
+ end
229
+ end
230
+ alias set_form_element enter_text_into_field_with_name
231
+ alias enter_text enter_text_into_field_with_name
232
+
233
+ #links
234
+ def click_link_with_id(link_id)
235
+ wait_before_and_after { link(:id, link_id).click }
236
+ end
237
+
238
+ def click_link_with_text(text)
239
+ wait_before_and_after { link(:text, text).click }
240
+ end
241
+
242
+ ##
243
+ # buttons
244
+
245
+ def click_button_with_id(id)
246
+ wait_before_and_after { button(:id, id).click }
247
+ end
248
+
249
+ def click_button_with_name(name)
250
+ wait_before_and_after { button(:name, name).click }
251
+ end
252
+
253
+ def click_button_with_caption(caption)
254
+ wait_before_and_after { button(:caption, caption).click }
255
+ end
256
+
257
+ def click_button_with_value(value)
258
+ wait_before_and_after { button(:value, value).click }
259
+ end
260
+
261
+ def select_option(selectName, option)
262
+ select_list(:name, selectName).select(option)
263
+ end
264
+
265
+ # submit first submit button
266
+ def submit(buttonName = nil)
267
+ if (buttonName.nil?) then
268
+ buttons.each { |button|
269
+ next if button.type != 'submit'
270
+ button.click
271
+ return
272
+ }
273
+ else
274
+ click_button_with_name(buttonName)
275
+ end
276
+ end
277
+
278
+ # checkbox
279
+ def check_checkbox(checkBoxName, values=nil)
280
+ if values
281
+ values.class == Array ? arys = values : arys = [values]
282
+ arys.each {|cbx_value|
283
+ checkbox(:name, checkBoxName, cbx_value).set
284
+ }
285
+ else
286
+ checkbox(:name, checkBoxName).set
287
+ end
288
+ end
289
+
290
+ def uncheck_checkbox(checkBoxName, values = nil)
291
+ if values
292
+ values.class == Array ? arys = values : arys = [values]
293
+ arys.each {|cbx_value|
294
+ checkbox(:name, checkBoxName, cbx_value).clear
295
+ }
296
+ else
297
+ checkbox(:name, checkBoxName).clear
298
+ end
299
+ end
300
+
301
+
302
+ # the method is protected in JWebUnit
303
+ def click_radio_option(radio_group, radio_option)
304
+ radio(:name, radio_group, radio_option).set
305
+ end
306
+
307
+ def clear_radio_option(radio_group, radio_option)
308
+ radio(:name, radio_group, radio_option).clear
309
+ end
310
+
311
+ def element_by_id(elem_id)
312
+ if is_firefox?
313
+ # elem = @browser.document.getElementById(elem_id)
314
+ elem = div(:id, elem_id) || label(:id, elem_id) || button(:id, elem_id) || span(:id, elem_id) || hidden(:id, elem_id) || link(:id, elem_id) || radio(:id, elem_id)
315
+ else
316
+ elem = @browser.document.getElementById(elem_id)
317
+ end
318
+ end
319
+
320
+ def element_value(elementId)
321
+ if is_firefox? then
322
+ elem = element_by_id(elementId)
323
+ elem ? elem.invoke('innerText') : nil
324
+ else
325
+ elem = element_by_id(elementId)
326
+ elem ? elem.invoke('innerText') : nil
327
+ end
328
+ end
329
+
330
+ def element_source(elementId)
331
+ elem = element_by_id(elementId)
332
+ assert_not_nil(elem, "HTML element: #{elementId} not exists")
333
+ elem.innerHTML
334
+ end
335
+
336
+ def select_file_for_upload(file_field, file_path)
337
+ normalized_file_path = is_windows? ? file_path.gsub("/", "\\") : file_path
338
+ file_field(:name, file_field).set(normalized_file_path)
339
+ end
340
+
341
+ def start_window(url = nil)
342
+ @browser.start_window(url);
343
+ end
344
+
345
+ # Attach to existing browser
346
+ #
347
+ # Usage:
348
+ # WebBrowser.attach_browser(:title, "iTest2")
349
+ # WebBrowser.attach_browser(:url, "http://www.itest2.com")
350
+ # WebBrowser.attach_browser(:url, "http://www.itest2.com", {:browser => "Firefox", :base_url => "http://www.itest2.com"})
351
+ # WebBrowser.attach_browser(:title, /agileway\.com\.au\/attachment/) # regular expression
352
+ def self.attach_browser(how, what, options={})
353
+ default_options = {:browser => "IE"}
354
+ options = default_options.merge(options)
355
+ site_context = Context.new(options[:base_url]) if options[:base_url]
356
+ if (options[:browser] == "Firefox")
357
+ return WebBrowser.new_from_existing(FireWatir::Firefox.new.attach(how, what), site_context)
358
+ else
359
+ return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context)
360
+ end
361
+ end
362
+
363
+ # Attach a Watir::IE instance to a popup window.
364
+ #
365
+ # Typical usage
366
+ # new_popup_window(:url => "http://www.google.com/a.pdf")
367
+ def new_popup_window(options, browser = "ie")
368
+ if is_firefox?
369
+ raise "not implemented"
370
+ else
371
+ if options[:url]
372
+ Watir::IE.attach(:url, options[:url])
373
+ elsif options[:title]
374
+ Watir::IE.attach(:title, options[:title])
375
+ else
376
+ raise 'Please specify title or url of new pop up window'
377
+ end
378
+ end
379
+ end
380
+
381
+ # ---
382
+ # For deubgging
383
+ # ---
384
+ def dump_response(stream = nil)
385
+ stream.nil? ? puts(page_source) : stream.puts(page_source)
386
+ end
387
+
388
+ # A Better Popup Handler using the latest Watir version. Posted by Mark_cain@rl.gov
389
+ #
390
+ # http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F
391
+ #
392
+ def start_clicker( button, waitTime= 9, user_input=nil)
393
+ # get a handle if one exists
394
+ hwnd = @browser.enabled_popup(waitTime)
395
+ if (hwnd) # yes there is a popup
396
+ w = WinClicker.new
397
+ if ( user_input )
398
+ w.setTextValueForFileNameField( hwnd, "#{user_input}" )
399
+ end
400
+ # I put this in to see the text being input it is not necessary to work
401
+ sleep 3
402
+ # "OK" or whatever the name on the button is
403
+ w.clickWindowsButton_hwnd( hwnd, "#{button}" )
404
+ #
405
+ # this is just cleanup
406
+ w = nil
407
+ end
408
+ end
409
+
410
+ # return underlying browser
411
+ def ie
412
+ raise "can't call this as it is configured to use Firefox" if is_firefox?
413
+ @browser
414
+ end
415
+
416
+ def firefox
417
+ raise "can't call this as it is configured to use IE" unless is_firefox?
418
+ @browser
419
+ end
420
+
421
+ def save_page(file_name = nil)
422
+ file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
423
+ puts "about to save page: #{File.expand_path(file_name)}"
424
+ File.open(file_name, "w").puts page_source
425
+ end
426
+
427
+
428
+ def is_windows?
429
+ RUBY_PLATFORM.downcase.include?("mswin")
430
+ end
431
+
432
+ end
433
+ end