rwebunit 1.0.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,170 +1,214 @@
1
- require 'runit/assert'
1
+ require 'test/unit/assertions'
2
2
 
3
3
  module RWebUnit
4
4
  module Assert
5
5
 
6
- include RUNIT::Assert
6
+ include Test::Unit::Assertions
7
+
8
+ def assert_not(condition, msg = "")
9
+ assert(!condition, msg)
10
+ end
11
+
12
+ def assert_nil(actual, msg="")
13
+ assert(actual.nil?, msg)
14
+ end
7
15
 
8
- #TODO for drag-n-drop, check the postion in list
9
- # def assert_position_in_list(list_element_id)
10
- # raise "not implemented"
11
- # end
16
+ def assert_not_nil(actual, msg="")
17
+ assert(!actual.nil?, msg)
18
+ end
12
19
 
13
- ##
14
- # text
20
+ def fail(message)
21
+ assert(false, message)
22
+ end
23
+
24
+ # assertions
25
+ def assert_title_equals(title)
26
+ assert_equals(title, @web_browser.page_title)
27
+ end
28
+ alias assert_title assert_title_equals
29
+
30
+ # Assert text present in page source (html)
31
+ # assert_text_present("<h1>iTest2</h1>")
15
32
  def assert_text_present(text)
16
33
  assert((@web_browser.page_source.include? text), 'expected text: ' + text + ' not found')
17
34
  end
18
35
 
36
+ # Assert text not present in page source (html)
37
+ # assert_text_not_present("<h1>iTest2</h1>")
19
38
  def assert_text_not_present(text)
20
39
  assert(!(@web_browser.page_source.include? text), 'expected text: ' + text + ' found')
21
40
  end
22
41
 
23
- def assert_text_in_table(tableId, text)
24
- elem = element_by_id(tableId)
25
- assert_not_nil(elem, "tableId #{tableId} not exists")
26
- assert_equal(elem.tagName.upcase, "TABLE")
27
- puts elem.innerHTML if $DEBUG
28
- assert(elem.innerHTML.include?(text), "the text #{text} not found in table #{tableId}")
29
- end
30
42
 
31
- def assert_text_not_in_table(tableId, text)
32
- elem = element_by_id(tableId)
33
- assert_equal(elem.tagName.upcase, "TABLE")
34
- assert_not_nil(elem, "tableId #{tableId} not exists")
35
- assert(!elem.innerHTML.include?(text), "unexpected text #{text} found in table #{tableId}")
43
+ ##
44
+ # Link
45
+
46
+ # Assert a link with specified text (exact match) in the page
47
+ #
48
+ # <a href="">Click Me</a>
49
+ # assert_link_present_with_exact("Click Me") => true
50
+ # assert_link_present_with_exact("Click") => false
51
+ #
52
+ def assert_link_present_with_exact(link_text)
53
+ @web_browser.links.each { |link|
54
+ return if link_text == link.text
55
+ }
56
+ fail( "can't find the link with text: #{link_text}")
36
57
  end
37
58
 
38
- def assert_element_present(elementID)
39
- assert_not_nil(element_by_id(elementID), "element with id #{elementID} not found")
59
+ def assert_link_not_present_with_exact(link_text)
60
+ @web_browser.links.each { |link|
61
+ assert(link_text != link.text, "unexpected link (exact): #{link_text} found")
62
+ }
40
63
  end
41
64
 
42
- def assert_element_not_present(elementID)
43
- assert_nil(element_by_id(elementID), "unexpected element with id #{elementID} found")
65
+ # Assert a link containing specified text in the page
66
+ #
67
+ # <a href="">Click Me</a>
68
+ # assert_link_present_with_text("Click ") # =>
69
+ #
70
+ def assert_link_present_with_text(link_text)
71
+ @web_browser.links.each { |link|
72
+ return if link.text.include?(link_text)
73
+ }
74
+ fail( "can't find the link containing text: #{link_text}")
44
75
  end
45
76
 
46
- def assert_text_in_element(elementID, text)
47
- elem = element_by_id(elementID)
48
- assert_not_nil(elem.innerText, "element #{elementID} has no text")
49
- assert(elem.innerText.include?(text), "the text #{text} not found in element #{elementID}")
77
+ def assert_link_not_present_with_text(link_text)
78
+ @web_browser.links.each { |link|
79
+ assert(!link.text.include?(link_text), "unexpected link containing: #{link_text} found")
80
+ }
50
81
  end
