rwebspec-webdriver 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/Rakefile +11 -12
  2. data/lib/rwebspec-webdriver/assert.rb +445 -0
  3. data/lib/{rwebspec → rwebspec-webdriver}/clickJSDialog.rb +0 -0
  4. data/lib/{rwebspec → rwebspec-webdriver}/context.rb +13 -11
  5. data/lib/rwebspec-webdriver/database_checker.rb +76 -0
  6. data/lib/rwebspec-webdriver/driver.rb +1011 -0
  7. data/lib/rwebspec-webdriver/element_locator.rb +89 -0
  8. data/lib/rwebspec-webdriver/load_test_helper.rb +174 -0
  9. data/lib/{rwebspec → rwebspec-webdriver}/matchers/contains_text.rb +0 -0
  10. data/lib/rwebspec-webdriver/popup.rb +149 -0
  11. data/lib/rwebspec-webdriver/rspec_helper.rb +101 -0
  12. data/lib/rwebspec-webdriver/test_script.rb +10 -0
  13. data/lib/rwebspec-webdriver/test_utils.rb +219 -0
  14. data/lib/rwebspec-webdriver/testwise_plugin.rb +85 -0
  15. data/lib/rwebspec-webdriver/using_pages.rb +51 -0
  16. data/lib/rwebspec-webdriver/web_browser.rb +744 -0
  17. data/lib/rwebspec-webdriver/web_page.rb +110 -0
  18. data/lib/rwebspec-webdriver/web_testcase.rb +40 -0
  19. data/lib/rwebspec-webdriver.rb +12 -12
  20. metadata +22 -22
  21. data/lib/rwebspec/assert.rb +0 -443
  22. data/lib/rwebspec/database_checker.rb +0 -74
  23. data/lib/rwebspec/driver.rb +0 -1009
  24. data/lib/rwebspec/element_locator.rb +0 -87
  25. data/lib/rwebspec/load_test_helper.rb +0 -172
  26. data/lib/rwebspec/popup.rb +0 -147
  27. data/lib/rwebspec/rspec_helper.rb +0 -99
  28. data/lib/rwebspec/test_script.rb +0 -8
  29. data/lib/rwebspec/test_utils.rb +0 -217
  30. data/lib/rwebspec/testwise_plugin.rb +0 -83
  31. data/lib/rwebspec/using_pages.rb +0 -49
  32. data/lib/rwebspec/web_browser.rb +0 -742
  33. data/lib/rwebspec/web_page.rb +0 -108
  34. data/lib/rwebspec/web_testcase.rb +0 -38
data/Rakefile CHANGED
@@ -7,7 +7,6 @@ gem 'darkfish-rdoc'
7
7
  # require 'darkfish-rdoc'
8
8
 
9
9
  $:.unshift(File.dirname(__FILE__) + "/lib")
10
- #require 'rwebspec'
11
10
 
12
11
  desc "Default task"
13
12
  task :default => [ :clean, :spec, :rdoc, :chm, :gem]
@@ -36,17 +35,17 @@ end
36
35
  # rdoc.template = "#{ENV['template']}.rb" if ENV['template']
37
36
  # rdoc.rdoc_files.include('README')
38
37
  # rdoc.rdoc_files.include('lib/rwebspec.rb')
39
- # rdoc.rdoc_files.include('lib/rwebspec/*.rb')
38
+ # rdoc.rdoc_files.include('lib/rwebspec-webdriver/*.rb')
40
39
  # }
41
40
 
42
41
  # using DarkFish - http://deveiate.org/projects/Darkfish-Rdoc/
43
42
  Rake::RDocTask.new do |rdoc|
44
43
  rdoc.rdoc_dir = 'doc'
45
44
  rdoc.title = 'RWebSpec'
