rwebspec 1.4.0.1 → 1.4.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,528 +1,566 @@
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
- begin
26
- require "rubygems";
27
- require "celerity";
28
- $celerity_loaded = true
29
- rescue LoadError => e
30
- $celerity_loaded = false
31
- end
32
-
33
- raise "You have must at least Watir or Firewatir installed" unless $watir_loaded || $firewatir_loaded || $celerity_loaded
34
-
35
- module RWebSpec
36
-
37
- ##
38
- # Wrapping WATIR IE and FireWatir Firefox
39
- #
40
- class WebBrowser
41
-
42
- attr_accessor :context
43
-
44
- def initialize(base_url = nil, existing_browser = nil, options = {})
45
- default_options = {:speed => "zippy",
46
- :visible => true,
47
- :highlight_colour => 'yellow',
48
- :close_others => true
49
- }
50
- options = default_options.merge options
51
- @context = Context.new base_url if base_url
52
-
53
- case RUBY_PLATFORM
54
- when /java/i
55
- # Java, maybe firewatir or celerity
56
- puts "Ruby java platform"
57
- raise "Not supported, no FireWatir or Celerity detected" unless $firewatir_loaded || $celerity_loaded
58
- if $firewatir_loaded && $celerity_loaded then
59
- # choose one out of two, :default to celerity
60
- if options[:firefox] then
61
- initialize_firefox_browser(existing_browser, base_url, options)
62
- else
63
- initialize_celerity_browser(base_url, options)
64
- end
65
- elsif $firewatir_loaded
66
- initialize_firefox_browser(existing_browser, base_url, options)
67
- else
68
- initialize_celerity_browser(base_url, options)
69
- end
70
-
71
- when /mswin|windows|mingw/i
72
- puts "Ruby windows platform"
73
- raise "Not supported, no Watir or FireWatir detected" unless $watir_loaded || $firewatir_loaded
74
- if $firewatir_loaded && options[:firefox] then
75
- initialize_firefox_browser(existing_browser, base_url, options)
76
- else
77
- initialize_ie_browser(existing_browser, options)
78
- end
79
- else
80
- raise "Not supported, no FireWatirdetected" unless $firewatir_loaded
81
- puts "Ruby Linux or Mac platform: firefox"
82
- initialize_firefox_browser(base_url, options)
83
- end
84
- end
85
-
86
- def initialize_firefox_browser(existing_browser, base_url, options)
87
- if existing_browser then
88
- @browser = existing_browser
89
- return
90
- end
91
- # JSSH is running, 9997
92
- begin
93
- require 'net/telnet'
94
- firefox_jssh = Net::Telnet::new("Host" => "127.0.0.1", "Port" => 9997)
95
- FireWatir::Firefox.firefox_started = true
96
- rescue => e
97
- # puts "debug: XXX #{e}"
98
- sleep 1
99
- end
100
- @browser = FireWatir::Firefox.start(base_url)
101
- end
102
-
103
- def initialize_celerity_browser(base_url, options)
104
- default_celerity_options = { :proxy => nil, :browser => :firefox, :resynchronize => true, :log_level => :off }
105
- options = default_celerity_options.merge options
106
- options.each { |k, v| options.delete(k) unless default_celerity_options.keys.include?(k)}
107
- puts "Starting Celerity: #{options.inspect}"
108
- @browser = Celerity::Browser.new(options)
109
- @browser.goto(base_url)
110
- end
111
-
112
- def initialize_ie_browser(existing_browser, options)
113
- if existing_browser then
114
- @browser = existing_browser
115
- return
116
- end
117
-
118
- @browser = Watir::IE.new
119
- if $ITEST2_EMULATE_TYPING && $ITEST2_TYPING_SPEED then
120
- @browser.set_slow_speed if $ITEST2_TYPING_SPEED == 'slow'
121
- @browser.set_fast_speed if $ITEST2_TYPING_SPEED == 'fast'
122
- else
123
- @browser.speed = :zippy
124
- end
125
- @browser.activeObjectHighLightColor = options[:highlight_colour]
126
- @browser.visible = options[:visible] unless $HIDE_IE
127
- #NOTE: close_others fails
128
- if RUBY_VERSION =~ /^1\.8/ && options[:close_others] then
129
- @browser.close_others
130
- else
131
- puts "close other browser instances not working yet in Ruby 1.9.1 version of Watir"
132
- end
133
- end
134
-
135
- def self.reuse(base_url, options)
136
- if RUBY_PLATFORM.downcase.include?("mswin") && $ITEST2_BROWSER != "Firefox"
137
- Watir::IE.each do |browser_window|
138
- return WebBrowser.new(base_url, browser_window, options)
139
- end
140
- puts "no browser instance found"
141
- WebBrowser.new(base_url, nil, options)
142
- else
143
- WebBrowser.new(base_url, nil, options)
144
- end
145
- end
146
-
147
- # for popup windows
148
- def self.new_from_existing(underlying_browser, web_context = nil)
149
- return WebBrowser.new(web_context ? web_context.base_url : nil, underlying_browser, {:close_others => false})
150
- end
151
-
152
-
153
- ##
154
- # Delegate to Watir
155
- #
156
- [: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|
157
- define_method method do |*args|
158
- @browser.send(method, *args)
159
- end
160
- end
161
- alias td cell
162
- alias check_box checkbox # seems watir doc is wrong, checkbox not check_box
163
- alias tr row
164
-
165
- # FireWatir does not support area directly, treat it as text_field
166
- def area(*args)
167
- if is_firefox?
168
- text_field(*args)
169
- else
170
- @browser.send("area", *args)
171
- end
172
- end
173
-
174
- def contains_text(text)
175
- @browser.contains_text(text);
176
- end
177
-
178
- def page_source
179
- @browser.html()
180
- #@browser.document.body
181
- end
182
-
183
- alias html_body page_source
184
-
185
- def html
186
- @browser.html
187
- end
188
-
189
- def text
190
- @browser.text
191
- end
192
-
193
- def page_title
194
- case @browser.class.to_s
195
- when "FireWatir::Firefox"
196
- @browser.title
197
- when "Watir::IE"
198
- @browser.document.title
199
- else
200
- @browser.title
201
- end
202
- end
203
-
204
- [:images, :links, :buttons, :select_lists, :checkboxes, :radios, :text_fields].each do |method|
205
- define_method method do
206
- @browser.send(method)
207
- end
208
- end
209
-
210
- # current url
211
- def url
212
- @browser.url
213
- end
214
-
215
- def base_url=(new_base_url)
216
- if @context
217
- @conext.base_url = new_base_url
218
- return
219
- end
220
- @context = Context.new base_url
221
- end
222
-
223
- def is_firefox?
224
- return false unless $firewatir_loaded
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
- case @browser.class.to_s
236
- when "FireWatir::Firefox"
237
- @browser.close
238
- when "Watir::IE"
239
- @browser.getIE.quit
240
- else
241
- puts "#{@browser.class} can't close, ignore"
242
- end
243
- sleep 2
244
- end
245
-
246
- alias close close_browser
247
-
248
- #TODO determine browser type, check FireWatir support or not
249
- def self.close_all_browsers
250
- if RUBY_PLATFORM.downcase.include?("mswin")
251
- Watir::IE.close_all
252
- else
253
- # raise "not supported in FireFox yet."
254
- end
255
- end
256
-
257
- def full_url(relative_url)
258
- if @context && @context.base_url
259
- @context.base_url + relative_url
260
- else
261
- relative_url
262
- end
263
- end
264
-
265
- def begin_at(relative_url)
266
- @browser.goto full_url(relative_url)
267
- end
268
-
269
- def browser_opened?
270
- begin
271
- @browser != nil
272
- rescue => e
273
- return false
274
- end
275
- end
276
-
277
- # Some browsers (i.e. IE) need to be waited on before more actions can be
278
- # performed. Most action methods in Watir::Simple already call this before
279
- # and after.
280
- def wait_for_browser
281
- if $celerity_loaded then
282
- # puts "ignore, using celerity"
283
- else
284
- @browser.waitForIE unless is_firefox?
285
- end
286
- end
287
-
288
-
289
- # A convenience method to wait at both ends of an operation for the browser
290
- # to catch up.
291
- def wait_before_and_after
292
- wait_for_browser
293
- yield
294
- wait_for_browser
295
- end
296
-
297
-
298
- [:back, :forward, :refresh, :focus, :close_others].each do |method|
299
- define_method(method) do
300
- @browser.send(method)
301
- end
302
- end
303
- alias refresh_page refresh
304
- alias go_back back
305
- alias go_forward forward
306
-
307
- def goto_page(page)
308
- @browser.goto full_url(page);
309
- end
310
-
311
- def goto_url(url)
312
- @browser.goto url
313
- end
314
-
315
- # text fields
316
- def enter_text_into_field_with_name(name, text)
317
- if is_firefox?
318
- wait_before_and_after { text_field(:name, name).value = text }
319
- sleep 0.3
320
- else
321
- wait_before_and_after { text_field(:name, name).set(text) }
322
- end
323
- end
324
-
325
- alias set_form_element enter_text_into_field_with_name
326
- alias enter_text enter_text_into_field_with_name
327
-
328
- #links
329
- def click_link_with_id(link_id)
330
- wait_before_and_after { link(:id, link_id).click }
331
- end
332
-
333
- def click_link_with_text(text)
334
- wait_before_and_after { link(:text, text).click }
335
- end
336
-
337
- ##
338
- # buttons
339
-
340
- def click_button_with_id(id)
341
- wait_before_and_after { button(:id, id).click }
342
- end
343
-
344
- def click_button_with_name(name)
345
- wait_before_and_after { button(:name, name).click }
346
- end
347
-
348
- def click_button_with_caption(caption)
349
- wait_before_and_after { button(:caption, caption).click }
350
- end
351
-
352
- def click_button_with_value(value)
353
- wait_before_and_after { button(:value, value).click }
354
- end
355
-
356
- def select_option(selectName, option)
357
- select_list(:name, selectName).select(option)
358
- end
359
-
360
- # submit first submit button
361
- def submit(buttonName = nil)
362
- if (buttonName.nil?) then
363
- buttons.each { |button|
364
- next if button.type != 'submit'
365
- button.click
366
- return
367
- }
368
- else
369
- click_button_with_name(buttonName)
370
- end
371
- end
372
-
373
- # checkbox
374
- def check_checkbox(checkBoxName, values=nil)
375
- if values
376
- values.class == Array ? arys = values : arys = [values]
377
- arys.each {|cbx_value|
378
- checkbox(:name, checkBoxName, cbx_value).set
379
- }
380
- else
381
- checkbox(:name, checkBoxName).set
382
- end
383
- end
384
-
385
- def uncheck_checkbox(checkBoxName, values = nil)
386
- if values
387
- values.class == Array ? arys = values : arys = [values]
388
- arys.each {|cbx_value|
389
- checkbox(:name, checkBoxName, cbx_value).clear
390
- }
391
- else
392
- checkbox(:name, checkBoxName).clear
393
- end
394
- end
395
-
396
-
397
- # the method is protected in JWebUnit
398
- def click_radio_option(radio_group, radio_option)
399
- radio(:name, radio_group, radio_option).set
400
- end
401
-
402
- def clear_radio_option(radio_group, radio_option)
403
- radio(:name, radio_group, radio_option).clear
404
- end
405
-
406
- def element_by_id(elem_id)
407
- if is_firefox?
408
- # elem = @browser.document.getElementById(elem_id)
409
- 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)
410
- else
411
- elem = @browser.document.getElementById(elem_id)
412
- end
413
- end
414
-
415
- def element_value(elementId)
416
- if is_firefox? then
417
- elem = element_by_id(elementId)
418
- elem ? elem.invoke('innerText') : nil
419
- else
420
- elem = element_by_id(elementId)
421
- elem ? elem.invoke('innerText') : nil
422
- end
423
- end
424
-
425
- def element_source(elementId)
426
- elem = element_by_id(elementId)
427
- assert_not_nil(elem, "HTML element: #{elementId} not exists")
428
- elem.innerHTML
429
- end
430
-
431
- def select_file_for_upload(file_field, file_path)
432
- normalized_file_path = RUBY_PLATFORM.downcase.include?("mswin") ? file_path.gsub("/", "\\") : file_path
433
- file_field(:name, file_field).set(normalized_file_path)
434
- end
435
-
436
- def start_window(url = nil)
437
- @browser.start_window(url);
438
- end
439
-
440
- # Attach to existing browser
441
- #
442
- # Usage:
443
- # WebBrowser.attach_browser(:title, "iTest2")
444
- # WebBrowser.attach_browser(:url, "http://www.itest2.com")
445
- # WebBrowser.attach_browser(:url, "http://www.itest2.com", {:browser => "Firefox", :base_url => "http://www.itest2.com"})
446
- # WebBrowser.attach_browser(:title, /agileway\.com\.au\/attachment/) # regular expression
447
- def self.attach_browser(how, what, options={})
448
- default_options = {:browser => "IE"}
449
- options = default_options.merge(options)
450
- site_context = Context.new(options[:base_url]) if options[:base_url]
451
- if (options[:browser] == "Firefox")
452
- return WebBrowser.new_from_existing(FireWatir::Firefox.new.attach(how, what), site_context)
453
- else
454
- return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context)
455
- end
456
- end
457
-
458
- # Attach a Watir::IE instance to a popup window.
459
- #
460
- # Typical usage
461
- # new_popup_window(:url => "http://www.google.com/a.pdf")
462
- def new_popup_window(options, browser = "ie")
463
- if is_firefox?
464
- raise "not implemented"
465
- else
466
- if options[:url]
467
- Watir::IE.attach(:url, options[:url])
468
- elsif options[:title]
469
- Watir::IE.attach(:title, options[:title])
470
- else
471
- raise 'Please specify title or url of new pop up window'
472
- end
473
- end
474
- end
475
-
476
- # ---
477
- # For deubgging
478
- # ---
479
- def dump_response(stream = nil)
480
- stream.nil? ? puts(page_source) : stream.puts(page_source)
481
- end
482
-
483
- # A Better Popup Handler using the latest Watir version. Posted by Mark_cain@rl.gov
484
- #
485
- # http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F
486
- #
487
- def start_clicker( button, waitTime= 9, user_input=nil)
488
- # get a handle if one exists
489
- hwnd = @browser.enabled_popup(waitTime)
490
- if (hwnd) # yes there is a popup
491
- w = WinClicker.new
492
- if ( user_input )
493
- w.setTextValueForFileNameField( hwnd, "#{user_input}" )
494
- end
495
- # I put this in to see the text being input it is not necessary to work
496
- sleep 3
497
- # "OK" or whatever the name on the button is
498
- w.clickWindowsButton_hwnd( hwnd, "#{button}" )
499
- #
500
- # this is just cleanup
501
- w = nil
502
- end
503
- end
504
-
505
- # return underlying browser
506
- def ie
507
- raise "can't call this as it is configured to use Firefox" if is_firefox?
508
- @browser
509
- end
510
-
511
- def firefox
512
- raise "can't call this as it is configured to use IE" unless is_firefox?
513
- @browser
514
- end
515
-
516
- def save_page(file_name = nil)
517
- file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
518
- puts "about to save page: #{File.expand_path(file_name)}"
519
- File.open(file_name, "w").puts page_source
520
- end
521
-
522
-
523
- def self.is_windows?
524
- RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw")
525
- end
526
-
527
- end
528
- 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
+ begin
26
+ require "rubygems";
27
+ require "celerity";
28
+ $celerity_loaded = true
29
+ rescue LoadError => e
30
+ $celerity_loaded = false
31
+ end
32
+
33
+ raise "You have must at least Watir or Firewatir installed" unless $watir_loaded || $firewatir_loaded || $celerity_loaded
34
+
35
+ module RWebSpec
36
+
37
+ ##
38
+ # Wrapping WATIR IE and FireWatir Firefox
39
+ #
40
+ class WebBrowser
41
+
42
+ attr_accessor :context
43
+
44
+ def initialize(base_url = nil, existing_browser = nil, options = {})
45
+ default_options = {:speed => "zippy",
46
+ :visible => true,
47
+ :highlight_colour => 'yellow',
48
+ :close_others => true
49
+ }
50
+ options = default_options.merge options
51
+ @context = Context.new base_url if base_url
52
+
53
+ case RUBY_PLATFORM
54
+ when /java/i
55
+ # Java, maybe firewatir or celerity
56
+ puts "Ruby java platform"
57
+ raise "Not supported, no FireWatir or Celerity detected" unless $firewatir_loaded || $celerity_loaded
58
+ if $firewatir_loaded && $celerity_loaded then
59
+ # choose one out of two, :default to celerity
60
+ if options[:firefox] then
61
+ initialize_firefox_browser(existing_browser, base_url, options)
62
+ else
63
+ initialize_celerity_browser(base_url, options)
64
+ end
65
+ elsif $firewatir_loaded
66
+ initialize_firefox_browser(existing_browser, base_url, options)
67
+ else
68
+ initialize_celerity_browser(base_url, options)
69
+ end
70
+
71
+ when /mswin|windows|mingw/i
72
+ raise "Not supported, no Watir or FireWatir detected" unless $watir_loaded || $firewatir_loaded
73
+ if $firewatir_loaded && options[:firefox] then
74
+ initialize_firefox_browser(existing_browser, base_url, options)
75
+ else
76
+ initialize_ie_browser(existing_browser, options)
77
+ end
78
+ else
79
+ raise "Not supported, no FireWatirdetected" unless $firewatir_loaded
80
+ puts "Ruby Linux or Mac platform: firefox"
81
+ initialize_firefox_browser(existing_browser, base_url, options)
82
+ end
83
+ end
84
+
85
+ def initialize_firefox_browser(existing_browser, base_url, options)
86
+ if existing_browser then
87
+ @browser = existing_browser
88
+ return
89
+ end
90
+ # JSSH is running, 9997
91
+ begin
92
+ require 'net/telnet'
93
+ firefox_jssh = Net::Telnet::new("Host" => "127.0.0.1", "Port" => 9997)
94
+ FireWatir::Firefox.firefox_started = true
95
+ rescue => e
96
+ # puts "debug: XXX #{e}"
97
+ sleep 1
98
+ end
99
+ @browser = FireWatir::Firefox.start(base_url)
100
+ end
101
+
102
+ def initialize_celerity_browser(base_url, options)
103
+ default_celerity_options = { :proxy => nil, :browser => :firefox, :resynchronize => true, :log_level => :off }
104
+ options = default_celerity_options.merge options
105
+ options.each { |k, v| options.delete(k) unless default_celerity_options.keys.include?(k)}
106
+ puts "Starting Celerity: #{options.inspect}"
107
+ @browser = Celerity::Browser.new(options)
108
+ @browser.goto(base_url)
109
+ end
110
+
111
+ def initialize_ie_browser(existing_browser, options)
112
+ if existing_browser then
113
+ @browser = existing_browser
114
+ return
115
+ end
116
+
117
+ @browser = Watir::IE.new
118
+ if $ITEST2_EMULATE_TYPING && $ITEST2_TYPING_SPEED then
119
+ @browser.set_slow_speed if $ITEST2_TYPING_SPEED == 'slow'
120
+ @browser.set_fast_speed if $ITEST2_TYPING_SPEED == 'fast'
121
+ else
122
+ @browser.speed = :zippy
123
+ end
124
+ @browser.activeObjectHighLightColor = options[:highlight_colour]
125
+ @browser.visible = options[:visible] unless $HIDE_IE
126
+ #NOTE: close_others fails
127
+ if RUBY_VERSION =~ /^1\.8/ && options[:close_others] then
128
+ @browser.close_others
129
+ else
130
+ puts "close other browser instances not working yet in Ruby 1.9.1 version of Watir"
131
+ end
132
+ end
133
+
134
+ def self.reuse(base_url, options)
135
+ if self.is_windows? && $ITEST2_BROWSER != "Firefox"
136
+ Watir::IE.each do |browser_window|
137
+ return WebBrowser.new(base_url, browser_window, options)
138
+ end
139
+ puts "no browser instance found"
140
+ WebBrowser.new(base_url, nil, options)
141
+ else
142
+ WebBrowser.new(base_url, nil, options)
143
+ end
144
+ end
145
+
146
+ # for popup windows
147
+ def self.new_from_existing(underlying_browser, web_context = nil)
148
+ return WebBrowser.new(web_context ? web_context.base_url : nil, underlying_browser, {:close_others => false})
149
+ end
150
+
151
+
152
+ ##
153
+ # Delegate to Watir
154
+ #
155
+ [: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|
156
+ define_method method do |*args|
157
+ @browser.send(method, *args)
158
+ end
159
+ end
160
+ alias td cell
161
+ alias check_box checkbox # seems watir doc is wrong, checkbox not check_box
162
+ alias tr row
163
+
164
+ # Wrapp of Watir's area to support Firefox and Watir
165
+ #
166
+ # Note: FireWatir does not support area directly, treat it as text_field
167
+ def area(*args)
168
+ if is_firefox?
169
+ text_field(*args)
170
+ else
171
+ @browser.send("area", *args)
172
+ end
173
+ end
174
+
175
+ def contains_text(text)
176
+ @browser.contains_text(text);
177
+ end
178
+
179
+ # return HTML of current web page
180
+ def page_source
181
+ @browser.html()
182
+ #@browser.document.body
183
+ end
184
+ alias html_body page_source
185
+ alias html page_source
186
+
187
+
188
+ # return plain text of current web page
189
+ def text
190
+ @browser.text
191
+ end
192
+
193
+ def page_title
194
+ case @browser.class.to_s
195
+ when "FireWatir::Firefox"
196
+ @browser.title
197
+ when "Watir::IE"
198
+ @browser.document.title
199
+ else
200
+ @browser.title
201
+ end
202
+ end
203
+
204
+ [:images, :links, :buttons, :select_lists, :checkboxes, :radios, :text_fields].each do |method|
205
+ define_method method do
206
+ @browser.send(method)
207
+ end
208
+ end
209
+
210
+ # current url
211
+ def url
212
+ @browser.url
213
+ end
214
+
215
+ def base_url=(new_base_url)
216
+ if @context
217
+ @conext.base_url = new_base_url
218
+ return
219
+ end
220
+ @context = Context.new base_url
221
+ end
222
+
223
+ def is_firefox?
224
+ return false unless $firewatir_loaded
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
+ case @browser.class.to_s
236
+ when "FireWatir::Firefox"
237
+ @browser.close
238
+ when "Watir::IE"
239
+ @browser.getIE.quit
240
+ else
241
+ puts "#{@browser.class} can't close, ignore"
242
+ end
243
+ sleep 2
244
+ end
245
+
246
+ alias close close_browser
247
+
248
+ #TODO determine browser type, check FireWatir support or not
249
+ def self.close_all_browsers
250
+ if RUBY_PLATFORM.downcase.include?("mswin")
251
+ Watir::IE.close_all
252
+ else
253
+ # raise "not supported in FireFox yet."
254
+ end
255
+ end
256
+
257
+ def full_url(relative_url)
258
+ if @context && @context.base_url
259
+ @context.base_url + relative_url
260
+ else
261
+ relative_url
262
+ end
263
+ end
264
+
265
+ def begin_at(relative_url)
266
+ @browser.goto full_url(relative_url)
267
+ end
268
+
269
+ def browser_opened?
270
+ begin
271
+ @browser != nil
272
+ rescue => e
273
+ return false
274
+ end
275
+ end
276
+
277
+ # Some browsers (i.e. IE) need to be waited on before more actions can be
278
+ # performed. Most action methods in Watir::Simple already call this before
279
+ # and after.
280
+ def wait_for_browser
281
+ if $celerity_loaded then
282
+ # puts "ignore, using celerity"
283
+ else
284
+ @browser.waitForIE unless is_firefox?
285
+ end
286
+ end
287
+
288
+
289
+ # A convenience method to wait at both ends of an operation for the browser
290
+ # to catch up.
291
+ def wait_before_and_after
292
+ wait_for_browser
293
+ yield
294
+ wait_for_browser
295
+ end
296
+
297
+
298
+ [:back, :forward, :refresh, :focus, :close_others].each do |method|
299
+ define_method(method) do
300
+ @browser.send(method)
301
+ end
302
+ end
303
+ alias refresh_page refresh
304
+ alias go_back back
305
+ alias go_forward forward
306
+
307
+ # Go to a page
308
+ # Usage:
309
+ # open_browser("http://www.itest2.com"
310
+ # ....
311
+ # goto_page("/purchase") # full url => http://www.itest.com/purchase
312
+ def goto_page(page)
313
+ @browser.goto full_url(page);
314
+ end
315
+
316
+ # Go to a URL directly
317
+ # goto_url("http://www.itest2.com/downloads")
318
+ def goto_url(url)
319
+ @browser.goto url
320
+ end
321
+
322
+ # text fields
323
+ def enter_text_into_field_with_name(name, text)
324
+ if is_firefox?
325
+ wait_before_and_after { text_field(:name, name).value = text }
326
+ sleep 0.3
327
+ else
328
+ wait_before_and_after { text_field(:name, name).set(text) }
329
+ end
330
+ end
331
+
332
+ alias set_form_element enter_text_into_field_with_name
333
+ alias enter_text enter_text_into_field_with_name
334
+
335
+ #links
336
+ def click_link_with_id(link_id)
337
+ wait_before_and_after { link(:id, link_id).click }
338
+ end
339
+
340
+ def click_link_with_text(text)
341
+ wait_before_and_after { link(:text, text).click }
342
+ end
343
+
344
+ # Click a button with give HTML id
345
+ # Usage:
346
+ # click_button_with_id("btn_sumbit")
347
+ def click_button_with_id(id)
348
+ wait_before_and_after { button(:id, id).click }
349
+ end
350
+
351
+ # Click a button with give name
352
+ # Usage:
353
+ # click_button_with_name("confirm")
354
+ def click_button_with_name(name)
355
+ wait_before_and_after { button(:name, name).click }
356
+ end
357
+
358
+ # Click a button with caption
359
+ # Usage:
360
+ # click_button_with_caption("Confirm payment")
361
+ def click_button_with_caption(caption)
362
+ wait_before_and_after { button(:caption, caption).click }
363
+ end
364
+
365
+ # Click a button with value
366
+ # Usage:
367
+ # click_button_with_value("Confirm payment")
368
+ def click_button_with_value(value)
369
+ wait_before_and_after { button(:value, value).click }
370
+ end
371
+
372
+ # Select a dropdown list by name
373
+ # Usage:
374
+ # select_option("country", "Australia")
375
+ def select_option(selectName, option)
376
+ select_list(:name, selectName).select(option)
377
+ end
378
+
379
+ # submit first submit button
380
+ def submit(buttonName = nil)
381
+ if (buttonName.nil?) then
382
+ buttons.each { |button|
383
+ next if button.type != 'submit'
384
+ button.click
385
+ return
386
+ }
387
+ else
388
+ click_button_with_name(buttonName)
389
+ end
390
+ end
391
+
392
+ # Check a checkbox
393
+ # Usage:
394
+ # check_checkbox("agree")
395
+ # check_checkbox("agree", "true")
396
+ def check_checkbox(checkBoxName, values=nil)
397
+ if values
398
+ values.class == Array ? arys = values : arys = [values]
399
+ arys.each {|cbx_value|
400
+ checkbox(:name, checkBoxName, cbx_value).set
401
+ }
402
+ else
403
+ checkbox(:name, checkBoxName).set
404
+ end
405
+ end
406
+
407
+ # Check a checkbox
408
+ # Usage:
409
+ # uncheck_checkbox("agree")
410
+ # uncheck_checkbox("agree", "false")
411
+ def uncheck_checkbox(checkBoxName, values = nil)
412
+ if values
413
+ values.class == Array ? arys = values : arys = [values]
414
+ arys.each {|cbx_value|
415
+ checkbox(:name, checkBoxName, cbx_value).clear
416
+ }
417
+ else
418
+ checkbox(:name, checkBoxName).clear
419
+ end
420
+ end
421
+
422
+
423
+ # Click a radio button
424
+ # Usage:
425
+ # click_radio_option("country", "Australia")
426
+ def click_radio_option(radio_group, radio_option)
427
+ radio(:name, radio_group, radio_option).set
428
+ end
429
+
430
+ # Clear a radio button
431
+ # Usage:
432
+ # click_radio_option("country", "Australia")
433
+ def clear_radio_option(radio_group, radio_option)
434
+ radio(:name, radio_group, radio_option).clear
435
+ end
436
+
437
+ # Deprecated: using Watir style directly instead
438
+ def element_by_id(elem_id)
439
+ if is_firefox?
440
+ # elem = @browser.document.getElementById(elem_id)
441
+ 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)
442
+ else
443
+ elem = @browser.document.getElementById(elem_id)
444
+ end
445
+ end
446
+
447
+ def element_value(elementId)
448
+ if is_firefox? then
449
+ elem = element_by_id(elementId)
450
+ elem ? elem.invoke('innerText') : nil
451
+ else
452
+ elem = element_by_id(elementId)
453
+ elem ? elem.invoke('innerText') : nil
454
+ end
455
+ end
456
+
457
+ def element_source(elementId)
458
+ elem = element_by_id(elementId)
459
+ assert_not_nil(elem, "HTML element: #{elementId} not exists")
460
+ elem.innerHTML
461
+ end
462
+
463
+ def select_file_for_upload(file_field, file_path)
464
+ normalized_file_path = RUBY_PLATFORM.downcase.include?("mswin") ? file_path.gsub("/", "\\") : file_path
465
+ file_field(:name, file_field).set(normalized_file_path)
466
+ end
467
+
468
+ def start_window(url = nil)
469
+ @browser.start_window(url);
470
+ end
471
+
472
+ # Attach to existing browser
473
+ #
474
+ # Usage:
475
+ # WebBrowser.attach_browser(:title, "iTest2")
476
+ # WebBrowser.attach_browser(:url, "http://www.itest2.com")
477
+ # WebBrowser.attach_browser(:url, "http://www.itest2.com", {:browser => "Firefox", :base_url => "http://www.itest2.com"})
478
+ # WebBrowser.attach_browser(:title, /agileway\.com\.au\/attachment/) # regular expression
479
+ def self.attach_browser(how, what, options={})
480
+ default_options = {:browser => "IE"}
481
+ options = default_options.merge(options)
482
+ site_context = Context.new(options[:base_url]) if options[:base_url]
483
+ if (options[:browser] == "Firefox")
484
+ return WebBrowser.new_from_existing(FireWatir::Firefox.new.attach(how, what), site_context)
485
+ else
486
+ return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context)
487
+ end
488
+ end
489
+
490
+ # Attach a Watir::IE instance to a popup window.
491
+ #
492
+ # Typical usage
493
+ # new_popup_window(:url => "http://www.google.com/a.pdf")
494
+ def new_popup_window(options, browser = "ie")
495
+ if is_firefox?
496
+ raise "not implemented"
497
+ else
498
+ if options[:url]
499
+ Watir::IE.attach(:url, options[:url])
500
+ elsif options[:title]
501
+ Watir::IE.attach(:title, options[:title])
502
+ else
503
+ raise 'Please specify title or url of new pop up window'
504
+ end
505
+ end
506
+ end
507
+
508
+ # ---
509
+ # For deubgging
510
+ # ---
511
+ def dump_response(stream = nil)
512
+ stream.nil? ? puts(page_source) : stream.puts(page_source)
513
+ end
514
+
515
+ # A Better Popup Handler using the latest Watir version. Posted by Mark_cain@rl.gov
516
+ #
517
+ # http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F
518
+ #
519
+ def start_clicker( button, waitTime= 9, user_input=nil)
520
+ # get a handle if one exists
521
+ hwnd = @browser.enabled_popup(waitTime)
522
+ if (hwnd) # yes there is a popup
523
+ w = WinClicker.new
524
+ if ( user_input )
525
+ w.setTextValueForFileNameField( hwnd, "#{user_input}" )
526
+ end
527
+ # I put this in to see the text being input it is not necessary to work
528
+ sleep 3
529
+ # "OK" or whatever the name on the button is
530
+ w.clickWindowsButton_hwnd( hwnd, "#{button}" )
531
+ #
532
+ # this is just cleanup
533
+ w = nil
534
+ end
535
+ end
536
+
537
+ # return underlying browser
538
+ def ie
539
+ raise "can't call this as it is configured to use Firefox" if is_firefox?
540
+ @browser
541
+ end
542
+
543
+ # return underlying firefox browser object, raise error if not running using Firefox
544
+ def firefox
545
+ raise "can't call this as it is configured to use IE" unless is_firefox?
546
+ @browser
547
+ end
548
+
549
+ # Save current web page source to file
550
+ # usage:
551
+ # save_page("/tmp/01.html")
552
+ # save_page() => # will save to "20090830112200.html"
553
+ def save_page(file_name = nil)
554
+ file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
555
+ puts "about to save page: #{File.expand_path(file_name)}" if $DEBUG
556
+ File.open(file_name, "w").puts page_source
557
+ end
558
+
559
+
560
+ # is it running in MS Windows platforms?
561
+ def self.is_windows?
562
+ RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw")
563
+ end
564
+
565
+ end
566
+ end