51
82
 
83
+
52
84
  ##
53
85
  # Checkbox
54
- def assert_checkbox_not_selected(checkBoxName)
86
+ def assert_checkbox_not_selected(checkbox_name)
55
87
  @web_browser.checkboxes.each { |checkbox|
56
- if (checkbox.name == checkBoxName) then
57
- assert(!checkbox.isSet?, "Checkbox #{checkBoxName} checked unexpected")
88
+ if (checkbox.name == checkbox_name) then
89
+ assert(!checkbox.isSet?, "Checkbox #{checkbox_name} is checked unexpectly")
58
90
  end
59
91
  }
60
92
  end
93
+ alias assert_checkbox_not_checked assert_checkbox_not_selected
61
94
 
62
- def assert_checkbox_selected(checkBoxName)
95
+ def assert_checkbox_selected(checkbox_name)
63
96
  @web_browser.checkboxes.each { |checkbox|
64
- if (checkbox.name == checkBoxName) then
65
- assert(checkbox.isSet?, "Checkbox #{checkBoxName} not checked")
97
+ if (checkbox.name == checkbox_name) then
98
+ assert(checkbox.isSet?, "Checkbox #{checkbox_name} not checked")
66
99
  end
67
100
  }
68
101
  end
102
+ alias assert_checkbox_checked assert_checkbox_selected
69
103
 
70
104
  ##
71
105
  # select
72
- def assert_option_value_not_present(selectName, optionValue)
106
+ def assert_option_value_not_present(select_name, option_value)
73
107
  @web_browser.select_lists.each { |select|
74
- continue unless select.name == selectName
108
+ continue unless select.name == select_name
75
109
  select.o.each do |option| # items in the list
76
- assert(!(option.value == optionValue), "unexpected select option: #{optionValue} for #{selectName} found")
110
+ assert(!(option.value == option_value), "unexpected select option: #{option_value} for #{select_name} found")
77
111
  end
78
112
  }
79
113
  end
114
+ alias assert_select_value_not_present assert_option_value_not_present
80
115
 
81
- def assert_option_not_present(selectName, optionLabel)
116
+ def assert_option_not_present(select_name, option_label)
82
117
  @web_browser.select_lists.each { |select|
83
- next unless select.name == selectName
118
+ next unless select.name == select_name
84
119
  select.o.each do |option| # items in the list
85
- assert(!(option.text == optionLabel), "unexpected select option: #{optionLabel} for #{selectName} found")
120
+ assert(!(option.text == option_label), "unexpected select option: #{option_label} for #{select_name} found")
86
121
  end
87
122
  }
88
123
  end
124
+ alias assert_select_label_not_present assert_option_not_present
89
125
 
90
- def assert_option_value_present(selectName, optionValue)
126
+ def assert_option_value_present(select_name, option_value)
91
127
  @web_browser.select_lists.each { |select|
92
- next unless select.name == selectName
128
+ next unless select.name == select_name
93
129
  select.o.each do |option| # items in the list
94
- return if option.value == optionValue
130
+ return if option.value == option_value
95
131
  end
96
132
  }
97
- assert(false, "can't find the combob box with value: #{optionValue}")
133
+ assert(false, "can't find the combob box with value: #{option_value}")
98
134
  end
135
+ alias assert_select_value_present assert_option_value_present
99
136
 
100
- def assert_option_present(selectName, optionLabel)
137
+ def assert_option_present(select_name, option_label)
101
138
  @web_browser.select_lists.each { |select|
102
- next unless select.name == selectName
139
+ next unless select.name == select_name
103
140
  select.o.each do |option| # items in the list
104
- return if option.text == optionLabel
141
+ return if option.text == option_label
105
142
  end
106
143
  }
107
- assert(false, "can't find the combob box: #{selectName} with value: #{optionLabel}")
144
+ assert(false, "can't find the combob box: #{select_name} with value: #{option_label}")
108
145
  end
146
+ alias assert_select_label_present assert_option_present
109
147
 