46
- rdoc.rdoc_files.include('lib/rwebspec.rb')
47
- rdoc.rdoc_files.include('lib/rwebspec/*.rb')
48
- rdoc.rdoc_files.delete("lib/rwebspec/web_testcase.rb")
49
- rdoc.rdoc_files.delete("lib/rwebspec/checkJSDialog.rb")
45
+ rdoc.rdoc_files.include('lib/rwebspec-webdriver.rb')
46
+ rdoc.rdoc_files.include('lib/rwebspec-webdriver/*.rb')
47
+ rdoc.rdoc_files.delete("lib/rwebspec-webdriver/web_testcase.rb")
48
+ rdoc.rdoc_files.delete("lib/rwebspec-webdriver/checkJSDialog.rb")
50
49
  rdoc.options += [
51
50
  '-SHN',
52
51
  '-f', 'darkfish', # This is the important bit
@@ -56,10 +55,10 @@ end
56
55
  Rake::RDocTask.new("chm") do |rdoc|
57
56
  rdoc.rdoc_dir = 'chm'
58
57
  rdoc.title = 'RWebSpec'
59
- rdoc.rdoc_files.include('lib/rwebspec.rb')
60
- rdoc.rdoc_files.include('lib/rwebspec/*.rb')
61
- rdoc.rdoc_files.delete("lib/rwebspec/web_testcase.rb")
62
- rdoc.rdoc_files.delete("lib/rwebspec/checkJSDialog.rb")
58
+ rdoc.rdoc_files.include('lib/rwebspec-webdriver.rb')
59
+ rdoc.rdoc_files.include('lib/rwebspec-webdriver/*.rb')
60
+ rdoc.rdoc_files.delete("lib/rwebspec-webdriver/web_testcase.rb")
61
+ rdoc.rdoc_files.delete("lib/rwebspec-webdriver/checkJSDialog.rb")
63
62
  rdoc.options += [
64
63
  '-SHN',
65
64
  '-f', 'chm', # This is the important bit
@@ -70,14 +69,14 @@ end
70
69
  spec = Gem::Specification.new do |s|
71
70
  s.platform= Gem::Platform::RUBY
72
71
  s.name = "rwebspec-webdriver"
73
- s.version = "0.1.1"
72
+ s.version = "0.1.2"
74
73
  s.summary = "Executable functional specification for web applications in RSpec syntax and Selenium-WebDriver"
75
74
  # s.description = ""
76
75
 
77
76
  s.author = "Zhimin Zhan"
78
77
  s.email = "zhimin@agileway.net"
79
78
  s.homepage= "http://github.com/zhimin/rwebspec-webdriver/tree/master"
80
- s.rubyforge_project = "rwebspec-webdrive"
79
+ s.rubyforge_project = "rwebspec-webdriver"
81
80
 
82
81
  s.has_rdoc = true
83
82
  s.requirements << 'none'
@@ -0,0 +1,445 @@
1
+ require 'test/unit/assertions'
2
+
3
+ # http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/SearchContext.html#find_element-instance_method
4
+
5
+ module RWebSpec
6
+ module WebDriver
7
+ module Assert
8
+ include Test::Unit::Assertions
9
+
10
+ def assert_not(condition, msg = "")
11
+ perform_assertion { assert(!condition, msg) }
12
+ end
13
+
14
+ def assert_nil(actual, msg="")
15
+ perform_assertion { assert(actual.nil?, msg) }
16
+ end
17
+
18
+ def assert_not_nil(actual, msg="")
19
+ perform_assertion { assert(!actual.nil?, msg) }
20
+ end
21
+
22
+ def fail(message)
23
+ perform_assertion { assert(false, message) }
24
+ end
25
+
26
+ # assertions
27
+ def assert_title_equals(title)
28
+ assert_equals(title, @web_browser.page_title)
29
+ end
30
+
31
+ alias assert_title assert_title_equals
32
+
33
+ # Assert text present in page source (html)
34
+ # assert_text_in_page_source("<b>iTest2</b> Cool") # <b>iTest2</b> Cool
35
+ def assert_text_in_page_source(text)
36
+ perform_assertion { assert((@web_browser.page_source.include? text), 'expected html: ' + text + ' not found') }
37
+ end
38
+
39
+ # Assert text not present in page source (html)
40
+ # assert_text_not_in_page_source("<b>iTest2</b> Cool") # <b>iTest2</b> Cool
41
+ def assert_text_not_in_page_source(text)
42
+ perform_assertion { assert(!(@web_browser.page_source.include? text), 'expected html: ' + text + ' found') }
43
+ end
44
+
45
+ # Assert text present in page source (html)
46
+ # assert_text_present("iTest2 Cool") # <b>iTest2</b> Cool
47
+ def assert_text_present(text)
48
+ perform_assertion { assert((@web_browser.text.include? text), 'expected text: ' + text + ' not found') }
49
+ end
50
+
51
+ # Assert text not present in page source (html)
52
+ # assert_text_not_present("iTest2 Cool") # <b>iTest2</b> Cool
53
+ def assert_text_not_present(text)
54
+ perform_assertion { assert(!(@web_browser.text.include? text), 'expected text: ' + text + ' found') }
55
+ end
56
+
57
+
58
+ ##
59
+ # Link
60
+ # @NOTE: this is different from Watir version
61
+
62
+ # Assert a link containing specified text in the page
63
+ #
64
+ # <a href="">Click Me</a>
65
+ # assert_link_present_with_text("Click ") # =>
66
+ #
67
+ def assert_link_present_with_text(link_text, opts = {})
68
+ begin
69
+ if opts && opts[:partial]
70
+ elem = @web_browser.find_element(:partial_link_text, link_text)
71
+ else
72
+ elem = @web_browser.find_element(:link_text, link_text)
73
+ end
74
+ rescue => e
75
+ puts "Failed to find the link text :#{e}"
76
+ end
77
+
78
+ if elem
79
+ return true
80
+ else
81
+ fail("can't find the link with text: #{link_text}")
82
+ end
83
+ end
84
+
85
+ def assert_link_not_present_with_text(link_text, opts = {})
86
+ begin
87
+ if opts && opts[:partial]
88
+ elem = @web_browser.find_element(:partial_link_text, link_text)
89
+ else
90
+ elem = @web_browser.find_element(:link_text, link_text)
91
+ end
92
+ rescue => e
93
+ puts "Failed to find the link text :#{e}"
94
+ end
95
+ if elem
96
+ fail("unexpected link (exact): #{link_text} found")
97
+ else
98
+ return true
99
+ end
100
+ end
101
+
102
+
103
+ ##
104
+ # Checkbox
105
+ def assert_checkbox_not_selected(checkbox_name)
106
+ elem = find_element(:name, checkbox_name)
107
+ fail "Checkbox with name:#{checkbox_name} not found" unless elem.tag_name == "input"
108
+ assert !elem.selected?
109
+ end
110
+
111
+ alias assert_checkbox_not_checked assert_checkbox_not_selected
112
+
113
+ def assert_checkbox_selected(checkbox_name)
114
+ elem = find_element(:name, checkbox_name)
115
+ fail "Checkbox with name:#{checkbox_name} not found" unless elem.tag_name == "input"
116
+ assert elem.selected?
117
+ end
118
+
119
+ alias assert_checkbox_checked assert_checkbox_selected
120
+
121
+ ##
122
+ # select
123
+ def assert_option_value_not_present(select_name, option_value)
124
+ select_element = find_element(:name, select_name)
125
+ options = select_element.find_elements(:xpath, ".//option")
126
+ option_values = options.collect { |x| x.attribute('value') }
127
+ if option_values.include?(option_value)
128
+ fail "Unexpected select option value #{option_value}"
129
+ else
130
+ return true
131
+ end
132
+ end
133
+
134
+ alias assert_select_value_not_present assert_option_value_not_present
135
+
136
+ def assert_option_not_present(select_name, option_label)
137
+ select_element = find_element(:name, select_name)
138
+ options = select_element.find_elements(:xpath, ".//option")
139
+ option_labels= options.collect { |x| x.attribute('value') }
140
+ if option_labels.include?(option_label)
141
+ fail "Unexpected select option label #{option_label}"
142
+ else
143
+ return true
144
+ end
145
+ end
146
+
147
+ alias assert_select_label_not_present assert_option_not_present
148
+
149
+ def assert_option_value_present(select_name, option_value)
150
+ select_element = find_element(:name, select_name)
151
+ options = select_element.find_elements(:xpath, ".//option")
152
+ option_values = options.collect { |x| x.attribute('value') }
153
+ if option_values.include?(option_value)
154
+ return true
155
+ else
156
+ fail "Unexpected select option value #{option_value}"
157
+ end
158
+ end
159
+
160
+ alias assert_select_value_present assert_option_value_present
161
+ alias assert_menu_value_present assert_option_value_present
162
+
163
+ def assert_option_present(select_name, option_label)
164
+ select_element = find_element(:name, select_name)
165
+ options = select_element.find_elements(:xpath, ".//option")
166
+ option_labels = options.collect { |x| x.text }
167
+ if option_labels.include?(option_label)
168
+ return true
169
+ else
170
+ fail "Unexpected select option label #{option_label}"
171
+ end
172
+ end
173
+
174
+ alias assert_select_label_present assert_option_present
175
+ alias assert_menu_label_present assert_option_present
176
+
177
+ def assert_option_equals(select_name, option_label)
178
+ select_element = find_element(:name, select_name)
179
+ options = select_element.find_elements(:xpath, ".//option")
180
+ selected_label_list = []
181
+ options.each do |op|
182
+ selected_label_list << op.text if op.selected?
183
+ end
184
+ assert selected_label_list.join(",").to_s == option_label
185
+ end
186
+
187
+ alias assert_select_label assert_option_equals
188
+ alias assert_menu_label assert_option_equals
189
+
190
+ def assert_option_value_equals(select_name, option_value)
191
+ select_element = find_element(:name, select_name)
192
+ options = select_element.find_elements(:xpath, ".//option")
193
+ selected_value_list = []
194
+ options.each do |op|
195
+ selected_value_list << op.attribute('value') if op.selected?
196
+ end
197
+ assert selected_value_list.join(",").to_s == option_value
198
+ end
199
+
200
+ alias assert_select_value assert_option_value_equals
201
+ alias assert_menu_value assert_option_value_equals
202
+
203
+ ##
204
+ # radio
205
+
206
+ # radio_group is the name field, radio options 'value' field
207
+ def assert_radio_option_not_present(radio_group, radio_option)
208
+ begin
209
+ elem = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
210
+ rescue => e
211
+ end
212
+
213
+ if elem
214
+ fail("unexpected radio option: " + radio_option + " found")
215
+ else
216
+ return true
217
+ end
218
+ end
219
+
220
+ def assert_radio_option_present(radio_group, radio_option)
221
+ elem = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
222
+ if elem
223
+ return true
224
+ else
225
+ fail("can't find the radio option : '#{radio_option}'")
226
+ end
227
+ end
228
+
229
+ def assert_radio_option_selected(radio_group, radio_option)
230
+ elem = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
231
+ assert elem.selected?
232
+ end
233
+
234
+ alias assert_radio_button_checked assert_radio_option_selected
235
+ alias assert_radio_option_checked assert_radio_option_selected
236
+
237
+ def assert_radio_option_not_selected(radio_group, radio_option)
238
+ elem = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
239
+ assert !elem.selected?
240
+ end
241
+
242
+ alias assert_radio_button_not_checked assert_radio_option_not_selected
243
+ alias assert_radio_option_not_checked assert_radio_option_not_selected
244
+
245
+ ##
246
+ # Button
247
+
248
+ def assert_button_present(button_id)
249
+ find_element(:id, button_id)
250
+ end
251
+
252
+ def assert_button_not_present(button_id)
253
+ elem = safe_find_element_by_id(button_id)
254
+ if elem && elem.tag_name == "button" then
255
+ fail("Unexpected button with this id: #{button_id}")
256
+ end
257
+ return true
258
+ end
259
+
260
+ def assert_button_present_with_text(button_text)
261
+ input_buttons = find_elements(:xpath, "//input[@type = 'button' or @type = 'submit']")
262
+ input_button_values = input_buttons.collect { |x| x.attribute('value') }
263
+ if input_button_values.include?(button_text)
264
+ return true
265
+ else
266
+ #TODO check for just <button tag
267
+ fail("can't find the button with text: #{button_text}")
268
+ end
269
+ end
270
+
271
+ def assert_button_not_present_with_text(button_text)
272
+ input_buttons = find_elements(:xpath, "//input[@type = 'button' or @type = 'submit']")
273
+ input_button_values = input_buttons.collect { |x| x.attribute('value') }
274
+ if input_button_values.include?(button_text)
275
+ fail("can't find the button with text: #{button_text}")
276
+ else
277
+ #TODO check for just <button tag
278
+ return true
279
+ end
280
+ end
281
+
282
+ ## General
283
+ #
284
+
285
+ def assert_equals(expected, actual, msg=nil)
286
+ perform_assertion { assert(expected == actual, (msg.nil?) ? "Expected: #{expected} diff from actual: #{actual}" : msg) }
287
+ end
288
+
289
+
290
+ # Check a HTML element exists or not
291
+ # Example:
292
+ # assert_exists("label", "receipt_date")
293
+ # assert_exists(:span, :receipt_date)
294
+ def assert_exists(tag, element_id)
295
+ elem = find_element(:id, element_id)
296
+ if elem
297
+ return true
298
+ else
299
+ fail("Element '#{tag}' with id: '#{element_id}' not found")
300
+ end
301
+ end
302
+
303
+ alias assert_exists? assert_exists
304
+ alias assert_element_exists assert_exists
305
+
306
+ def assert_not_exists(tag, element_id)
307
+ elem = find_element(:id, element_id)
308
+ if elem
309
+ fail("#{tag} with #{element_id} not expected there, but exists")
310
+ else
311
+ return true
312
+ end
313
+ end
314
+
315
+ alias assert_not_exists? assert_not_exists
316
+ alias assert_element_not_exists? assert_not_exists
317
+
318
+
319
+ # Assert tag with element id is visible?, eg.
320
+ # assert_visible(:div, "public_notice")
321
+ # assert_visible(:span, "public_span")
322
+ def assert_visible(tag, element_id)
323
+ element = find_element(:id, element_id)
324
+ perform_assertion { assert(element.displayed?, "Element '#{tag}' with id: '#{element_id}' not visible") }
325
+ end
326
+
327
+ # Assert tag with element id is hidden?, example
328
+ # assert_hidden(:div, "secret")
329
+ # assert_hidden(:span, "secret_span")
330
+ def assert_hidden(tag, element_id)
331
+ element = find_element(:id, element_id)
332
+ perform_assertion { assert(!element.displayed?, "Element '#{tag}' with id: '#{element_id}' is visible") }
333
+ end
334
+
335
+ alias assert_not_visible assert_hidden
336
+
337
+ def assert_disabled(tag, element_id)
338
+ element = find_element(:id, element_id)
339
+ assert !element.enabled?
340
+ end
341
+
342
+ def assert_enabled(tag, element_id)
343
+ element = find_element(:id, element_id)
344
+ puts "XXX :#{element.tag_name}"
345
+ assert element.enabled?
346
+ end
347
+
348
+
349
+ # Assert given text appear inside a table (inside <table> tag like below)
350
+ #
351
+ # <table id="t1">
352
+ #
353
+ # <tbody>
354
+ # <tr id="row_1">
355
+ # <td id="cell_1_1">A</td>
356
+ # <td id="cell_1_2">B</td>
357
+ # </tr>
358
+ # <tr id="row_2">
359
+ # <td id="cell_2_1">a</td>
360
+ # <td id="cell_2_2">b</td>
361
+ # </tr>
362
+ # </tbody>
363
+ #
364
+ # </table>
365
+ #
366
+ # The plain text view of above table
367
+ # A B a b
368
+ #
369
+ # Examples
370
+ # assert_text_present_in_table("t1", ">A<") # => true
371
+ # assert_text_present_in_table("t1", ">A<", :just_plain_text => true) # => false
372
+ def assert_text_present_in_table(table_id, text, options = {:just_plain_text => false})
373
+ perform_assertion { assert(table_source(table_id, options).include?(text), "the text #{text} not found in table #{table_id}") }
374
+ end
375
+
376
+ alias assert_text_in_table assert_text_present_in_table
377
+
378
+ def assert_text_not_present_in_table(table_id, text, options = {:just_plain_text => false})
379
+ perform_assertion { assert_not(table_source(table_id, options).include?(text), "the text #{text} not found in table #{table_id}") }
380
+ end
381
+
382
+ alias assert_text_not_in_table assert_text_not_present_in_table
383
+
384
+ # Assert a text field (with given name) has the value
385
+ #
386
+ # <input id="tid" name="text1" value="text already there" type="text">
387
+ #
388
+ # assert_text_field_value("text1", "text already there") => true
389
+ #
390
+ def assert_text_field_value(textfield_name, text)
391
+ perform_assertion { assert_equal(text, text_field(:name, textfield_name).attribute('value') ) }
392
+ end
393
+
394
+
395
+ #-- Not tested
396
+ # -----
397
+
398
+ def assert_text_in_element(element_id, text)
399
+ elem = element_by_id(element_id)
400
+ assert_not_nil(elem.innerText, "element #{element_id} has no text")
401
+ perform_assertion { assert(elem.innerText.include?(text), "the text #{text} not found in element #{element_id}") }
402
+ end
403
+
404
+ # Use
405
+ #
406
+
407
+ #TODO for drag-n-drop, check the postion in list
408
+ # def assert_position_in_list(list_element_id)
409
+ # raise "not implemented"
410
+ # end
411
+
412
+ private
413
+ def table_source(table_id, options)
414
+ elem_table = table(:id, table_id.to_s)
415
+ elem_table_text = elem_table.text
416
+ elem_table_html = is_firefox? ? elem_table.innerHTML : elem_table.html
417
+ table_source = options[:just_plain_text] ? elem_table_text : elem_table_html
418
+ end
419
+
420
+
421
+ def perform_assertion(&block)
422
+ begin
423
+ yield
424
+ return true
425
+ rescue StandardError => e
426
+ # puts "[DEBUG] Assertion error: #{e}"
427
+ take_screenshot
428
+ raise e
429
+ end
430
+ end
431
+
432
+ #
433
+ def safe_find_element_by_id(an_id)
434
+ begin
435
+ find_element(:id, an_id)
436
+ rescue => e
437
+ puts "Not found, but not throwing an error"
438
+ nil
439
+ end
440
+ end
441
+
442
+ end
443
+
444
+ end
445
+ end