rwebunit 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,386 +8,403 @@ require 'runit/assert'
8
8
 
9
9
  module RWebUnit
10
10
 
11
- ##
12
- # Wrapping WATIR and provide assertions
13
- #
14
- class WebTester
15
- include Watir::Simple
16
- include RUNIT::Assert
17
-
18
- attr_accessor :test_context
19
-
20
- def initialize
21
- @test_context = TestContext.new
22
- end
23
-
24
- def beginAt(relativeURL)
25
- url = @test_context.base_url + relativeURL
26
- new_browser_at(url) # create @@browser
27
- end
28
-
29
- def closeBrowser
30
- begin
31
- close_browser
32
- rescue
33
- end
34
- end
35
-
36
-
37
- def goBack
38
- go_back()
39
- end
40
-
41
- def goForward
42
- go_forward()
43
- end
44
-
45
- def gotoPage(page)
46
- navigate_to(page)
47
- end
48
-
49
- # assertions
50
- def assertTitleEquals(title)
51
- assert_equals(title, html_title())
52
- end
53
-
54
- # text fields
55
- def setFormElement(elementName, elementValue)
56
- enter_text_into_field_with_name(elementName, elementValue)
57
- end
58
- alias enterText setFormElement
59
-
60
- def assertTextPresentInTextField(textfieldName, text, msg = nil)
61
- assert_text_in_field(textfieldName, text, msg)
62
- end
63
-
64
- #links
65
- def clickLink(linkId)
66
- click_link_with_id(linkId)
67
- end
68
-
69
- def clickLinkWithText(linkText)
70
- click_link_with_text(linkText)
71
- end
72
-
73
- def assertLinkPresentWithExactText(linkText)
74
- html_links.each { |link|
75
- return if linkText == link.text
76
- }
77
- fail( "can't find the link with text: #{linkText}")
78
- end
79
-
80
- def assertLinkNotPresentWithExactText(linkText)
81
- html_links.each { |link|
82
- assert(linkText != link.text, "unexpected link (exact): #{linkText} found")
83
- }
84
- end
85
-
86
- def assertLinkPresentWithText(linkText)
87
- html_links.each { |link|
88
- return if link.text.include?(linkText)
89
- }
90
- fail( "can't find the link containing text: #{linkText}")
91
- end
92
-
93
- def assertLinkNotPresentWithText(linkText)
94
- html_links.each { |link|
95
- assert(!link.Text.include?(linkText), "unexpected link containing: #{linkText} found")
96
- }
97
- end
98
-
99
-
100
- ##
101
- # buttons
102
-
103
- # submit first submit button
104
- def submit(buttonName = nil)
105
- if (buttonName.nil?) then
106
- html_buttons.each { |button|
107
- next if button.type != 'submit'
108
- button.click
109
- return
110
- }
111
- else
112
- click_button_with_name(buttonName)
113
- end
114
- end
115
-
116
- def clickButton(buttonId)
117
- click_button_with_id(buttonId)
118
- end
119
-
120
- def clickButtonWithCaption(caption)
121
- click_button_with_caption(caption)
122
- end
123
-
124
- def clickButtonWithValue(value)
125
- click_button_with_value(value)
126
- end
127
-
128
- def assertButtonNotPresent(buttonId)
129
- html_buttons.each { |button|
130
- assert(button.id != buttonId, "unexpected button id: #{buttonId} found")
131
- }
132
- end
133
-
134
- def assertButtonNotPresentWithText(text)
135
- html_buttons.each { |button|
136
- assert(button.value != text, "unexpected button id: #{text} found")
137
- }
138
- end
139
-
140
- def assertButtonPresent(buttonID)
141
- html_buttons.each { |button|
142
- return if buttonID == button.id
143
- }
144
- assert(false, "can't find the button with id: #{buttonID}")
145
- end
146
-
147
- def assertButtonPresentWithText(buttonText)
148
- html_buttons.each { |button|
149
- return if buttonText == button.value
150
- }
151
- assert(false, "can't find the button with text: #{buttonText}")
152
- end
153
-
154
- # checkbox
155
- def checkCheckbox(checkBoxName)
156
- @@browser.checkbox(:name, checkBoxName).set
157
- end
158
-
159
- def uncheckCheckbox(checkBoxName)
160
- @@browser.checkbox(:name, checkBoxName).clear
161
- end
162
-
163
- def assertCheckboxNotSelected(checkBoxName)
164
- html_checkboxes.each { |checkbox|
165
- if (checkbox.name == checkBoxName) then
166
- assert(!checkbox.isSet?, "Checkbox #{checkBoxName} checked unexpected")
167
- end
168
- }
169
- end
170
-
171
- def assertCheckboxSelected(checkBoxName)
172
- html_checkboxes.each { |checkbox|
173
- if (checkbox.name == checkBoxName) then
174
- assert(checkbox.isSet?, "Checkbox #{checkBoxName} not checked")
175
- end
176
- }
177
- end
178
-
179
- # combo box
180
- def selectOption(selectName, option)
181
- @@browser.select_from_combobox_with_name(selectName, option)
182
- end
183
-
184
- def assertOptionValueNotPresent(selectName, optionValue)
185
- html_selects.each { |select|
186
- continue unless select.name == selectName
187
- select.o.each do |option| # items in the list
188
- assert(!(option.value == optionValue), "unexpected select option: #{optionValue} for #{selectName} found")
189
- end
190
- }
191
- end
192
-
193
- def assertOptionNotPresent(selectName, optionLabel)
194
- html_selects.each { |select|
195
- next unless select.name == selectName
196
- select.o.each do |option| # items in the list
197
- assert(!(option.text == optionLabel), "unexpected select option: #{optionLabel} for #{selectName} found")
198
- end
199
- }
200
- end
201
-
202
-
203
- def assertOptionValuePresent(selectName, optionValue)
204
- html_selects.each { |select|
205
- next unless select.name == selectName
206
- select.o.each do |option| # items in the list
207
- return if option.value == optionValue
208
- end
209
- }
210
- assert(false, "can't find the combob box with value: #{optionValue}")
211
- end
212
-
213
- def assertOptionPresent(selectName, optionLabel)
214
- html_selects.each { |select|
215
- next unless select.name == selectName
216
- select.o.each do |option| # items in the list
217
- return if option.text == optionLabel
218
- end
219
- }
220
- assert(false, "can't find the combob box: #{selectName} with value: #{optionLabel}")
221
- end
222
-
223
- def assertOptionEquals(selectName, optionLabel)
224
- html_selects.each { |select|
225
- next unless select.name == selectName
226
- select.o.each do |option| # items in the list
227
- if (option.text == optionLabel) then
228
- assert_equal(select.value, option.value, "Select #{selectName}'s value is not equal to expected option label: '#{optionLabel}'")
229
- end
230
- end
231
- }
232
- end
233
-
234
-
235
- def assertOptionValueEquals(selectName, optionValue)
236
- html_selects.each { |select|
237
- next unless select.name == selectName
238
- assert_equal(select.value, optionValue, "Select #{selectName}'s value is not equal to expected: '#{optionValue}'")
239
- }
240
- end
241
-
242
-
243
- #radio
244
-
245
- # radioGroup is the name field, radio options 'value' field
246
- def assertRadioOptionNotPresent(radioGroup, radioOption)
247
- html_radios.each { |radio|
248
- if (radio.name == radioGroup) then
249
- assert(!(radioOption == radio.value), "unexpected radio option: " + radioOption + " found")
250
- end
251
- }
252
- end
253
-
254
- def assertRadioOptionPresent(radioGroup, radioOption)
255
- html_radios.each { |radio|
256
- return if (radio.name == radioGroup) and (radioOption == radio.value)
257
- }
258
- fail("can't find the radio option : '#{radioOption}'")
259
- end
260
-
261
- def assertRadioOptionSelected(radioGroup, radioOption)
262
- html_radios.each { |radio|
263
- if (radio.name == radioGroup and radioOption == radio.value) then
264
- assert(radio.isSet?, "Radio button #{radioGroup}-[#{radioOption}] not checked")
265
- end
266
- }
267
- end
268
-
269
- def assertRadioOptionNotSelected(radioGroup, radioOption)
270
- html_radios.each { |radio|
271
- if (radio.name == radioGroup and radioOption == radio.value) then
272
- assert(!radio.isSet?, "Radio button #{radioGroup}-[#{radioOption}] checked unexpected")
273
- end
274
- }
275
- end
276
-
277
- # the method is protected in JWebUnit
278
- def clickRadioOption(radioGroup, radioOption)
279
- html_radios.each { |radio|
280
- if (radio.name == radioGroup and radioOption == radio.value) then
281
- radio.set
282
- end
283
- }
284
- end
285
-
286
- #text
287
- def assertTextPresent(text)
288
- assert((html_body().include? text), 'expected text: ' + text + ' not found')
289
- end
290
-
291
- def assertTextNotPresent(text)
292
- assert(!(html_body().include? text), 'expected text: ' + text + ' found')
293
- end
294
-
295
-
296
- def assertTextInTable(tableId, text)
297
- elem = @@browser.document.getElementById(tableId)
298
- assert_not_nil(elem, "tableId #{tableId} not exists")
299
- assert_equal(elem.tagName, "TABLE")
300
- puts elem.innerHTML if $DEBUG
301
- assert(elem.innerHTML.include?(text), "the text #{text} not found in table #{tableId}")
302
- end
303
-
304
- def assertTextNotInTable(tableId, text)
305
- elem = @@browser.document.getElementById(tableId)
306
- assert_equal(elem.name.uppercase, "TABLE")
307
- assert_not_nil(elem, "tableId #{tableId} not exists")
308
- assert(!elem.innerHTML.include?(text), "unexpected text #{text} found in table #{tableId}")
309
- end
310
-
311
- def assertElementPresent(elementID)
312
- elem = @@browser.document.getElementById(elementID)
313
- assert_not_nil(elem)
314
- end
315
-
316
- def assertElementNotPresent(elementID)
317
- elem = @@browser.document.getElementById(elementID)
318
- assert_nil(elem)
319
- end
320
-
321
- def assertTextInElement(elementID, text)
322
- elem = @@browser.document.getElementById(elementID)
323
- assert_not_nil(elem.innerText)
324
- assert(elem.innerText.include?(text))
325
- end
326
-
327
- def getElementValue(elementId)
328
- elem = @@browser.document.getElementById(elementId)
329
- return nil if elem == nil
330
- elem_val = elem.invoke('innerText')
331
- return elem_val
332
- end
333
-
334
- # ---
335
- # For deubgging
336
- # ---
337
- def dumpResponse(stream = nil)
338
- if stream.nil?
339
- puts html_body # std out
340
- else
341
- stream.puts html_body
342
- end
343
- end
344
-
345
- private
346
- def html_body
347
- @@browser.html()
348
- end
349
-
350
- def html_title
351
- @@browser.document.title
352
- end
353
-
354
- def html_links
355
- @@browser.links
356
- end
357
-
358
- def html_buttons
359
- @@browser.buttons
360
- end
361
-
362
- def html_selects
363
- @@browser.select_lists
364
- end
365
-
366
- def html_checkboxes
367
- @@browser.checkboxes
368
- end
369
-
370
- def html_radios
371
- @@browser.radios
372
- end
373
-
374
- def assert_equals(expected, actual, msg=nil)
375
- assert(expected == actual, (msg.nil?) ? "Expected: #{expected} diff from actual: #{actual}" : msg)
376
- end
377
- alias assert_equal assert_equals
378
-
379
- def assert_nil(actual, msg="")
380
- assert(actual.nil?, msg)
381
- end
382
-
383
- def assert_not_nil(actual, msg="")
384
- assert(!actual.nil?, msg)
385
- end
386
-
387
- def fail(message)
388
- assert(false, message)
389
- end
11
+ ##
12
+ # Wrapping WATIR and provide assertions
13
+ #
14
+ class WebTester
15
+ include Watir::Simple
16
+ include RUNIT::Assert
390
17
 