110
- def assert_option_equals(selectName, optionLabel)
148
+ def assert_option_equals(select_name, option_label)
111
149
  @web_browser.select_lists.each { |select|
112
- next unless select.name == selectName
150
+ next unless select.name == select_name
113
151
  select.o.each do |option| # items in the list
114
- if (option.text == optionLabel) then
115
- assert_equal(select.value, option.value, "Select #{selectName}'s value is not equal to expected option label: '#{optionLabel}'")
152
+ if (option.text == option_label) then
153
+ assert_equal(select.value, option.value, "Select #{select_name}'s value is not equal to expected option label: '#{option_label}'")
116
154
  end
117
155
  end
118
156
  }
119
157
  end
158
+ alias assert_select_label assert_option_equals
120
159
 
121
- def assert_option_value_equals(selectName, optionValue)
160
+ def assert_option_value_equals(select_name, option_value)
122
161
  @web_browser.select_lists.each { |select|
123
- next unless select.name == selectName
124
- assert_equal(select.value, optionValue, "Select #{selectName}'s value is not equal to expected: '#{optionValue}'")
162
+ next unless select.name == select_name
163
+ assert_equal(select.value, option_value, "Select #{select_name}'s value is not equal to expected: '#{option_value}'")
125
164
  }
126
165
  end
127
-
166
+ alias assert_select_value assert_option_value_equals
167
+
128
168
  ##
129
169
  # radio
130
170
 
131
- # radioGroup is the name field, radio options 'value' field
132
- def assert_radio_option_not_present(radioGroup, radioOption)
171
+ # radio_group is the name field, radio options 'value' field
172
+ def assert_radio_option_not_present(radio_group, radio_option)
133
173
  @web_browser.radios.each { |radio|
134
- if (radio.name == radioGroup) then
135
- assert(!(radioOption == radio.value), "unexpected radio option: " + radioOption + " found")
174
+ if (radio.name == radio_group) then
175
+ assert(!(radio_option == radio.value), "unexpected radio option: " + radio_option + " found")
136
176
  end
137
177
  }
138
178
  end
139
-
140
- def assert_radio_option_present(radioGroup, radioOption)
179
+
180
+ def assert_radio_option_present(radio_group, radio_option)
141
181
  @web_browser.radios.each { |radio|
142
- return if (radio.name == radioGroup) and (radioOption == radio.value)
182
+ return if (radio.name == radio_group) and (radio_option == radio.value)
143
183
  }
144
- fail("can't find the radio option : '#{radioOption}'")
184
+ fail("can't find the radio option : '#{radio_option}'")
145
185
  end
146
186
 
147
- def assert_radio_option_selected(radioGroup, radioOption)
187
+ def assert_radio_option_selected(radio_group, radio_option)
148
188
  @web_browser.radios.each { |radio|
149
- if (radio.name == radioGroup and radioOption == radio.value) then
150
- assert(radio.isSet?, "Radio button #{radioGroup}-[#{radioOption}] not checked")
189
+ if (radio.name == radio_group and radio_option == radio.value) then
190
+ assert(radio.isSet?, "Radio button #{radio_group}-[#{radio_option}] not checked")
151
191
  end
152
192
  }
153
193
  end
154
-
155
- def assert_radio_option_not_selected(radioGroup, radioOption)
194
+ alias assert_radio_button_checked assert_radio_option_selected
195
+ alias assert_radio_option_checked assert_radio_option_selected
196
+
197
+ def assert_radio_option_not_selected(radio_group, radio_option)
156
198
  @web_browser.radios.each { |radio|
157
- if (radio.name == radioGroup and radioOption == radio.value) then
158
- assert(!radio.isSet?, "Radio button #{radioGroup}-[#{radioOption}] checked unexpected")
199
+ if (radio.name == radio_group and radio_option == radio.value) then
200
+ assert(!radio.isSet?, "Radio button #{radio_group}-[#{radio_option}] checked unexpected")
159
201
  end
160
202
  }
161
203
  end
162
-
204
+ alias assert_radio_button_not_checked assert_radio_option_not_selected
205
+ alias assert_radio_option_not_checked assert_radio_option_not_selected
206
+
163
207
  ##
164
208
  # Button
