rwebspec 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,361 @@
1
+ require 'test/unit/assertions'
2
+
3
+ module RWebSpec
4
+ module Assert
5
+
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
15
+
16
+ def assert_not_nil(actual, msg="")
17
+ assert(!actual.nil?, msg)
18
+ end
19
+
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>")
32
+ def assert_text_present(text)
33
+ assert((@web_browser.page_source.include? text), 'expected text: ' + text + ' not found')
34
+ end
35
+
36
+ # Assert text not present in page source (html)
37
+ # assert_text_not_present("<h1>iTest2</h1>")
38
+ def assert_text_not_present(text)
39
+ assert(!(@web_browser.page_source.include? text), 'expected text: ' + text + ' found')
40
+ end
41
+
42
+
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}")
57
+ end
58
+
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
+ }
63
+ end
64
+
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}")
75
+ end
76
+
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
+ }
81
+ end
82
+
83
+
84
+ ##
85
+ # Checkbox
86
+ def assert_checkbox_not_selected(checkbox_name)
87
+ @web_browser.checkboxes.each { |checkbox|
88
+ if (checkbox.name == checkbox_name) then
89
+ assert(!checkbox.isSet?, "Checkbox #{checkbox_name} is checked unexpectly")
90
+ end
91
+ }
92
+ end
93
+ alias assert_checkbox_not_checked assert_checkbox_not_selected
94
+
95
+ def assert_checkbox_selected(checkbox_name)
96
+ @web_browser.checkboxes.each { |checkbox|
97
+ if (checkbox.name == checkbox_name) then
98
+ assert(checkbox.isSet?, "Checkbox #{checkbox_name} not checked")
99
+ end
100
+ }
101
+ end
102
+ alias assert_checkbox_checked assert_checkbox_selected
103
+
104
+ ##
105
+ # select
106
+ def assert_option_value_not_present(select_name, option_value)
107
+ @web_browser.select_lists.each { |select|
108
+ continue unless select.name == select_name
109
+ select.o.each do |option| # items in the list
110
+ assert(!(option.value == option_value), "unexpected select option: #{option_value} for #{select_name} found")
111
+ end
112
+ }
113
+ end
114
+ alias assert_select_value_not_present assert_option_value_not_present
115
+
116
+ def assert_option_not_present(select_name, option_label)
117
+ @web_browser.select_lists.each { |select|
118
+ next unless select.name == select_name
119
+ select.o.each do |option| # items in the list
120
+ assert(!(option.text == option_label), "unexpected select option: #{option_label} for #{select_name} found")
121
+ end
122
+ }
123
+ end
124
+ alias assert_select_label_not_present assert_option_not_present
125
+
126
+ def assert_option_value_present(select_name, option_value)
127
+ @web_browser.select_lists.each { |select|
128
+ next unless select.name == select_name
129
+ select.o.each do |option| # items in the list
130
+ return if option.value == option_value
131
+ end
132
+ }
133
+ assert(false, "can't find the combob box with value: #{option_value}")
134
+ end
135
+ alias assert_select_value_present assert_option_value_present
136
+
137
+ def assert_option_present(select_name, option_label)
138
+ @web_browser.select_lists.each { |select|
139
+ next unless select.name == select_name
140
+ select.o.each do |option| # items in the list
141
+ return if option.text == option_label
142
+ end
143
+ }
144
+ assert(false, "can't find the combob box: #{select_name} with value: #{option_label}")
145
+ end
146
+ alias assert_select_label_present assert_option_present
147
+
148
+ def assert_option_equals(select_name, option_label)
149
+ @web_browser.select_lists.each { |select|
150
+ next unless select.name == select_name
151
+ select.o.each do |option| # items in the list
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}'")
154
+ end
155
+ end
156
+ }
157
+ end
158
+ alias assert_select_label assert_option_equals
159
+
160
+ def assert_option_value_equals(select_name, option_value)
161
+ @web_browser.select_lists.each { |select|
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}'")
164
+ }
165
+ end
166
+ alias assert_select_value assert_option_value_equals
167
+
168
+ ##
169
+ # radio
170
+
171
+ # radio_group is the name field, radio options 'value' field
172
+ def assert_radio_option_not_present(radio_group, radio_option)
173
+ @web_browser.radios.each { |radio|
174
+ if (radio.name == radio_group) then
175
+ assert(!(radio_option == radio.value), "unexpected radio option: " + radio_option + " found")
176
+ end
177
+ }
178
+ end
179
+
180
+ def assert_radio_option_present(radio_group, radio_option)
181
+ @web_browser.radios.each { |radio|
182
+ return if (radio.name == radio_group) and (radio_option == radio.value)
183
+ }
184
+ fail("can't find the radio option : '#{radio_option}'")
185
+ end
186
+
187
+ def assert_radio_option_selected(radio_group, radio_option)
188
+ @web_browser.radios.each { |radio|
189
+ if (radio.name == radio_group and radio_option == radio.value) then
190
+ assert(radio.isSet?, "Radio button #{radio_group}-[#{radio_option}] not checked")
191
+ end
192
+ }
193
+ end
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)
198
+ @web_browser.radios.each { |radio|
199
+ if (radio.name == radio_group and radio_option == radio.value) then
200
+ assert(!radio.isSet?, "Radio button #{radio_group}-[#{radio_option}] checked unexpected")
201
+ end
202
+ }
203
+ end
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
+
207
+ ##
208
+ # Button
209
+ def assert_button_not_present(button_id)
210
+ @web_browser.buttons.each { |button|
211
+ assert(button.id != button_id, "unexpected button id: #{button_id} found")
212
+ }
213
+ end
214
+
215
+ def assert_button_not_present_with_text(text)
216
+ @web_browser.buttons.each { |button|
217
+ assert(button.value != text, "unexpected button id: #{text} found")
218
+ }
219
+ end
220
+
221
+ def assert_button_present(button_id)
222
+ @web_browser.buttons.each { |button|
223
+ return if button_id == button.id
224
+ }
225
+ assert(false, "can't find the button with id: #{button_id}")
226
+ end
227
+
228
+ def assert_button_present_with_text(button_text)
229
+ @web_browser.buttons.each { |button|
230
+ return if button_text == button.value
231
+ }
232
+ assert(false, "can't find the button with text: #{button_text}")
233
+ end
234
+
235
+
236
+ def assert_equals(expected, actual, msg=nil)
237
+ assert(expected == actual, (msg.nil?) ? "Expected: #{expected} diff from actual: #{actual}" : msg)
238
+ end
239
+
240
+
241
+
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
+ #
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
+
360
+ end
361
+ end
@@ -0,0 +1,15 @@
1
+ #
2
+ # clickJSDialog.rb
3
+ #
4
+ #
5
+ # This file contains the JS clicker when it runs as a separate process
6
+ require 'watir/winClicker'
7
+
8
+ button = "OK"
9
+ button = ARGV[0] unless ARGV[0] == nil
10
+ sleepTime = 0
11
+ sleepTime = ARGV[1] unless ARGV[1] == nil
12
+
13
+ clicker= WinClicker.new
14
+ result = clicker.clickJavaScriptDialog( button )
15
+ clicker = nil
@@ -0,0 +1,24 @@
1
+ #***********************************************************
2
+ #* Copyright (c) 2006 - 2009 Zhimin Zhan.
3
+ #* Distributed open-source, see full license in MIT-LICENSE
4
+ #***********************************************************
5
+
6
+ module RWebSpec
7
+
8
+ ##
9
+ # Store test optionns
10
+ #
11
+ class Context
12
+ attr_accessor :base_url
13
+
14
+ def initialize(base_url)
15
+ set_base_url(base_url)
16
+ end
17
+
18
+ def set_base_url(baseUrl)
19
+ @base_url = baseUrl
20
+ end
21
+
22
+ end
23
+
24
+ end