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