rwebunit 0.9.4 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +228 -168
- data/MIT-LICENSE +1 -1
- data/README +32 -29
- data/Rakefile +5 -4
- data/lib/rspec_extensions.rb +10 -2
- data/lib/rwebunit.rb +8 -3
- data/lib/rwebunit/assert.rb +25 -24
- data/lib/rwebunit/clickJSDialog.rb +15 -0
- data/lib/rwebunit/context.rb +24 -24
- data/lib/rwebunit/driver.rb +297 -33
- data/lib/rwebunit/itest_plugin.rb +47 -0
- data/lib/rwebunit/matchers/contains_text.rb +37 -0
- data/lib/rwebunit/rspec_helper.rb +140 -128
- data/lib/rwebunit/test_utils.rb +98 -88
- data/lib/rwebunit/{web_tester.rb → web_browser.rb} +433 -510
- data/lib/rwebunit/web_page.rb +96 -99
- data/lib/rwebunit/web_testcase.rb +36 -36
- data/lib/watir_extensions.rb +65 -0
- data/test/mock_page.rb +8 -0
- data/test/setup.rb +10 -0
- data/test/test.html +129 -0
- data/test/test_assert.rb +64 -0
- data/test/test_driver.rb +23 -5
- metadata +24 -14
- data/sample/README.txt +0 -21
- data/sample/kangxi_home_webtest.rb +0 -37
- data/sample/kangxi_httpcaller_webtest.rb +0 -33
- data/sample/kangxi_pages.rb +0 -62
- data/sample/kangxi_xml_formatter_webtest.rb +0 -44
- data/sample/rwebunit_home_testcase.rb +0 -22
- data/sample/sample_rwebunit_test.rb +0 -15
- data/sample/sample_rwebunit_testcase.rb +0 -22
- data/sample/sample_watir_test.rb +0 -17
@@ -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/
|
9
|
-
require 'watir/
|
10
|
-
|
11
|
-
|
12
|
-
$watir_loaded =
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
module
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
end
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
def
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
def
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
def
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
end
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
def
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
#
|
234
|
-
def
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
end
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
end
|
340
|
-
|
341
|
-
def
|
342
|
-
|
343
|
-
end
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
if (
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
#
|
392
|
-
def
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
def
|
429
|
-
|
430
|
-
end
|
431
|
-
|
432
|
-
|
433
|
-
|
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
|