165
- def assert_button_not_present(buttonId)
209
+ def assert_button_not_present(button_id)
166
210
  @web_browser.buttons.each { |button|
167
- assert(button.id != buttonId, "unexpected button id: #{buttonId} found")
211
+ assert(button.id != button_id, "unexpected button id: #{button_id} found")
168
212
  }
169
213
  end
170
214
 
@@ -174,77 +218,144 @@ module RWebUnit
174
218
  }
175
219
  end
176
220
 
177
- def assert_button_present(buttonID)
221
+ def assert_button_present(button_id)
178
222
  @web_browser.buttons.each { |button|
179
- return if buttonID == button.id
223
+ return if button_id == button.id
180
224
  }
181
- assert(false, "can't find the button with id: #{buttonID}")
225
+ assert(false, "can't find the button with id: #{button_id}")
182
226
  end
183
227
 
184
- def assert_button_present_with_text(buttonText)
228
+ def assert_button_present_with_text(button_text)
185
229
  @web_browser.buttons.each { |button|
186
- return if buttonText == button.value
230
+ return if button_text == button.value
187
231
  }
188
- assert(false, "can't find the button with text: #{buttonText}")
232
+ assert(false, "can't find the button with text: #{button_text}")
189
233
  end
190
234
 
191
- ##
192
- # Link
193
- def assert_link_present_with_exact(linkText)
194
- @web_browser.links.each { |link|
195
- return if linkText == link.text
196
- }
197
- fail( "can't find the link with text: #{linkText}")
198
- end
199
-
200
- def assert_link_not_present_with_exact(linkText)
201
- @web_browser.links.each { |link|
202
- assert(linkText != link.text, "unexpected link (exact): #{linkText} found")
203
- }
204
- end
205
-
206
- def assert_link_present_with_text(linkText)
207
- @web_browser.links.each { |link|
208
- return if link.text.include?(linkText)
209
- }
210
- fail( "can't find the link containing text: #{linkText}")
211
- end
212
-
213
- def assert_link_not_present_with_text(linkText)
214
- @web_browser.links.each { |link|
215
- assert(!link.text.include?(linkText), "unexpected link containing: #{linkText} found")
216
- }
217
- end
218
-
219
- def assert_text_present_in_text_field(textfieldName, text, msg = nil)
220
- @web_browser.textfields.each { |textfield|
221
- if (textfield.name == textfieldName) then
222
- assert(text_field.value.include?(text), "text: #{text} not in text field: " + textfieldName)
223
- end
224
- }
225
- end
226
-
227
- # assertions
228
- def assert_title_equals(title)
229
- assert_equals(title, @web_browser.page_title)
230
- end
231
- alias assert_title assert_title_equals
232
235
 
233
236
  def assert_equals(expected, actual, msg=nil)
234
237
  assert(expected == actual, (msg.nil?) ? "Expected: #{expected} diff from actual: #{actual}" : msg)
235
238
  end
236
239
 
237
- def assert_nil(actual, msg="")
238
- assert(actual.nil?, msg)
239
- end
240
240
 
241
- def assert_not_nil(actual, msg="")
242
- assert(!actual.nil?, msg)
243
- end
244
241
 
