rwebspec 3.1.4 → 4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,611 +1,611 @@
1
- #***********************************************************
2
- #* Copyright (c) 2006, Zhimin Zhan.
3
- #* Distributed open-source, see full license in MIT-LICENSE
4
- #***********************************************************
5
- begin
6
- require 'watir'
7
- # require 'watir/contrib/enabled_popup'
8
- # require 'watir/close_all'
9
- # require 'watir/screen_capture'
10
- # NO need any more
11
- require 'watir-classic/ie'
12
- # require 'watir/contrib/visible'
13
- $watir_loaded = true
14
- rescue LoadError => e
15
- puts "!!!Error on loading watir: #{e}"
16
- $watir_loaded = false
17
- end
18
-
19
- if RUBY_PLATFORM =~ /mingw/
20
- raise "You have must at least Watir installed" unless $watir_loaded
21
- end
22
-
23
- module RWebSpec
24
-
25
- ##
26
- # Wrapping WATIR IE and FireWatir Firefox
27
- #
28
- class WebBrowser
29
-
30
- attr_accessor :context
31
-
32
- def initialize(base_url = nil, existing_browser = nil, options = {})
33
- default_options = {:speed => "zippy",
34
- :visible => true,
35
- :highlight_colour => 'yellow',
36
- :close_others => true
37
- }
38
- options = default_options.merge options
39
- @context = Context.new base_url if base_url
40
-
41
- initialize_ie_browser(existing_browser, options)
42
- end
43
-
44
-
45
- def initialize_ie_browser(existing_browser, options)
46
- @browser = existing_browser || Watir::IE.new
47
- if ($TESTWISE_EMULATE_TYPING && $TESTWISE_TYPING_SPEED) then
48
- @browser.set_slow_speed if $TESTWISE_TYPING_SPEED == "slow"
49
- @browser.set_fast_speed if $TESTWISE_TYPING_SPEED == 'fast'
50
- else
51
- @browser.speed = :zippy
52
- end
53
-
54
- return if existing_browser
55
-
56
- @browser.activeObjectHighLightColor = options[:highlight_colour]
57
- @browser.visible = options[:visible] unless $HIDE_IE
58
- #NOTE: close_others fails
59
- if RUBY_VERSION =~ /^1\.8/ && options[:close_others] then
60
- begin
61
- @browser.close_others
62
- rescue => e1
63
- puts "Failed to close others"
64
- end
65
- else
66
- puts "close other browser instances not working yet in Ruby 1.9.1 version of Watir"
67
- end
68
- end
69
-
70
- def self.reuse(base_url, options)
71
- if self.is_windows? && ($TESTWISE_BROWSER != "Firefox" && $TESTWISE_BROWSER != "Firefox")
72
- require 'watir-classic/ie'
73
- # try to avoid
74
- # lib/ruby/1.8/dl/win32.rb:11:in `sym': unknown type specifier 'v'
75
- Watir::IE.each do |browser_window|
76
- return WebBrowser.new(base_url, browser_window, options)
77
- end
78
- #puts "no browser instance found"
79
- WebBrowser.new(base_url, nil, options)
80
- else
81
- WebBrowser.new(base_url, nil, options)
82
- end
83
- end
84
-
85
- # for popup windows
86
- def self.new_from_existing(underlying_browser, web_context = nil)
87
- return WebBrowser.new(web_context ? web_context.base_url : nil, underlying_browser, {:close_others => false})
88
- end
89
-
90
-
91
- ##
92
- # Delegate to Watir
93
- #
94
- [:button, :td, :checkbox, :div, :form, :frame, :h1, :h2, :h3, :h4, :h5, :h6, :hidden, :image, :li, :link, :map, :pre, :tr, :radio, :select_list, :span, :table, :text_field, :paragraph, :file_field, :label].each do |method|
95
- define_method method do |*args|
96
- @browser.send(method, *args)
97
- end
98
- end
99
- alias cell td
100
- alias check_box checkbox # seems watir doc is wrong, checkbox not check_box
101
- alias row tr
102
- alias a link
103
- alias img image
104
-
105
- def area(*args)
106
- @browser.send("area", *args)
107
- end
108
-
109
- def modal_dialog(how=nil, what=nil)
110
- @browser.modal_dialog(how, what)
111
- end
112
-
113
- # This is the main method for accessing a generic element with a given attibute
114
- # * how - symbol - how we access the element. Supports all values except :index and :xpath
115
- # * what - string, integer or regular expression - what we are looking for,
116
- #
117
- # Valid values for 'how' are listed in the Watir Wiki - http://wiki.openqa.org/display/WTR/Methods+supported+by+Element
118
- #
119
- # returns an Watir::Element object
120
- #
121
- # Typical Usage
122
- #
123
- # element(:class, /foo/) # access the first element with class 'foo'. We can use a string in place of the regular expression
124
- # element(:id, "11") # access the first element that matches an id
125
- def element(how, what)
126
- return @browser.element(how, what)
127
- end
128
-
129
- # this is the main method for accessing generic html elements by an attribute
130
- #
131
- # Returns a HTMLElements object
132
- #
133
- # Typical usage:
134
- #
135
- # elements(:class, 'test').each { |l| puts l.to_s } # iterate through all elements of a given attribute
136
- # elements(:alt, 'foo')[1].to_s # get the first element of a given attribute
137
- # elements(:id, 'foo').length # show how many elements are foung in the collection
138
- #
139
- def elements(how, what)
140
- return @browser.elements(how, what)
141
- end
142
-
143
- def show_all_objects
144
- @browser.show_all_objects
145
- end
146
-
147
- # Returns the specified ole object for input elements on a web page.
148
- #
149
- # This method is used internally by Watir and should not be used externally. It cannot be marked as private because of the way mixins and inheritance work in watir
150
- #
151
- # * how - symbol - the way we look for the object. Supported values are
152
- # - :name
153
- # - :id
154
- # - :index
155
- # - :value etc
156
- # * what - string that we are looking for, ex. the name, or id tag attribute or index of the object we are looking for.
157
- # * types - what object types we will look at.
158
- # * value - used for objects that have one name, but many values. ex. radio lists and checkboxes
159
- def locate_input_element(how, what, types, value=nil)
160
- @browser.locate_input_element(how, what, types, value)
161
- end
162
-
163
- # This is the main method for accessing map tags - http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/map.asp?frame=true
164
- # * how - symbol - how we access the map,
165
- # * what - string, integer or regular expression - what we are looking for,
166
- #
167
- # Valid values for 'how' are listed in the Watir Wiki - http://wiki.openqa.org/display/WTR/Methods+supported+by+Element
168
- #
169
- # returns a map object
170
- #
171
- # Typical Usage
172
- #
173
- # map(:id, /list/) # access the first map that matches list.
174
- # map(:index,2) # access the second map on the page
175
- # map(:title, "A Picture") # access a map using the tooltip text. See http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/title_1.asp?frame=true
176
- #
177
- def map(how, what=nil)
178
- @browser.map(how, what)
179
- end
180
-
181
- def contains_text(text)
182
- @browser.contains_text(text);
183
- end
184
-
185
- # return HTML of current web page
186
- def page_source
187
- @browser.html()
188
- #@browser.document.body
189
- end
190
- alias html_body page_source
191
- alias html page_source
192
-
193
-
194
- # return plain text of current web page
195
- def text
196
- @browser.text
197
- end
198
-
199
- def page_title
200
- case @browser.class.to_s
201
- when "Watir::IE"
202
- @browser.document.title
203
- else
204
- @browser.title
205
- end
206
- end
207
-
208
- [:images, :links, :buttons, :select_lists, :checkboxes, :radios, :text_fields, :divs, :dls, :dds, :dts, :ems, :lis, :maps, :spans, :strongs, :ps, :pres, :labels, :tds, :trs].each do |method|
209
- define_method method do
210
- @browser.send(method)
211
- end
212
- end
213
- alias as links
214
- alias rows trs
215
- alias cells tds
216
- alias imgs images
217
-
218
- # current url
219
- def url
220
- @browser.url
221
- end
222
-
223
- def base_url=(new_base_url)
224
- if @context
225
- @conext.base_url = new_base_url
226
- return
227
- end
228
- @context = Context.new base_url
229
- end
230
-
231
- def is_firefox?
232
- return false
233
- end
234
-
235
- # Close the browser window. Useful for automated test suites to reduce
236
- # test interaction.
237
- def close_browser
238
- @browser.close
239
- sleep 2
240
- end
241
- alias close close_browser
242
-
243
- def self.close_all_browsers(browser_type = :ie)
244
- if browser_type == :ie
245
- Watir::IE.close_all
246
- end
247
- end
248
-
249
- def full_url(relative_url)
250
- if @context && @context.base_url
251
- @context.base_url + relative_url
252
- else
253
- relative_url
254
- end
255
- end
256
-
257
- def begin_at(relative_url)
258
- @browser.goto full_url(relative_url)
259
- end
260
-
261
- def browser_opened?
262
- begin
263
- @browser != nil
264
- rescue => e
265
- return false
266
- end
267
- end
268
-
269
- # Some browsers (i.e. IE) need to be waited on before more actions can be
270
- # performed. Most action methods in Watir::Simple already call this before
271
- # and after.
272
- def wait_for_browser
273
- # Watir 3 does not support it any more
274
- # @browser.waitForIE unless is_firefox?
275
- end
276
-
277
-
278
- # A convenience method to wait at both ends of an operation for the browser
279
- # to catch up.
280
- def wait_before_and_after
281
- wait_for_browser
282
- yield
283
- wait_for_browser
284
- end
285
-
286
-
287
- [:back, :forward, :refresh, :focus, :close_others].each do |method|
288
- define_method(method) do
289
- @browser.send(method)
290
- end
291
- end
292
- alias refresh_page refresh
293
- alias go_back back
294
- alias go_forward forward
295
-
296
- # Go to a page
297
- # Usage:
298
- # open_browser("http://www.itest2.com"
299
- # ....
300
- # goto_page("/purchase") # full url => http://www.itest.com/purchase
301
- def goto_page(page)
302
- # puts "DEBUG calling goto page => #{page}"
303
- @browser.goto full_url(page);
304
- end
305
-
306
- # Go to a URL directly
307
- # goto_url("http://www.itest2.com/downloads")
308
- def goto_url(url)
309
- @browser.goto url
310
- end
311
-
312
- # text fields
313
- def enter_text_into_field_with_name(name, text)
314
- if is_firefox?
315
- wait_before_and_after { text_field(:name, name).value = text }
316
- sleep 0.3
317
- else
318
- wait_before_and_after { text_field(:name, name).set(text) }
319
- end
320
- end
321
-
322
- alias set_form_element enter_text_into_field_with_name
323
- alias enter_text enter_text_into_field_with_name
324
- alias set_hidden_field set_form_element
325
-
326
- #links
327
- def click_link_with_id(link_id, opts = {})
328
- if opts && opts[:index]
329
- wait_before_and_after { link(:id => link_id, :index => opts[:index]).click }
330
- else
331
- wait_before_and_after { link(:id, link_id).click }
332
- end
333
- end
334
-
335
- def click_link_with_text(text, opts = {})
336
- if opts && opts[:index]
337
- wait_before_and_after { link(:text => text, :index => opts[:index]).click }
338
- else
339
- wait_before_and_after { link(:text, text).click }
340
- end
341
- end
342
- alias click_link click_link_with_text
343
-
344
-
345
- # Click a button with give HTML id
346
- # Usage:
347
- # click_button_with_id("btn_sumbit")
348
- def click_button_with_id(id, opts = {})
349
- if opts && opts[:index]
350
- wait_before_and_after { button(:id => id, :index => opts[:index]).click }
351
- else
352
- wait_before_and_after { button(:id, id).click }
353
- end
354
- end
355
-
356
- # Click a button with give name
357
- # Usage:
358
- # click_button_with_name("confirm")
359
- def click_button_with_name(name, opts={})
360
- if opts && opts[:index]
361
- wait_before_and_after { button(:name => name, :index => opts[:index]).click }
362
- else
363
- wait_before_and_after { button(:name, name).click }
364
- end
365
- end
366
-
367
- # Click a button with caption
368
- # Usage:
369
- # click_button_with_caption("Confirm payment")
370
- def click_button_with_caption(caption, opts={})
371
- if opts && opts[:index]
372
- wait_before_and_after { button(:caption => caption, :index => opts[:index]).click }
373
- else
374
- wait_before_and_after { button(:caption, caption).click }
375
- end
376
- end
377
- alias click_button click_button_with_caption
378
- alias click_button_with_text click_button_with_caption
379
-
380
- # Click a button with value
381
- # Usage:
382
- # click_button_with_value("Confirm payment")
383
- def click_button_with_value(value, opts={})
384
- if opts && opts[:index]
385
- wait_before_and_after { button(:value => value, :index => opts[:index]).click }
386
- else
387
- wait_before_and_after { button(:value, value).click }
388
- end
389
- end
390
-
391
- # Select a dropdown list by name
392
- # Usage:
393
- # select_option("country", "Australia")
394
- def select_option(selectName, option)
395
- select_list(:name, selectName).select(option)
396
- end
397
-
398
- # submit first submit button
399
- def submit(buttonName = nil)
400
- if (buttonName.nil?) then
401
- buttons.each { |button|
402
- next if button.type != 'submit'
403
- button.click
404
- return
405
- }
406
- else
407
- click_button_with_name(buttonName)
408
- end
409
- end
410
-
411
- # Check a checkbox
412
- # Usage:
413
- # check_checkbox("agree")
414
- # check_checkbox("agree", "true")
415
- def check_checkbox(checkBoxName, values=nil)
416
- if values
417
- values.class == Array ? arys = values : arys = [values]
418
- arys.each {|cbx_value|
419
- if Watir::VERSION =~ /^1/ then
420
- checkbox(:name, checkBoxName, cbx_value).set
421
- else
422
- checkbox(:name => checkBoxName, :value => cbx_value).set
423
- end
424
- }
425
- else
426
- checkbox(:name, checkBoxName).set
427
- end
428
- end
429
-
430
- # Check a checkbox
431
- # Usage:
432
- # uncheck_checkbox("agree")
433
- # uncheck_checkbox("agree", "false")
434
- def uncheck_checkbox(checkBoxName, values = nil)
435
- if values
436
- values.class == Array ? arys = values : arys = [values]
437
- arys.each {|cbx_value|
438
- if Watir::VERSION =~ /^1/ then
439
- checkbox(:name, checkBoxName, cbx_value).clear
440
- else
441
- checkbox(:name => checkBoxName, :value => cbx_value).clear
442
- end
443
- }
444
- else
445
- checkbox(:name, checkBoxName).clear
446
- end
447
- end
448
-
449
-
450
- # Click a radio button
451
- # Usage:
452
- # click_radio_option("country", "Australia")
453
- def click_radio_option(radio_group, radio_option)
454
- if Watir::VERSION =~ /^1/ then
455
- radio(:name, radio_group, radio_option).set
456
- else
457
- radio(:name => radio_group, :value => radio_option).set
458
- end
459
- end
460
- alias click_radio_button click_radio_option
461
-
462
- # Clear a radio button
463
- # Usage:
464
- # click_radio_option("country", "Australia")
465
- def clear_radio_option(radio_group, radio_option)
466
- if Watir::VERSION =~ /^2/ then
467
- radio(:name => radio_group, :value => radio_option).clear
468
- else
469
- radio(:name, radio_group, radio_option).clear
470
- end
471
- end
472
- alias clear_radio_button clear_radio_option
473
-
474
- # Deprecated: using Watir style directly instead
475
- def element_by_id(elem_id)
476
- if is_firefox?
477
- # elem = @browser.document.getElementById(elem_id)
478
- # elem = div(:id, elem_id) || label(:id, elem_id) || button(:id, elem_id) ||
479
- # span(:id, elem_id) || hidden(:id, elem_id) || link(:id, elem_id) || radio(:id, elem_id)
480
- elem = browser.element_by_xpath("//*[@id='#{elem_id}']")
481
- else
482
- elem = @browser.document.getElementById(elem_id)
483
- end
484
- end
485
-
486
- def element_value(elementId)
487
- if is_firefox? then
488
- elem = element_by_id(elementId)
489
- elem ? elem.invoke('innerText') : nil
490
- else
491
- elem = element_by_id(elementId)
492
- elem ? elem.invoke('innerText') : nil
493
- end
494
- end
495
-
496
- def element_source(elementId)
497
- elem = element_by_id(elementId)
498
- assert_not_nil(elem, "HTML element: #{elementId} not exists")
499
- elem.innerHTML
500
- end
501
-
502
- def select_file_for_upload(file_field, file_path)
503
- normalized_file_path = RUBY_PLATFORM.downcase.include?("mingw") ? file_path.gsub("/", "\\") : file_path
504
- file_field(:name, file_field).set(normalized_file_path)
505
- end
506
-
507
- # Watir 1.9
508
- def javascript_dialog
509
- @browser.javascript_dialog
510
- end
511
-
512
- def start_window(url = nil)
513
- @browser.start_window(url);
514
- end
515
-
516
- # Attach to existing browser
517
- #
518
- # Usage:
519
- # WebBrowser.attach_browser(:title, "iTest2")
520
- # WebBrowser.attach_browser(:url, "http://www.itest2.com")
521
- # WebBrowser.attach_browser(:url, "http://www.itest2.com", {:browser => "Firefox", :base_url => "http://www.itest2.com"})
522
- # WebBrowser.attach_browser(:title, /agileway\.com\.au\/attachment/) # regular expression
523
- def self.attach_browser(how, what, options={})
524
- default_options = {:browser => "IE"}
525
- options = default_options.merge(options)
526
- site_context = Context.new(options[:base_url]) if options[:base_url]
527
- return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context)
528
- end
529
-
530
- # Attach a Watir::IE instance to a popup window.
531
- #
532
- # Typical usage
533
- # new_popup_window(:url => "http://www.google.com/a.pdf")
534
- def new_popup_window(options, browser = "ie")
535
- if is_firefox?
536
- raise "not implemented"
537
- else
538
- if options[:url]
539
- Watir::IE.attach(:url, options[:url])
540
- elsif options[:title]
541
- Watir::IE.attach(:title, options[:title])
542
- else
543
- raise 'Please specify title or url of new pop up window'
544
- end
545
- end
546
- end
547
-
548
- # ---
549
- # For deubgging
550
- # ---
551
- def dump_response(stream = nil)
552
- stream.nil? ? puts(page_source) : stream.puts(page_source)
553
- end
554
-
555
- # A Better Popup Handler using the latest Watir version. Posted by Mark_cain@rl.gov
556
- #
557
- # http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F
558
- #
559
- def start_clicker( button, waitTime= 9, user_input=nil)
560
- # get a handle if one exists
561
- hwnd = @browser.enabled_popup(waitTime)
562
- if (hwnd) # yes there is a popup
563
- w = WinClicker.new
564
- if ( user_input )
565
- w.setTextValueForFileNameField( hwnd, "#{user_input}" )
566
- end
567
- # I put this in to see the text being input it is not necessary to work
568
- sleep 3
569
- # "OK" or whatever the name on the button is
570
- w.clickWindowsButton_hwnd( hwnd, "#{button}" )
571
- #
572
- # this is just cleanup
573
- w = nil
574
- end
575
- end
576
-
577
- # return underlying browser
578
- def ie
579
- @browser
580
- end
581
-
582
- # Save current web page source to file
583
- # usage:
584
- # save_page("/tmp/01.html")
585
- # save_page() => # will save to "20090830112200.html"
586
- def save_page(file_name = nil)
587
- file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
588
- puts "about to save page: #{File.expand_path(file_name)}" if $DEBUG
589
- File.open(file_name, "w").puts page_source
590
- end
591
-
592
-
593
- # Verify the next page following an operation.
594
- #
595
- # Typical usage:
596
- # browser.expect_page HomePage
597
- def expect_page(page_clazz, argument = nil)
598
- if argument
599
- page_clazz.new(self, argument)
600
- else
601
- page_clazz.new(self)
602
- end
603
- end
604
-
605
- # is it running in MS Windows platforms?
606
- def self.is_windows?
607
- RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw")
608
- end
609
-
610
- end
611
- end
1
+ #***********************************************************
2
+ #* Copyright (c) 2006, Zhimin Zhan.
3
+ #* Distributed open-source, see full license in MIT-LICENSE
4
+ #***********************************************************
5
+ begin
6
+ require 'watir'
7
+ # require 'watir/contrib/enabled_popup'
8
+ # require 'watir/close_all'
9
+ # require 'watir/screen_capture'
10
+ # NO need any more
11
+ require 'watir-classic/ie'
12
+ # require 'watir/contrib/visible'
13
+ $watir_loaded = true
14
+ rescue LoadError => e
15
+ puts "!!!Error on loading watir: #{e}"
16
+ $watir_loaded = false
17
+ end
18
+
19
+ if RUBY_PLATFORM =~ /mingw/
20
+ raise "You have must at least Watir installed" unless $watir_loaded
21
+ end
22
+
23
+ module RWebSpec
24
+
25
+ ##
26
+ # Wrapping WATIR IE and FireWatir Firefox
27
+ #
28
+ class WebBrowser
29
+
30
+ attr_accessor :context
31
+
32
+ def initialize(base_url = nil, existing_browser = nil, options = {})
33
+ default_options = {:speed => "zippy",
34
+ :visible => true,
35
+ :highlight_colour => 'yellow',
36
+ :close_others => true
37
+ }
38
+ options = default_options.merge options
39
+ @context = Context.new base_url if base_url
40
+
41
+ initialize_ie_browser(existing_browser, options)
42
+ end
43
+
44
+
45
+ def initialize_ie_browser(existing_browser, options)
46
+ @browser = existing_browser || Watir::IE.new
47
+ if ($TESTWISE_EMULATE_TYPING && $TESTWISE_TYPING_SPEED) then
48
+ @browser.set_slow_speed if $TESTWISE_TYPING_SPEED == "slow"
49
+ @browser.set_fast_speed if $TESTWISE_TYPING_SPEED == 'fast'
50
+ else
51
+ @browser.speed = :zippy
52
+ end
53
+
54
+ return if existing_browser
55
+
56
+ @browser.activeObjectHighLightColor = options[:highlight_colour]
57
+ @browser.visible = options[:visible] unless $HIDE_IE
58
+ #NOTE: close_others fails
59
+ if RUBY_VERSION =~ /^1\.8/ && options[:close_others] then
60
+ begin
61
+ @browser.close_others
62
+ rescue => e1
63
+ puts "Failed to close others"
64
+ end
65
+ else
66
+ puts "close other browser instances not working yet in Ruby 1.9.1 version of Watir"
67
+ end
68
+ end
69
+
70
+ def self.reuse(base_url, options)
71
+ if self.is_windows? && ($TESTWISE_BROWSER != "Firefox" && $TESTWISE_BROWSER != "Firefox")
72
+ require 'watir-classic/ie'
73
+ # try to avoid
74
+ # lib/ruby/1.8/dl/win32.rb:11:in `sym': unknown type specifier 'v'
75
+ Watir::IE.each do |browser_window|
76
+ return WebBrowser.new(base_url, browser_window, options)
77
+ end
78
+ #puts "no browser instance found"
79
+ WebBrowser.new(base_url, nil, options)
80
+ else
81
+ WebBrowser.new(base_url, nil, options)
82
+ end
83
+ end
84
+
85
+ # for popup windows
86
+ def self.new_from_existing(underlying_browser, web_context = nil)
87
+ return WebBrowser.new(web_context ? web_context.base_url : nil, underlying_browser, {:close_others => false})
88
+ end
89
+
90
+
91
+ ##
92
+ # Delegate to Watir
93
+ #
94
+ [:button, :td, :checkbox, :div, :form, :frame, :h1, :h2, :h3, :h4, :h5, :h6, :hidden, :image, :li, :link, :map, :pre, :tr, :radio, :select_list, :span, :table, :text_field, :paragraph, :file_field, :label].each do |method|
95
+ define_method method do |*args|
96
+ @browser.send(method, *args)
97
+ end
98
+ end
99
+ alias cell td
100
+ alias check_box checkbox # seems watir doc is wrong, checkbox not check_box
101
+ alias row tr
102
+ alias a link
103
+ alias img image
104
+
105
+ def area(*args)
106
+ @browser.send("area", *args)
107
+ end
108
+
109
+ def modal_dialog(how=nil, what=nil)
110
+ @browser.modal_dialog(how, what)
111
+ end
112
+
113
+ # This is the main method for accessing a generic element with a given attibute
114
+ # * how - symbol - how we access the element. Supports all values except :index and :xpath
115
+ # * what - string, integer or regular expression - what we are looking for,
116
+ #
117
+ # Valid values for 'how' are listed in the Watir Wiki - http://wiki.openqa.org/display/WTR/Methods+supported+by+Element
118
+ #
119
+ # returns an Watir::Element object
120
+ #
121
+ # Typical Usage
122
+ #
123
+ # element(:class, /foo/) # access the first element with class 'foo'. We can use a string in place of the regular expression
124
+ # element(:id, "11") # access the first element that matches an id
125
+ def element(how, what)
126
+ return @browser.element(how, what)
127
+ end
128
+
129
+ # this is the main method for accessing generic html elements by an attribute
130
+ #
131
+ # Returns a HTMLElements object
132
+ #
133
+ # Typical usage:
134
+ #
135
+ # elements(:class, 'test').each { |l| puts l.to_s } # iterate through all elements of a given attribute
136
+ # elements(:alt, 'foo')[1].to_s # get the first element of a given attribute
137
+ # elements(:id, 'foo').length # show how many elements are foung in the collection
138
+ #
139
+ def elements(how, what)
140
+ return @browser.elements(how, what)
141
+ end
142
+
143
+ def show_all_objects
144
+ @browser.show_all_objects
145
+ end
146
+
147
+ # Returns the specified ole object for input elements on a web page.
148
+ #
149
+ # This method is used internally by Watir and should not be used externally. It cannot be marked as private because of the way mixins and inheritance work in watir
150
+ #
151
+ # * how - symbol - the way we look for the object. Supported values are
152
+ # - :name
153
+ # - :id
154
+ # - :index
155
+ # - :value etc
156
+ # * what - string that we are looking for, ex. the name, or id tag attribute or index of the object we are looking for.
157
+ # * types - what object types we will look at.
158
+ # * value - used for objects that have one name, but many values. ex. radio lists and checkboxes
159
+ def locate_input_element(how, what, types, value=nil)
160
+ @browser.locate_input_element(how, what, types, value)
161
+ end
162
+
163
+ # This is the main method for accessing map tags - http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/map.asp?frame=true
164
+ # * how - symbol - how we access the map,
165
+ # * what - string, integer or regular expression - what we are looking for,
166
+ #
167
+ # Valid values for 'how' are listed in the Watir Wiki - http://wiki.openqa.org/display/WTR/Methods+supported+by+Element
168
+ #
169
+ # returns a map object
170
+ #
171
+ # Typical Usage
172
+ #
173
+ # map(:id, /list/) # access the first map that matches list.
174
+ # map(:index,2) # access the second map on the page
175
+ # map(:title, "A Picture") # access a map using the tooltip text. See http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/title_1.asp?frame=true
176
+ #
177
+ def map(how, what=nil)
178
+ @browser.map(how, what)
179
+ end
180
+
181
+ def contains_text(text)
182
+ @browser.contains_text(text);
183
+ end
184
+
185
+ # return HTML of current web page
186
+ def page_source
187
+ @browser.html()
188
+ #@browser.document.body
189
+ end
190
+ alias html_body page_source
191
+ alias html page_source
192
+
193
+
194
+ # return plain text of current web page
195
+ def text
196
+ @browser.text
197
+ end
198
+
199
+ def page_title
200
+ case @browser.class.to_s
201
+ when "Watir::IE"
202
+ @browser.document.title
203
+ else
204
+ @browser.title
205
+ end
206
+ end
207
+
208
+ [:images, :links, :buttons, :select_lists, :checkboxes, :radios, :text_fields, :divs, :dls, :dds, :dts, :ems, :lis, :maps, :spans, :strongs, :ps, :pres, :labels, :tds, :trs].each do |method|
209
+ define_method method do
210
+ @browser.send(method)
211
+ end
212
+ end
213
+ alias as links
214
+ alias rows trs
215
+ alias cells tds
216
+ alias imgs images
217
+
218
+ # current url
219
+ def url
220
+ @browser.url
221
+ end
222
+
223
+ def base_url=(new_base_url)
224
+ if @context
225
+ @conext.base_url = new_base_url
226
+ return
227
+ end
228
+ @context = Context.new base_url
229
+ end
230
+
231
+ def is_firefox?
232
+ return false
233
+ end
234
+
235
+ # Close the browser window. Useful for automated test suites to reduce
236
+ # test interaction.
237
+ def close_browser
238
+ @browser.close
239
+ sleep 2
240
+ end
241
+ alias close close_browser
242
+
243
+ def self.close_all_browsers(browser_type = :ie)
244
+ if browser_type == :ie
245
+ Watir::IE.close_all
246
+ end
247
+ end
248
+
249
+ def full_url(relative_url)
250
+ if @context && @context.base_url
251
+ @context.base_url + relative_url
252
+ else
253
+ relative_url
254
+ end
255
+ end
256
+
257
+ def begin_at(relative_url)
258
+ @browser.goto full_url(relative_url)
259
+ end
260
+
261
+ def browser_opened?
262
+ begin
263
+ @browser != nil
264
+ rescue => e
265
+ return false
266
+ end
267
+ end
268
+
269
+ # Some browsers (i.e. IE) need to be waited on before more actions can be
270
+ # performed. Most action methods in Watir::Simple already call this before
271
+ # and after.
272
+ def wait_for_browser
273
+ # Watir 3 does not support it any more
274
+ # @browser.waitForIE unless is_firefox?
275
+ end
276
+
277
+
278
+ # A convenience method to wait at both ends of an operation for the browser
279
+ # to catch up.
280
+ def wait_before_and_after
281
+ wait_for_browser
282
+ yield
283
+ wait_for_browser
284
+ end
285
+
286
+
287
+ [:back, :forward, :refresh, :focus, :close_others].each do |method|
288
+ define_method(method) do
289
+ @browser.send(method)
290
+ end
291
+ end
292
+ alias refresh_page refresh
293
+ alias go_back back
294
+ alias go_forward forward
295
+
296
+ # Go to a page
297
+ # Usage:
298
+ # open_browser("http://www.itest2.com"
299
+ # ....
300
+ # goto_page("/purchase") # full url => http://www.itest.com/purchase
301
+ def goto_page(page)
302
+ # puts "DEBUG calling goto page => #{page}"
303
+ @browser.goto full_url(page);
304
+ end
305
+
306
+ # Go to a URL directly
307
+ # goto_url("http://www.itest2.com/downloads")
308
+ def goto_url(url)
309
+ @browser.goto url
310
+ end
311
+
312
+ # text fields
313
+ def enter_text_into_field_with_name(name, text)
314
+ if is_firefox?
315
+ wait_before_and_after { text_field(:name, name).value = text }
316
+ sleep 0.3
317
+ else
318
+ wait_before_and_after { text_field(:name, name).set(text) }
319
+ end
320
+ end
321
+
322
+ alias set_form_element enter_text_into_field_with_name
323
+ alias enter_text enter_text_into_field_with_name
324
+ alias set_hidden_field set_form_element
325
+
326
+ #links
327
+ def click_link_with_id(link_id, opts = {})
328
+ if opts && opts[:index]
329
+ wait_before_and_after { link(:id => link_id, :index => opts[:index]).click }
330
+ else
331
+ wait_before_and_after { link(:id, link_id).click }
332
+ end
333
+ end
334
+
335
+ def click_link_with_text(text, opts = {})
336
+ if opts && opts[:index]
337
+ wait_before_and_after { link(:text => text, :index => opts[:index]).click }
338
+ else
339
+ wait_before_and_after { link(:text, text).click }
340
+ end
341
+ end
342
+ alias click_link click_link_with_text
343
+
344
+
345
+ # Click a button with give HTML id
346
+ # Usage:
347
+ # click_button_with_id("btn_sumbit")
348
+ def click_button_with_id(id, opts = {})
349
+ if opts && opts[:index]
350
+ wait_before_and_after { button(:id => id, :index => opts[:index]).click }
351
+ else
352
+ wait_before_and_after { button(:id, id).click }
353
+ end
354
+ end
355
+
356
+ # Click a button with give name
357
+ # Usage:
358
+ # click_button_with_name("confirm")
359
+ def click_button_with_name(name, opts={})
360
+ if opts && opts[:index]
361
+ wait_before_and_after { button(:name => name, :index => opts[:index]).click }
362
+ else
363
+ wait_before_and_after { button(:name, name).click }
364
+ end
365
+ end
366
+
367
+ # Click a button with caption
368
+ # Usage:
369
+ # click_button_with_caption("Confirm payment")
370
+ def click_button_with_caption(caption, opts={})
371
+ if opts && opts[:index]
372
+ wait_before_and_after { button(:caption => caption, :index => opts[:index]).click }
373
+ else
374
+ wait_before_and_after { button(:caption, caption).click }
375
+ end
376
+ end
377
+ alias click_button click_button_with_caption
378
+ alias click_button_with_text click_button_with_caption
379
+
380
+ # Click a button with value
381
+ # Usage:
382
+ # click_button_with_value("Confirm payment")
383
+ def click_button_with_value(value, opts={})
384
+ if opts && opts[:index]
385
+ wait_before_and_after { button(:value => value, :index => opts[:index]).click }
386
+ else
387
+ wait_before_and_after { button(:value, value).click }
388
+ end
389
+ end
390
+
391
+ # Select a dropdown list by name
392
+ # Usage:
393
+ # select_option("country", "Australia")
394
+ def select_option(selectName, option)
395
+ select_list(:name, selectName).select(option)
396
+ end
397
+
398
+ # submit first submit button
399
+ def submit(buttonName = nil)
400
+ if (buttonName.nil?) then
401
+ buttons.each { |button|
402
+ next if button.type != 'submit'
403
+ button.click
404
+ return
405
+ }
406
+ else
407
+ click_button_with_name(buttonName)
408
+ end
409
+ end
410
+
411
+ # Check a checkbox
412
+ # Usage:
413
+ # check_checkbox("agree")
414
+ # check_checkbox("agree", "true")
415
+ def check_checkbox(checkBoxName, values=nil)
416
+ if values
417
+ values.class == Array ? arys = values : arys = [values]
418
+ arys.each {|cbx_value|
419
+ if Watir::VERSION =~ /^1/ then
420
+ checkbox(:name, checkBoxName, cbx_value).set
421
+ else
422
+ checkbox(:name => checkBoxName, :value => cbx_value).set
423
+ end
424
+ }
425
+ else
426
+ checkbox(:name, checkBoxName).set
427
+ end
428
+ end
429
+
430
+ # Check a checkbox
431
+ # Usage:
432
+ # uncheck_checkbox("agree")
433
+ # uncheck_checkbox("agree", "false")
434
+ def uncheck_checkbox(checkBoxName, values = nil)
435
+ if values
436
+ values.class == Array ? arys = values : arys = [values]
437
+ arys.each {|cbx_value|
438
+ if Watir::VERSION =~ /^1/ then
439
+ checkbox(:name, checkBoxName, cbx_value).clear
440
+ else
441
+ checkbox(:name => checkBoxName, :value => cbx_value).clear
442
+ end
443
+ }
444
+ else
445
+ checkbox(:name, checkBoxName).clear
446
+ end
447
+ end
448
+
449
+
450
+ # Click a radio button
451
+ # Usage:
452
+ # click_radio_option("country", "Australia")
453
+ def click_radio_option(radio_group, radio_option)
454
+ if Watir::VERSION =~ /^1/ then
455
+ radio(:name, radio_group, radio_option).set
456
+ else
457
+ radio(:name => radio_group, :value => radio_option).set
458
+ end
459
+ end
460
+ alias click_radio_button click_radio_option
461
+
462
+ # Clear a radio button
463
+ # Usage:
464
+ # click_radio_option("country", "Australia")
465
+ def clear_radio_option(radio_group, radio_option)
466
+ if Watir::VERSION =~ /^2/ then
467
+ radio(:name => radio_group, :value => radio_option).clear
468
+ else
469
+ radio(:name, radio_group, radio_option).clear
470
+ end
471
+ end
472
+ alias clear_radio_button clear_radio_option
473
+
474
+ # Deprecated: using Watir style directly instead
475
+ def element_by_id(elem_id)
476
+ if is_firefox?
477
+ # elem = @browser.document.getElementById(elem_id)
478
+ # elem = div(:id, elem_id) || label(:id, elem_id) || button(:id, elem_id) ||
479
+ # span(:id, elem_id) || hidden(:id, elem_id) || link(:id, elem_id) || radio(:id, elem_id)
480
+ elem = browser.element_by_xpath("//*[@id='#{elem_id}']")
481
+ else
482
+ elem = @browser.document.getElementById(elem_id)
483
+ end
484
+ end
485
+
486
+ def element_value(elementId)
487
+ if is_firefox? then
488
+ elem = element_by_id(elementId)
489
+ elem ? elem.invoke('innerText') : nil
490
+ else
491
+ elem = element_by_id(elementId)
492
+ elem ? elem.invoke('innerText') : nil
493
+ end
494
+ end
495
+
496
+ def element_source(elementId)
497
+ elem = element_by_id(elementId)
498
+ assert_not_nil(elem, "HTML element: #{elementId} not exists")
499
+ elem.innerHTML
500
+ end
501
+
502
+ def select_file_for_upload(file_field, file_path)
503
+ normalized_file_path = RUBY_PLATFORM.downcase.include?("mingw") ? file_path.gsub("/", "\\") : file_path
504
+ file_field(:name, file_field).set(normalized_file_path)
505
+ end
506
+
507
+ # Watir 1.9
508
+ def javascript_dialog
509
+ @browser.javascript_dialog
510
+ end
511
+
512
+ def start_window(url = nil)
513
+ @browser.start_window(url);
514
+ end
515
+
516
+ # Attach to existing browser
517
+ #
518
+ # Usage:
519
+ # WebBrowser.attach_browser(:title, "iTest2")
520
+ # WebBrowser.attach_browser(:url, "http://www.itest2.com")
521
+ # WebBrowser.attach_browser(:url, "http://www.itest2.com", {:browser => "Firefox", :base_url => "http://www.itest2.com"})
522
+ # WebBrowser.attach_browser(:title, /agileway\.com\.au\/attachment/) # regular expression
523
+ def self.attach_browser(how, what, options={})
524
+ default_options = {:browser => "IE"}
525
+ options = default_options.merge(options)
526
+ site_context = Context.new(options[:base_url]) if options[:base_url]
527
+ return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context)
528
+ end
529
+
530
+ # Attach a Watir::IE instance to a popup window.
531
+ #
532
+ # Typical usage
533
+ # new_popup_window(:url => "http://www.google.com/a.pdf")
534
+ def new_popup_window(options, browser = "ie")
535
+ if is_firefox?
536
+ raise "not implemented"
537
+ else
538
+ if options[:url]
539
+ Watir::IE.attach(:url, options[:url])
540
+ elsif options[:title]
541
+ Watir::IE.attach(:title, options[:title])
542
+ else
543
+ raise 'Please specify title or url of new pop up window'
544
+ end
545
+ end
546
+ end
547
+
548
+ # ---
549
+ # For deubgging
550
+ # ---
551
+ def dump_response(stream = nil)
552
+ stream.nil? ? puts(page_source) : stream.puts(page_source)
553
+ end
554
+
555
+ # A Better Popup Handler using the latest Watir version. Posted by Mark_cain@rl.gov
556
+ #
557
+ # http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F
558
+ #
559
+ def start_clicker( button, waitTime= 9, user_input=nil)
560
+ # get a handle if one exists
561
+ hwnd = @browser.enabled_popup(waitTime)
562
+ if (hwnd) # yes there is a popup
563
+ w = WinClicker.new
564
+ if ( user_input )
565
+ w.setTextValueForFileNameField( hwnd, "#{user_input}" )
566
+ end
567
+ # I put this in to see the text being input it is not necessary to work
568
+ sleep 3
569
+ # "OK" or whatever the name on the button is
570
+ w.clickWindowsButton_hwnd( hwnd, "#{button}" )
571
+ #
572
+ # this is just cleanup
573
+ w = nil
574
+ end
575
+ end
576
+
577
+ # return underlying browser
578
+ def ie
579
+ @browser
580
+ end
581
+
582
+ # Save current web page source to file
583
+ # usage:
584
+ # save_page("/tmp/01.html")
585
+ # save_page() => # will save to "20090830112200.html"
586
+ def save_page(file_name = nil)
587
+ file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
588
+ puts "about to save page: #{File.expand_path(file_name)}" if $DEBUG
589
+ File.open(file_name, "w").puts page_source
590
+ end
591
+
592
+
593
+ # Verify the next page following an operation.
594
+ #
595
+ # Typical usage:
596
+ # browser.expect_page HomePage
597
+ def expect_page(page_clazz, argument = nil)
598
+ if argument
599
+ page_clazz.new(self, argument)
600
+ else
601
+ page_clazz.new(self)
602
+ end
603
+ end
604
+
605
+ # is it running in MS Windows platforms?
606
+ def self.is_windows?
607
+ RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw")
608
+ end
609
+
610
+ end
611
+ end