rwebunit 0.7.2 → 0.8.9

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,5 +1,17 @@
1
1
  require 'uri'
2
2
 
3
+ # ZZ patches to RSpec 1.1.2
4
+ # - add to_s method to example_group
5
+ module Spec
6
+ module Example
7
+ class ExampleGroup
8
+ def to_s
9
+ @_defined_description
10
+ end
11
+ end
12
+ end
13
+ end
14
+
3
15
  # example
4
16
  # should link_by_text(text, options).size > 0
5
17
 
@@ -13,41 +25,37 @@ module RWebUnit
13
25
  @web_tester
14
26
  end
15
27
 
16
- def open_browser_preconfigured(options = {})
17
- open_browser(ENV['ITEST_PROJECT_BASE_URL'], options)
18
- end
19
-
20
28
  # open a browser, and set base_url via hash, but does not acually
21
29
  #
22
30
  # example:
23
31
  # open_browser :base_url => http://localhost:8080
24
- def open_browser(base_url = nil, options = {})
25
- raise "base_url must be set" if base_url .nil?
32
+ def open_browser(base_url = nil, options = {})
33
+ base_url ||= ENV['ITEST_PROJECT_BASE_URL']
34
+ raise "base_url must be set" if base_url.nil?
26
35
 
27
- default_options = {:speed => "fast",
36
+ default_options = {:speed => "fast",
28
37
  :visible => true,
29
38
  :highlight_colour => 'yellow',
30
39
  :close_others => true,
31
40
  :start_new => false, # start a new browser always
32
41
  :go => true}
33
-
34
- options = default_options.merge options
35
- options[:firefox] = true if "Firefox" == ENV['ITEST_BROWSER']
42
+
43
+ options = default_options.merge options
44
+ options[:firefox] = true if "Firefox" == ENV['ITEST_BROWSER']
45
+ (ENV['ITEST_HIDE_BROWSER'] == "true") ? $HIDE_IE = true : $HIDE_IE = false
36
46
 
37
47
  uri = URI.parse(base_url)
38
48
  uri_base = "#{uri.scheme}://#{uri.host}:#{uri.port}"
39
- if options[:start_new]
49
+ if options[:start_new] || @web_tester.nil?
40
50
  @web_tester = WebTester.new(uri_base, options)
41
- else
42
- @web_tester ||= WebTester.new(uri_base, options)
43
51
  end
44
-
45
- if options[:go]
46
- (uri.path.length == 0) ? begin_at("/") : begin_at(uri.path)
52
+
53
+ if options[:go]
54
+ (uri.path.length == 0) ? begin_at("/") : begin_at(uri.path)
47
55
  end
48
56
  return @web_tester
49
57
  end
50
- alias open_browser_with open_browser
58
+ alias open_browser_with open_browser
51
59
 
52
60
  # --
53
61
  # Content
@@ -97,12 +105,24 @@ module RWebUnit
97
105
  # default options: exact => true
98
106
  def links_by_text(link_text, options = {})
99
107
  options.merge!({:exact=> true})
100
- links.select { |link|
101
- options[:exact] ? link.text == link_text : link.text.include?(link_text)
108
+ matching_links = []
109
+ links.each { |link|
110
+ matching_links << link if (options[:exact] ? link.text == link_text : link.text.include?(link_text))
102
111
  }
112
+ return matching_links
103
113
  end
104
114
  alias links_with_text links_by_text
105
115
 
116
+ def save_page(file_name = nil)
117
+ @web_tester.save_page(file_name)
118
+ end
119
+
120
+ def save_content_to_file(content, file_name = nil)
121
+ file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
122
+ puts "about to save page: #{File.expand_path(file_name)}"
123
+ File.open(file_name, "w").puts content
124
+ end
125
+
106
126
  end
107
127
 
108
128
  end
@@ -31,6 +31,11 @@ module RWebUnit
31
31
  def initialize(web_tester, page_text=nil)
32
32
  @web_tester = web_tester
33
33
  @page_text = page_text
34
+ begin
35
+ delay = ENV['ITEST_PAGE_DELAY'].to_i
36
+ sleep(delay)
37
+ rescue => e
38
+ end
34
39
  assert_on_page
35
40
  end
36
41
 
@@ -21,6 +21,19 @@ end
21
21
 
22
22
  raise "You have must at least Watir or Firewatir installed" unless $watir_loaded || $firewatir_loaded
23
23
 
24
+ # Patch: fix typo in Watir 1.5.3
25
+ module Watir
26
+ class IE
27
+ def self.close_all_but(except=nil)
28
+ Watir::IE.each do |ie|
29
+ ie.close_modal
30
+ ie.close unless except and except.hwnd == ie.hwnd
31
+ end
32
+ sleep 1.0 # replace with polling for window count to be zero?
33
+ end
34
+ end
35
+ end
36
+
24
37
  module RWebUnit
25
38
 
26
39
  ##
@@ -31,7 +44,7 @@ module RWebUnit
31
44
  attr_accessor :test_context
32
45
 
33
46
  def initialize(base_url = nil, options = {})
34
- default_options = {:speed => "fast",
47
+ default_options = {:speed => "zippy",
35
48
  :visible => true,
36
49
  :highlight_colour => 'yellow',
37
50
  :close_others => true}
@@ -40,54 +53,121 @@ module RWebUnit
40
53
 
41
54
  if (options[:firefox] && $firewatir_loaded) || ($firewatir_loaded and !$watir_loaded)
42
55
  @@browser = FireWatir::Firefox.start(base_url)
43
- elsif $watir_loaded
56
+ elsif $watir_loaded
44
57
  @@browser = Watir::IE.new
45
- if options[:speed] == 'slow' then @@browser.set_slow_speed else @@browser.set_fast_speed end
58
+
59
+ if ENV['ITEST_EMULATE_TYPING'] == "true" && ENV['ITEST_TYPING_SPEED'] then
60
+ @@browser.set_slow_speed if ENV['ITEST_TYPING_SPEED'] == 'slow'
61
+ @@browser.set_fast_speed if ENV['ITEST_TYPING_SPEED'] == 'fast'
62
+ else
63
+ @@browser.speed = :zippy
64
+ end
46
65
  @@browser.activeObjectHighLightColor = options[:highlight_colour]
47
- @@browser.visible = options[:visible]
66
+ @@browser.visible = options[:visible] unless $HIDE_IE
48
67
  @@browser.close_others if options[:close_others]
49
- else
68
+ else
50
69
  raise "rWebUnit initialiazation error, most likely Watir or Firewatir not present"
51
70
  end
71
+
52
72
  end
53
73
 
54
74
  ##
55
75
  # Delegate to Watir
56
76
  #
57
- def area(*args); @@browser.area(*args); end
58
- def button(*args); @@browser.button(*args); end
59
- def cell(*args); @@browser.cell(*args); end
77
+ def area(*args)
78
+ @@browser.area(*args)
79
+ end
80
+ def button(*args)
81
+ @@browser.button(*args);
82
+ end
83
+ def cell(*args)
84
+ @@browser.cell(*args);
85
+ end
60
86
  alias td cell
61
- def checkbox(*args); @@browser.checkbox(*args); end
62
- alias check_box checkbox # seems watir doc is wrong, checkbox not check_box
63
- def div(*args); @@browser.div(*args); end
64
- def form(*args); @@browser.form(*args); end
65
- def frame(*args); @@browser.frame(*args); end
66
- def h1(*args); @@browser.h1(*args); end
67
- def h2(*args); @@browser.h2(*args); end
68
- def h3(*args); @@browser.h3(*args); end
69
- def h4(*args); @@browser.h4(*args); end
70
- def h5(*args); @@browser.h5(*args); end
71
- def h6(*args); @@browser.h6(*args); end
72
- def hidden(*args); @@browser.hidden(*args); end
73
- def image(*args); @@browser.image(*args); end
74
- def li(*args); @@browser.li(*args); end
75
- def link(*args); @@browser.link(*args); end
76
- def map(*args); @@browser.map(*args); end
77
- def pre(*args); @@browser.pre(*args); end
78
- def radio(*args); @@browser.radio(*args); end
79
- def row(*args); @@browser.row(*args); end
87
+ def checkbox(*args)
88
+ @@browser.checkbox(*args);
89
+ end
90
+ alias check_box checkbox # seems watir doc is wrong, checkbox not check_box
91
+ def div(*args)
92
+ @@browser.div(*args);
93
+ end
94
+ def form(*args)
95
+ @@browser.form(*args);
96
+ end
97
+ def frame(*args)
98
+ @@browser.frame(*args);
99
+ end
100
+ def h1(*args)
101
+ @@browser.h1(*args);
102
+ end
103
+ def h2(*args)
104
+ @@browser.h2(*args);
105
+ end
106
+ def h3(*args)
107
+ @@browser.h3(*args);
108
+ end
109
+ def h4(*args)
110
+ @@browser.h4(*args);
111
+ end
112
+ def h5(*args)
113
+ @@browser.h5(*args);
114
+ end
115
+ def h6(*args)
116
+ @@browser.h6(*args);
117
+ end
118
+ def hidden(*args)
119
+ @@browser.hidden(*args);
120
+ end
121
+ def image(*args)
122
+ @@browser.image(*args);
123
+ end
124
+ def li(*args)
125
+ @@browser.li(*args);
126
+ end
127
+ def link(*args)
128
+ @@browser.link(*args);
129
+ end
130
+ def map(*args)
131
+ @@browser.map(*args);
132
+ end
133
+ def pre(*args)
134
+ @@browser.pre(*args);
135
+ end
136
+ def radio(*args)
137
+ @@browser.radio(*args);
138
+ end
139
+ def row(*args)
140
+ @@browser.row(*args);
141
+ end
80
142
  alias tr row
81
- def select_list(*args); @@browser.select_list(*args); end
82
- def span(*args); @@browser.span(*args); end
83
- def table(*args); @@browser.table(*args); end
84
- def text_field(*args); @@browser.text_field(*args); end
143
+ def select_list(*args)
144
+ @@browser.select_list(*args);
145
+ end
85
146
 
86
- def paragraph(*args); @@browser.paragraph(*args); end
87
- def file_field(*args); @@browser.file_field(*args); end
88
- def label(*args); @@browser.span(*args); end
147
+ def span(*args)
148
+ @@browser.span(*args);
149
+ end
89
150
 
90
- def contains_text(text); @@browser.contains_text(text); end
151
+ def table(*args)
152
+ @@browser.table(*args);
153
+ end
154
+ def text_field(*args)
155
+ @@browser.text_field(*args);
156
+ end
157
+
158
+ def paragraph(*args)
159
+ @@browser.paragraph(*args);
160
+ end
161
+ def file_field(*args)
162
+ @@browser.file_field(*args);
163
+ end
164
+ def label(*args)
165
+ @@browser.label(*args);
166
+ end
167
+
168
+ def contains_text(text)
169
+ @@browser.contains_text(text);
170
+ end
91
171
 
92
172
  def page_source
93
173
  @@browser.html()
@@ -103,15 +183,33 @@ module RWebUnit
103
183
  end
104
184
  end
105
185
 
106
- def links; @@browser.links; end
107
- def buttons; @@browser.buttons; end
108
- def select_lists; @@browser.select_lists; end
109
- def checkboxes; @@browser.checkboxes; end
110
- def radios; @@browser.radios; end
111
- def text_fields; @@browser.text_fields; end
186
+ def images
187
+ @@browser.images
188
+ end
189
+
190
+ def links
191
+ @@browser.links;
192
+ end
193
+ def buttons
194
+ @@browser.buttons;
195
+ end
196
+ def select_lists
197
+ @@browser.select_lists;
198
+ end
199
+ def checkboxes
200
+ @@browser.checkboxes;
201
+ end
202
+ def radios
203
+ @@browser.radios;
204
+ end
205
+ def text_fields
206
+ @@browser.text_fields;
207
+ end
112
208
 
113
209
  # current url
114
- def url; @@browser.url; end
210
+ def url
211
+ @@browser.url
212
+ end
115
213
 
116
214
  def base_url=(new_base_url)
117
215
  if @test_context
@@ -186,26 +284,42 @@ module RWebUnit
186
284
  end
187
285
 
188
286
 
189
- def go_back; @@browser.back; end
190
- def go_forward; @@browser.forward; end
191
- def goto_page(page); @@browser.goto full_url(page); end
192
- def refresh; @@browser.refresh; end
287
+ def go_back;
288
+ @@browser.back;
289
+ end
290
+ def go_forward;
291
+ @@browser.forward;
292
+ end
293
+ def goto_page(page)
294
+ @@browser.goto full_url(page);
295
+ end
296
+ def refresh;
297
+ @@browser.refresh;
298
+ end
193
299
 
194
- def focus; @@browser.focus; end
195
- def close_others; @@browser.close_others; end
300
+ def focus;
301
+ @@browser.focus;
302
+ end
303
+ def close_others;
304
+ @@browser.close_others;
305
+ end
196
306
 
197
307
  # text fields
198
308
  def enter_text_into_field_with_name(name, text)
199
- wait_before_and_after { text_field(:name, name).set(text) }
309
+ if is_firefox?
310
+ wait_before_and_after { text_field(:name, name).value = text }
311
+ sleep 0.5
312
+ else
313
+ wait_before_and_after { text_field(:name, name).set(text) }
314
+ end
200
315
  end
201
316
  alias set_form_element enter_text_into_field_with_name
202
317
  alias enter_text enter_text_into_field_with_name
203
318
 
204
319
  #links
205
- def click_link(link_id)
320
+ def click_link_with_id(link_id)
206
321
  wait_before_and_after { link(:id, link_id).click }
207
322
  end
208
- alias click_link_with_id click_link
209
323
 
210
324
  def click_link_with_text(text)
211
325
  wait_before_and_after { link(:text, text).click }
@@ -217,7 +331,6 @@ module RWebUnit
217
331
  def click_button_with_id(id)
218
332
  wait_before_and_after { button(:id, id).click }
219
333
  end
220
- alias click_button click_button_with_id
221
334
 
222
335
  def click_button_with_name(name)
223
336
  wait_before_and_after { button(:name, name).click }
@@ -283,7 +396,11 @@ module RWebUnit
283
396
  end
284
397
 
285
398
  def element_by_id(elem_id)
286
- elem = @@browser.document.getElementById(elem_id)
399
+ if is_firefox?
400
+ elem = label(:id, elem_id) || button(:id, elem_id) || span(:id, elem_id) || hidden(:id, elem_id) || link(:id, elem_id) || radio(:id, elem_id)
401
+ else
402
+ elem = @@browser.document.getElementById(elem_id)
403
+ end
287
404
  end
288
405
 
289
406
  def element_value(elementId)
@@ -306,12 +423,15 @@ module RWebUnit
306
423
  file_field(:name, file_field).set(file_path)
307
424
  end
308
425
 
309
- def start_window(url = nil); @@browser.start_window(url); end
426
+ def start_window(url = nil)
427
+ @@browser.start_window(url);
428
+ end
429
+
310
430
  def self.attach_browser(how, what)
311
- if is_firefox? then
312
- raise "not implemented yet"
313
- else
431
+ if @@browser && @@browser.class == Watir::IE
314
432
  Watir::IE.attach(how, what)
433
+ else
434
+ raise "not implemented yet"
315
435
  end
316
436
  end
317
437
 
@@ -323,7 +443,13 @@ module RWebUnit
323
443
  if is_firefox?
324
444
  raise "not implemented"
325
445
  else
326
- Watir::IE.attach(:url, options[:url])
446
+ if options[:url]
447
+ Watir::IE.attach(:url, options[:url])
448
+ elsif options[:title]
449
+ Watir::IE.attach(:title, options[:title])
450
+ else
451
+ raise 'Please specify title or url of new pop up window'
452
+ end
327
453
  end
328
454
  end
329
455
 
@@ -342,7 +468,7 @@ module RWebUnit
342
468
  #
343
469
  # http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F
344
470
  #
345
- def start_clicker( button , waitTime= 9, user_input=nil)
471
+ def start_clicker( button, waitTime= 9, user_input=nil)
346
472
  # get a handle if one exists
347
473
  hwnd = @@browser.enabled_popup(waitTime)
348
474
  if (hwnd) # yes there is a popup
@@ -371,6 +497,12 @@ module RWebUnit
371
497
  @@browser
372
498
  end
373
499
 
500
+ def save_page(file_name = nil)
501
+ file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
502
+ puts "about to save page: #{File.expand_path(file_name)}"
503
+ File.open(file_name, "w").puts page_source
504
+ end
505
+
374
506
  end
375
507
 
376
508
  end