391
- end
18
+ attr_accessor :test_context
19
+
20
+ def initialize
21
+ @test_context = TestContext.new
22
+ end
23
+
24
+ def beginAt(relativeURL)
25
+ url = @test_context.base_url + relativeURL
26
+ new_browser_at(url) # create @@browser
27
+ end
28
+
29
+ def closeBrowser
30
+ begin
31
+ close_browser
32
+ rescue
33
+ end
34
+ end
35
+
36
+ def browser_opened?
37
+ begin
38
+ @@browser != nil
39
+ rescue => e
40
+ return false
41
+ end
42
+ end
43
+
44
+ def goBack
45
+ go_back()
46
+ end
47
+
48
+ def goForward
49
+ go_forward()
50
+ end
51
+
52
+ def gotoPage(page)
53
+ navigate_to(page)
54
+ end
55
+
56
+ # assertions
57
+ def assertTitleEquals(title)
58
+ assert_equals(title, html_title())
59
+ end
60
+
61
+ # text fields
62
+ def setFormElement(elementName, elementValue)
63
+ enter_text_into_field_with_name(elementName, elementValue)
64
+ end
65
+ alias enterText setFormElement
66
+
67
+ def assertTextPresentInTextField(textfieldName, text, msg = nil)
68
+ assert_text_in_field(textfieldName, text, msg)
69
+ end
70
+
71
+ #links
72
+ def clickLink(linkId)
73
+ click_link_with_id(linkId)
74
+ end
75
+
76
+ def clickLinkWithText(linkText)
77
+ click_link_with_text(linkText)
78
+ end
79
+
80
+ def assertLinkPresentWithExactText(linkText)
81
+ html_links.each { |link|
82
+ return if linkText == link.text
83
+ }
84
+ fail( "can't find the link with text: #{linkText}")
85
+ end
86
+
87
+ def assertLinkNotPresentWithExactText(linkText)
88
+ html_links.each { |link|
89
+ assert(linkText != link.text, "unexpected link (exact): #{linkText} found")
90
+ }
91
+ end
92
+
93
+ def assertLinkPresentWithText(linkText)
94
+ html_links.each { |link|
95
+ return if link.text.include?(linkText)
96
+ }
97
+ fail( "can't find the link containing text: #{linkText}")
98
+ end
99
+
100
+ def assertLinkNotPresentWithText(linkText)
101
+ html_links.each { |link|
102
+ assert(!link.Text.include?(linkText), "unexpected link containing: #{linkText} found")
103
+ }
104
+ end
105
+
106
+
107
+ ##
108
+ # buttons
109
+
110
+ # submit first submit button
111
+ def submit(buttonName = nil)
112
+ if (buttonName.nil?) then
113
+ html_buttons.each { |button|
114
+ next if button.type != 'submit'
115
+ button.click
116
+ return
117
+ }
118
+ else
119
+ click_button_with_name(buttonName)
120
+ end
121
+ end
122
+
123
+ def clickButton(buttonId)
124
+ click_button_with_id(buttonId)
125
+ end
126
+
127
+ def clickButtonWithCaption(caption)
128
+ click_button_with_caption(caption)
129
+ end
130
+
131
+ def clickButtonWithValue(value)
132
+ click_button_with_value(value)
133
+ end
134
+
135
+ def assertButtonNotPresent(buttonId)
136
+ html_buttons.each { |button|
137
+ assert(button.id != buttonId, "unexpected button id: #{buttonId} found")
138
+ }
139
+ end
140
+
141
+ def assertButtonNotPresentWithText(text)
142
+ html_buttons.each { |button|
143
+ assert(button.value != text, "unexpected button id: #{text} found")
144
+ }
145
+ end
146
+
147
+ def assertButtonPresent(buttonID)
148
+ html_buttons.each { |button|
149
+ return if buttonID == button.id
150
+ }
151
+ assert(false, "can't find the button with id: #{buttonID}")
152
+ end
153
+
154
+ def assertButtonPresentWithText(buttonText)
155
+ html_buttons.each { |button|
156
+ return if buttonText == button.value
157
+ }
158
+ assert(false, "can't find the button with text: #{buttonText}")
159
+ end
160
+
161
+ # checkbox
162
+ def checkCheckbox(checkBoxName)
163
+ @@browser.checkbox(:name, checkBoxName).set
164
+ end
165
+
166
+ def uncheckCheckbox(checkBoxName)
167
+ @@browser.checkbox(:name, checkBoxName).clear
168
+ end
169
+
170
+ def assertCheckboxNotSelected(checkBoxName)
171
+ html_checkboxes.each { |checkbox|
172
+ if (checkbox.name == checkBoxName) then
173
+ assert(!checkbox.isSet?, "Checkbox #{checkBoxName} checked unexpected")
174
+ end
175
+ }
176
+ end
177
+
178
+ def assertCheckboxSelected(checkBoxName)
179
+ html_checkboxes.each { |checkbox|
180
+ if (checkbox.name == checkBoxName) then
181
+ assert(checkbox.isSet?, "Checkbox #{checkBoxName} not checked")
182
+ end
183
+ }
184
+ end
185
+
186
+ # combo box
187
+ def selectOption(selectName, option)
188
+ @@browser.select_from_combobox_with_name(selectName, option)
189
+ end
190
+
191
+ def assertOptionValueNotPresent(selectName, optionValue)
192
+ html_selects.each { |select|
193
+ continue unless select.name == selectName
194
+ select.o.each do |option| # items in the list
195
+ assert(!(option.value == optionValue), "unexpected select option: #{optionValue} for #{selectName} found")
196
+ end
197
+ }
198
+ end
199
+
200
+ def assertOptionNotPresent(selectName, optionLabel)
201
+ html_selects.each { |select|
202
+ next unless select.name == selectName
203
+ select.o.each do |option| # items in the list
204
+ assert(!(option.text == optionLabel), "unexpected select option: #{optionLabel} for #{selectName} found")
205
+ end
206
+ }
207
+ end
208
+
209
+
210
+ def assertOptionValuePresent(selectName, optionValue)
211
+ html_selects.each { |select|
212
+ next unless select.name == selectName
213
+ select.o.each do |option| # items in the list
214
+ return if option.value == optionValue
215
+ end
216
+ }
217
+ assert(false, "can't find the combob box with value: #{optionValue}")
218
+ end
392
219
 