245
- def fail(message)
246
- assert(false, message)
247
- end
242
+ # Check a HTML element exists or not
243
+ # Example:
244
+ # assert_exists("label", "receipt_date")
245
+ # assert_exists(:span, :receipt_date)
246
+ def assert_exists(tag, element_id) {}
247
+ begin
248
+ assert eval("#{tag}(:id, '#{element_id.to_s}').exists?")
249
+ rescue => e
250
+ raise "Element '#{tag}' with id: '#{element_id}' not found, #{e}"
251
+ end
252
+ end
253
+ alias assert_exists? assert_exists
254
+ alias assert_element_exists assert_exists
255
+
256
+ def assert_not_exists(tag, element_id) {}
257
+ begin
258
+ assert_not eval("#{tag}(:id, '#{element_id.to_s}').exists?")
259
+ raise "Unexpected element'#{tag}' + with id: '#{element_id}' found"
260
+ rescue => e
261
+ end
262
+ end
263
+ alias assert_not_exists? assert_not_exists
264
+ alias assert_element_not_exists? assert_not_exists
265
+
266
+
267
+ # Assert tag with element id is visible?, eg.
268
+ # assert_visible(:div, "public_notice")
269
+ # assert_visible(:span, "public_span")
270
+ def assert_visible(tag, element_id)
271
+ begin
272
+ assert(eval("#{tag}(:id, '#{element_id.to_s}').visible?"))
273
+ rescue => e
274
+ raise "Element '#{tag}' with id: '#{element_id}' not visible, #{e}"
275
+ end
276
+ end
277
+
278
+ # Assert tag with element id is hidden?, example
279
+ # assert_hidden(:div, "secret")
280
+ # assert_hidden(:span, "secret_span")
281
+ def assert_hidden(tag, element_id)
282
+ begin
283
+ assert(!eval("#{tag}(:id, '#{element_id.to_s}').visible?"))
284
+ rescue => e
285
+ raise "Element '#{tag}' with id: '#{element_id}' is visible, #{e}"
286
+ end
287
+ end
288
+ alias assert_not_visible assert_hidden
289
+
290
+
291
+ # Assert given text appear inside a table (inside <table> tag like below)
292
+ #
293
+ # <table id="t1">
294
+ #
295
+ # <tbody>
296
+ # <tr id="row_1">
297
+ # <td id="cell_1_1">A</td>
298
+ # <td id="cell_1_2">B</td>
299
+ # </tr>
300
+ # <tr id="row_2">
301
+ # <td id="cell_2_1">a</td>
302
+ # <td id="cell_2_2">b</td>
303
+ # </tr>
304
+ # </tbody>
305
+ #
306
+ # </table>
307
+ #
308
+ # The plain text view of above table
309
+ # A B a b
310
+ #
311
+ # Examples
312
+ # assert_text_present_in_table("t1", ">A<") # => true
313
+ # assert_text_present_in_table("t1", ">A<", :just_plain_text => true) # => false
314
+ def assert_text_present_in_table(table_id, text, options = { :just_plain_text => false })
315
+ assert(table_source(table_id, options).include?(text), "the text #{text} not found in table #{table_id}")
316
+ end
317
+ alias assert_text_in_table assert_text_present_in_table
318
+
319
+ def assert_text_not_present_in_table(table_id, text, options = { :just_plain_text => false })
320
+ assert_not(table_source(table_id, options).include?(text), "the text #{text} not found in table #{table_id}")
321
+ end
322
+ alias assert_text_not_in_table assert_text_not_present_in_table
323
+
324
+ # Assert a text field (with given name) has the value
325
+ #
326
+ # <input id="tid" name="text1" value="text already there" type="text">
327
+ #
328
+ # assert_text_field_value("text1", "text already there") => true
329
+ #
330
+ def assert_text_field_value(textfield_name, text)
331
+ assert_equal(text, text_field(:name, textfield_name).value)
332
+ end
333
+
334
+
335
+ #-- Not tested
336
+ # -----
337
+
338
+ def assert_text_in_element(element_id, text)
339
+ elem = element_by_id(element_id)
340
+ assert_not_nil(elem.innerText, "element #{element_id} has no text")
341
+ assert(elem.innerText.include?(text), "the text #{text} not found in element #{element_id}")
342
+ end
343
+
344
+ # Use
345
+ #
248
346
 
347
+ #TODO for drag-n-drop, check the postion in list
348
+ # def assert_position_in_list(list_element_id)
349
+ # raise "not implemented"
350
+ # end
351
+
352
+ private
353
+ def table_source(table_id, options)
354
+ elem_table = table(:id, table_id.to_s)
355
+ elem_table_text = elem_table.text
356
+ elem_table_html = is_firefox? ? elem_table.innerHTML : elem_table.html
357
+ table_source = options[:just_plain_text] ? elem_table_text : elem_table_html
358
+ end
359
+
249
360
  end
250
361
  end
@@ -1,5 +1,5 @@
1
1
  #***********************************************************
2
- #* Copyright (c) 2006, 2007 Zhimin Zhan.
2
+ #* Copyright (c) 2006 - 2009 Zhimin Zhan.
3
3
  #* Distributed open-source, see full license in MIT-LICENSE
4
4
  #***********************************************************
5
5