393
- end
220
+ def assertOptionPresent(selectName, optionLabel)
221
+ html_selects.each { |select|
222
+ next unless select.name == selectName
223
+ select.o.each do |option| # items in the list
224
+ return if option.text == optionLabel
225
+ end
226
+ }
227
+ assert(false, "can't find the combob box: #{selectName} with value: #{optionLabel}")
228
+ end
229
+
230
+ def assertOptionEquals(selectName, optionLabel)
231
+ html_selects.each { |select|
232
+ next unless select.name == selectName
233
+ select.o.each do |option| # items in the list
234
+ if (option.text == optionLabel) then
235
+ assert_equal(select.value, option.value, "Select #{selectName}'s value is not equal to expected option label: '#{optionLabel}'")
236
+ end
237
+ end
238
+ }
239
+ end
240
+
241
+
242
+ def assertOptionValueEquals(selectName, optionValue)
243
+ html_selects.each { |select|
244
+ next unless select.name == selectName
245
+ assert_equal(select.value, optionValue, "Select #{selectName}'s value is not equal to expected: '#{optionValue}'")
246
+ }
247
+ end
248
+
249
+
250
+ #radio
251
+
252
+ # radioGroup is the name field, radio options 'value' field
253
+ def assertRadioOptionNotPresent(radioGroup, radioOption)
254
+ html_radios.each { |radio|
255
+ if (radio.name == radioGroup) then
256
+ assert(!(radioOption == radio.value), "unexpected radio option: " + radioOption + " found")
257
+ end
258
+ }
259
+ end
260
+
261
+ def assertRadioOptionPresent(radioGroup, radioOption)
262
+ html_radios.each { |radio|
263
+ return if (radio.name == radioGroup) and (radioOption == radio.value)
264
+ }
265
+ fail("can't find the radio option : '#{radioOption}'")
266
+ end
267
+
268
+ def assertRadioOptionSelected(radioGroup, radioOption)
269
+ html_radios.each { |radio|
270
+ if (radio.name == radioGroup and radioOption == radio.value) then
271
+ assert(radio.isSet?, "Radio button #{radioGroup}-[#{radioOption}] not checked")
272
+ end
273
+ }
274
+ end
275
+
276
+ def assertRadioOptionNotSelected(radioGroup, radioOption)
277
+ html_radios.each { |radio|
278
+ if (radio.name == radioGroup and radioOption == radio.value) then
279
+ assert(!radio.isSet?, "Radio button #{radioGroup}-[#{radioOption}] checked unexpected")
280
+ end
281
+ }
282
+ end
283
+
284
+ # the method is protected in JWebUnit
285
+ def clickRadioOption(radioGroup, radioOption)
286
+ html_radios.each { |radio|
287
+ if (radio.name == radioGroup and radioOption == radio.value) then
288
+ radio.set
289
+ end
290
+ }
291
+ end
292
+
293
+ #text
294
+ def assertTextPresent(text)
295
+ assert((html_body().include? text), 'expected text: ' + text + ' not found')
296
+ end
297
+
298
+ def assertTextNotPresent(text)
299
+ assert(!(html_body().include? text), 'expected text: ' + text + ' found')
300
+ end
301
+
302
+
303
+ def assertTextInTable(tableId, text)
304
+ elem = @@browser.document.getElementById(tableId)
305
+ assert_not_nil(elem, "tableId #{tableId} not exists")
306
+ assert_equal(elem.tagName, "TABLE")
307
+ puts elem.innerHTML if $DEBUG
308
+ assert(elem.innerHTML.include?(text), "the text #{text} not found in table #{tableId}")
309
+ end
310
+
311
+ def assertTextNotInTable(tableId, text)
312
+ elem = @@browser.document.getElementById(tableId)
313
+ assert_equal(elem.name.uppercase, "TABLE")
314
+ assert_not_nil(elem, "tableId #{tableId} not exists")
315
+ assert(!elem.innerHTML.include?(text), "unexpected text #{text} found in table #{tableId}")
316
+ end
317
+
318
+ def assertElementPresent(elementID)
319
+ assert_not_nil(@@browser.document.getElementById(elementID), "element with id #{elementID} not found")
320
+ end
321
+
322
+ def assertElementNotPresent(elementID)
323
+ assert_nil(@@browser.document.getElementById(elementID), "unexpected element with id #{elementID} found")
324
+ end
325
+
326
+ def assertTextInElement(elementID, text)
327
+ assertElementPresent(elementID)
328
+ elem = @@browser.document.getElementById(elementID)
329
+ assert_not_nil(elem.innerText, "element #{elementID} has no text")
330
+ assert(elem.innerText.include?(text), "the text #{text} not found in element #{elementID}")
331
+ end
332
+
333
+ def getElementValue(elementId)
334
+ assertElementPresent(elementId)
335
+ elem = @@browser.document.getElementById(elementId)
336
+ elem_val = elem.invoke('innerText')
337
+ end
338
+
339
+ # new API
340
+ def getHtmlInElement(elementId)
341
+ elem = @@browser.document.getElementById(elementId)
342
+ assert_not_nil(elem, "HTML element: #{elementId} not exists")
343
+ elem.innerHTML
344
+ end
345
+
346
+ def getElementById(elementId)
347
+ elem = @@browser.document.getElementById(elementId)
348
+ end
349
+
350
+
351
+ # ---
352
+ # For deubgging
353
+ # ---
354
+ def dumpResponse(stream = nil)
355
+ if stream.nil?
356
+ puts html_body # std out
357
+ else
358
+ stream.puts html_body
359
+ end
360
+ end
361
+
362
+ private
363
+ def html_body
364
+ @@browser.html()
365
+ end
366
+
367
+ def html_title
368
+ @@browser.document.title
369
+ end
370
+
371
+ def html_links
372
+ @@browser.links
373
+ end
374
+
375
+ def html_buttons
376
+ @@browser.buttons
377
+ end
378
+
379
+ def html_selects
380
+ @@browser.select_lists
381
+ end
382
+
383
+ def html_checkboxes
384
+ @@browser.checkboxes
385
+ end
386
+
387
+ def html_radios
388
+ @@browser.radios
389
+ end
390
+
391
+ def assert_equals(expected, actual, msg=nil)
392
+ assert(expected == actual, (msg.nil?) ? "Expected: #{expected} diff from actual: #{actual}" : msg)
393
+ end
394
+ alias assert_equal assert_equals
395
+
396
+ def assert_nil(actual, msg="")
397
+ assert(actual.nil?, msg)
398
+ end
399
+
400
+ def assert_not_nil(actual, msg="")
401
+ assert(!actual.nil?, msg)
402
+ end
403
+
404
+ def fail(message)
405
+ assert(false, message)
406
+ end
407
+
408
+ end
409
+
410
+ end