webrat 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/History.txt +76 -14
  2. data/README.txt +40 -36
  3. data/Rakefile +80 -18
  4. data/TODO.txt +9 -3
  5. data/init.rb +1 -1
  6. data/lib/webrat.rb +30 -5
  7. data/lib/webrat/core.rb +12 -0
  8. data/lib/webrat/core/area.rb +44 -0
  9. data/lib/webrat/core/field.rb +332 -0
  10. data/lib/webrat/core/flunk.rb +7 -0
  11. data/lib/webrat/core/form.rb +130 -0
  12. data/lib/webrat/core/label.rb +18 -0
  13. data/lib/webrat/core/link.rb +101 -0
  14. data/lib/webrat/core/locators.rb +92 -0
  15. data/lib/webrat/core/logging.rb +25 -0
  16. data/lib/webrat/core/matchers.rb +4 -0
  17. data/lib/webrat/core/matchers/have_content.rb +94 -0
  18. data/lib/webrat/core/matchers/have_selector.rb +39 -0
  19. data/lib/webrat/core/matchers/have_tag.rb +58 -0
  20. data/lib/webrat/core/matchers/have_xpath.rb +85 -0
  21. data/lib/webrat/core/methods.rb +44 -0
  22. data/lib/webrat/core/mime.rb +29 -0
  23. data/lib/webrat/core/nokogiri.rb +42 -0
  24. data/lib/webrat/core/scope.rb +208 -0
  25. data/lib/webrat/core/select_option.rb +29 -0
  26. data/lib/webrat/core/session.rb +188 -0
  27. data/lib/webrat/core_extensions/blank.rb +58 -0
  28. data/lib/webrat/core_extensions/deprecate.rb +8 -0
  29. data/lib/webrat/core_extensions/detect_mapped.rb +12 -0
  30. data/lib/webrat/core_extensions/hash_with_indifferent_access.rb +131 -0
  31. data/lib/webrat/core_extensions/meta_class.rb +6 -0
  32. data/lib/webrat/core_extensions/nil_to_param.rb +5 -0
  33. data/lib/webrat/mechanize.rb +28 -0
  34. data/lib/webrat/merb.rb +75 -0
  35. data/lib/webrat/rack.rb +24 -0
  36. data/lib/webrat/rails.rb +102 -0
  37. data/lib/webrat/rails/redirect_actions.rb +18 -0
  38. data/lib/webrat/selenium.rb +3 -0
  39. data/lib/webrat/selenium/location_strategy_javascript/button.js +12 -0
  40. data/lib/webrat/selenium/location_strategy_javascript/label.js +16 -0
  41. data/lib/webrat/selenium/location_strategy_javascript/webrat.js +5 -0
  42. data/lib/webrat/selenium/location_strategy_javascript/webratlink.js +9 -0
  43. data/lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js +15 -0
  44. data/lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js +5 -0
  45. data/lib/webrat/selenium/selenium_extensions.js +6 -0
  46. data/lib/webrat/selenium/selenium_session.rb +137 -0
  47. data/lib/webrat/sinatra.rb +19 -0
  48. metadata +66 -52
  49. data/Manifest.txt +0 -20
  50. data/lib/webrat/rails_extensions.rb +0 -27
  51. data/lib/webrat/session.rb +0 -523
  52. data/test/checks_test.rb +0 -121
  53. data/test/chooses_test.rb +0 -74
  54. data/test/clicks_button_test.rb +0 -308
  55. data/test/clicks_link_test.rb +0 -193
  56. data/test/fills_in_test.rb +0 -139
  57. data/test/helper.rb +0 -21
  58. data/test/reloads_test.rb +0 -26
  59. data/test/selects_test.rb +0 -93
  60. data/test/visits_test.rb +0 -31
@@ -0,0 +1,18 @@
1
+ # For Rails before http://dev.rubyonrails.org/ticket/10497 was committed
2
+ module Webrat
3
+ module RedirectActions #:nodoc:
4
+
5
+ def put_via_redirect(path, parameters = {}, headers = {})
6
+ put path, parameters, headers
7
+ follow_redirect! while redirect?
8
+ status
9
+ end
10
+
11
+ def delete_via_redirect(path, parameters = {}, headers = {})
12
+ delete path, parameters, headers
13
+ follow_redirect! while redirect?
14
+ status
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ require "selenium"
2
+ require "webrat/selenium/selenium_session"
3
+
@@ -0,0 +1,12 @@
1
+ if (locator == '*') {
2
+ return selenium.browserbot.locationStrategies['xpath'].call(this, "//input[@type='submit']", inDocument, inWindow)
3
+ }
4
+ var inputs = inDocument.getElementsByTagName('input');
5
+ return $A(inputs).find(function(candidate){
6
+ inputType = candidate.getAttribute('type');
7
+ if (inputType == 'submit' || inputType == 'image') {
8
+ var buttonText = $F(candidate);
9
+ return (PatternMatcher.matches(locator, buttonText));
10
+ }
11
+ return false;
12
+ });
@@ -0,0 +1,16 @@
1
+ var allLabels = inDocument.getElementsByTagName("label");
2
+ var candidateLabels = $A(allLabels).select(function(candidateLabel){
3
+ var regExp = new RegExp('^' + locator + '\\b', 'i');
4
+ var labelText = getText(candidateLabel).strip();
5
+ return (labelText.search(regExp) >= 0);
6
+ });
7
+ if (candidateLabels.length == 0) {
8
+ return null;
9
+ }
10
+ candidateLabels = candidateLabels.sortBy(function(s) { return s.length * -1; }); //reverse length sort
11
+ var locatedLabel = candidateLabels.first();
12
+ var labelFor = locatedLabel.getAttribute('for');
13
+ if ((labelFor == null) && (locatedLabel.hasChildNodes())) {
14
+ return locatedLabel.firstChild; //TODO: should find the first form field, not just any node
15
+ }
16
+ return selenium.browserbot.locationStrategies['id'].call(this, labelFor, inDocument, inWindow);
@@ -0,0 +1,5 @@
1
+ var locationStrategies = selenium.browserbot.locationStrategies;
2
+ return locationStrategies['id'].call(this, locator, inDocument, inWindow)
3
+ || locationStrategies['name'].call(this, locator, inDocument, inWindow)
4
+ || locationStrategies['label'].call(this, locator, inDocument, inWindow)
5
+ || null;
@@ -0,0 +1,9 @@
1
+ var links = inDocument.getElementsByTagName('a');
2
+ var candidateLinks = $A(links).select(function(candidateLink) {
3
+ return PatternMatcher.matches(locator, getText(candidateLink));
4
+ });
5
+ if (candidateLinks.length == 0) {
6
+ return null;
7
+ }
8
+ candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort
9
+ return candidateLinks.first();
@@ -0,0 +1,15 @@
1
+ var locatorParts = locator.split('|');
2
+ var cssAncestor = locatorParts[0];
3
+ var linkText = locatorParts[1];
4
+ var matchingElements = cssQuery(cssAncestor, inDocument);
5
+ var candidateLinks = matchingElements.collect(function(ancestor){
6
+ var links = ancestor.getElementsByTagName('a');
7
+ return $A(links).select(function(candidateLink) {
8
+ return PatternMatcher.matches(linkText, getText(candidateLink));
9
+ });
10
+ }).flatten().compact();
11
+ if (candidateLinks.length == 0) {
12
+ return null;
13
+ }
14
+ candidateLinks = candidateLinks.sortBy(function(s) { return s.length * -1; }); //reverse length sort
15
+ return candidateLinks.first();
@@ -0,0 +1,5 @@
1
+ var optionElements = inDocument.getElementsByTagName('option');
2
+ var locatedOption = $A(optionElements).find(function(candidate){
3
+ return (PatternMatcher.matches(locator, getText(candidate)));
4
+ });
5
+ return locatedOption ? locatedOption.parentNode : null;
@@ -0,0 +1,6 @@
1
+ PatternMatcher.strategies['evalregex'] = function(regexpString) {
2
+ this.regexp = eval(regexpString);
3
+ this.matches = function(actual) {
4
+ return this.regexp.test(actual);
5
+ };
6
+ };
@@ -0,0 +1,137 @@
1
+ module Webrat
2
+ class SeleniumSession
3
+
4
+ def initialize(selenium_driver) #:nodoc:
5
+ @selenium = selenium_driver
6
+ extend_selenium
7
+ define_location_strategies
8
+ end
9
+
10
+ def visit(url)
11
+ @selenium.open(url)
12
+ end
13
+
14
+ alias_method :visits, :visit
15
+
16
+ def fill_in(field_identifier, options)
17
+ locator = "webrat=#{Regexp.escape(field_identifier)}"
18
+ @selenium.type(locator, "#{options[:with]}")
19
+ end
20
+
21
+ alias_method :fills_in, :fill_in
22
+
23
+ def response_body #:nodoc:
24
+ @selenium.get_html_source
25
+ end
26
+
27
+ def click_button(button_text_or_regexp = nil, options = {})
28
+ if button_text_or_regexp.is_a?(Hash) && options == {}
29
+ pattern, options = nil, button_text_or_regexp
30
+ else
31
+ pattern = adjust_if_regexp(button_text_or_regexp)
32
+ end
33
+ pattern ||= '*'
34
+ @selenium.click("button=#{pattern}")
35
+ wait_for_result(options[:wait])
36
+ end
37
+
38
+ alias_method :clicks_button, :click_button
39
+
40
+ def click_link(link_text_or_regexp, options = {})
41
+ pattern = adjust_if_regexp(link_text_or_regexp)
42
+ @selenium.click("webratlink=#{pattern}")
43
+ wait_for_result(options[:wait])
44
+ end
45
+
46
+ alias_method :clicks_link, :click_link
47
+
48
+ def click_link_within(selector, link_text, options = {})
49
+ @selenium.click("webratlinkwithin=#{selector}|#{link_text}")
50
+ wait_for_result(options[:wait])
51
+ end
52
+
53
+ alias_method :clicks_link_within, :click_link_within
54
+
55
+ def wait_for_result(wait_type)
56
+ if wait_type == :ajax
57
+ wait_for_ajax
58
+ elsif wait_type == :effects
59
+ wait_for_effects
60
+ else
61
+ wait_for_page_to_load
62
+ end
63
+ end
64
+
65
+ def wait_for_page_to_load(timeout = 15000)
66
+ @selenium.wait_for_page_to_load(timeout)
67
+ end
68
+
69
+ def wait_for_ajax(timeout = 15000)
70
+ @selenium.wait_for_condition "Ajax.activeRequestCount == 0", timeout
71
+ end
72
+
73
+ def wait_for_effects(timeout = 15000)
74
+ @selenium.wait_for_condition "window.Effect.Queue.size() == 0", timeout
75
+ end
76
+
77
+ def wait_for_ajax_and_effects
78
+ wait_for_ajax
79
+ wait_for_effects
80
+ end
81
+
82
+ def selects(option_text, options = {})
83
+ id_or_name_or_label = options[:from]
84
+
85
+ if id_or_name_or_label
86
+ select_locator = "webrat=#{id_or_name_or_label}"
87
+ else
88
+ select_locator = "webratselectwithoption=#{option_text}"
89
+ end
90
+ @selenium.select(select_locator, option_text)
91
+ end
92
+
93
+ def choose(label_text)
94
+ @selenium.click("webrat=#{label_text}")
95
+ end
96
+
97
+ alias_method :chooses, :choose
98
+
99
+ def check(label_text)
100
+ @selenium.check("webrat=#{label_text}")
101
+ end
102
+
103
+ alias_method :checks, :check
104
+
105
+ def is_ordered(*args) #:nodoc:
106
+ @selenium.is_ordered(*args)
107
+ end
108
+
109
+ def dragdrop(*args) #:nodoc:
110
+ @selenium.dragdrop(*args)
111
+ end
112
+
113
+ protected
114
+
115
+ def adjust_if_regexp(text_or_regexp) #:nodoc:
116
+ if text_or_regexp.is_a?(Regexp)
117
+ "evalregex:#{text_or_regexp.inspect}"
118
+ else
119
+ text_or_regexp
120
+ end
121
+ end
122
+
123
+ def extend_selenium #:nodoc:
124
+ extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js")
125
+ extenions_js = File.read(extensions_file)
126
+ @selenium.get_eval(extenions_js)
127
+ end
128
+
129
+ def define_location_strategies #:nodoc:
130
+ Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
131
+ strategy_js = File.read(file)
132
+ strategy_name = File.basename(file, '.js')
133
+ @selenium.add_location_strategy(strategy_name, strategy_js)
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,19 @@
1
+ require 'webrat/rack'
2
+ require 'sinatra'
3
+ require 'sinatra/test/methods'
4
+
5
+ module Webrat
6
+ class SinatraSession < RackSession #:nodoc:
7
+ include Sinatra::Test::Methods
8
+
9
+ %w(get head post put delete).each do |verb|
10
+ define_method(verb) do |*args| # (path, data, headers = nil)
11
+ path, data, headers = *args
12
+ params = data.merge({:env => headers || {}})
13
+ self.__send__("#{verb}_it", path, params)
14
+ follow! while @response.redirect?
15
+ end
16
+ end
17
+
18
+ end
19
+ end
metadata CHANGED
@@ -1,77 +1,98 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webrat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Helmkamp
8
- - Seth Fitzsimmons
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
11
 
13
- date: 2008-04-04 00:00:00 +01:00
12
+ date: 2008-11-07 00:00:00 -05:00
14
13
  default_executable:
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
- name: hpricot
16
+ name: nokogiri
17
+ type: :runtime
18
18
  version_requirement:
19
19
  version_requirements: !ruby/object:Gem::Requirement
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: "0.6"
23
+ version: 1.0.3
24
24
  version:
25
- - !ruby/object:Gem::Dependency
26
- name: hoe
27
- version_requirement:
28
- version_requirements: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 1.5.1
33
- version:
34
- description: Webrat lets you quickly write robust and thorough acceptance tests for a Ruby web application. By leveraging the DOM, it can run tests similarly to an in-browser testing solution without the associated performance hit (and browser dependency). The result is tests that are less fragile and more effective at verifying that the app will respond properly to users. When comparing Webrat with an in-browser testing solution like Watir or Selenium, the primary consideration should be how much JavaScript the application uses. In-browser testing is currently the only way to test JS, and that may make it a requirement for your project. If JavaScript is not central to your application, Webrat is a simpler, effective solution that will let you run your tests much faster and more frequently. (Benchmarks forthcoming.) Initial development was sponsored by EastMedia (http://www.eastmedia.com).
35
- email:
36
- - bryan@brynary.com
37
- - seth@mojodna.net
25
+ description: Webrat. Ruby Acceptance Testing for Web applications
26
+ email: bryan@brynary.com
38
27
  executables: []
39
28
 
40
29
  extensions: []
41
30
 
42
31
  extra_rdoc_files:
43
- - History.txt
44
- - MIT-LICENSE.txt
45
- - Manifest.txt
46
32
  - README.txt
47
- - TODO.txt
33
+ - MIT-LICENSE.txt
48
34
  files:
49
35
  - History.txt
36
+ - init.rb
37
+ - install.rb
50
38
  - MIT-LICENSE.txt
51
- - Manifest.txt
52
39
  - README.txt
53
40
  - Rakefile
54
41
  - TODO.txt
55
- - init.rb
56
- - install.rb
42
+ - lib/webrat
43
+ - lib/webrat/core
44
+ - lib/webrat/core/area.rb
45
+ - lib/webrat/core/field.rb
46
+ - lib/webrat/core/flunk.rb
47
+ - lib/webrat/core/form.rb
48
+ - lib/webrat/core/label.rb
49
+ - lib/webrat/core/link.rb
50
+ - lib/webrat/core/locators.rb
51
+ - lib/webrat/core/logging.rb
52
+ - lib/webrat/core/matchers
53
+ - lib/webrat/core/matchers/have_content.rb
54
+ - lib/webrat/core/matchers/have_selector.rb
55
+ - lib/webrat/core/matchers/have_tag.rb
56
+ - lib/webrat/core/matchers/have_xpath.rb
57
+ - lib/webrat/core/matchers.rb
58
+ - lib/webrat/core/methods.rb
59
+ - lib/webrat/core/mime.rb
60
+ - lib/webrat/core/nokogiri.rb
61
+ - lib/webrat/core/scope.rb
62
+ - lib/webrat/core/select_option.rb
63
+ - lib/webrat/core/session.rb
64
+ - lib/webrat/core.rb
65
+ - lib/webrat/core_extensions
66
+ - lib/webrat/core_extensions/blank.rb
67
+ - lib/webrat/core_extensions/deprecate.rb
68
+ - lib/webrat/core_extensions/detect_mapped.rb
69
+ - lib/webrat/core_extensions/hash_with_indifferent_access.rb
70
+ - lib/webrat/core_extensions/meta_class.rb
71
+ - lib/webrat/core_extensions/nil_to_param.rb
72
+ - lib/webrat/mechanize.rb
73
+ - lib/webrat/merb.rb
74
+ - lib/webrat/rack.rb
75
+ - lib/webrat/rails
76
+ - lib/webrat/rails/redirect_actions.rb
77
+ - lib/webrat/rails.rb
78
+ - lib/webrat/selenium
79
+ - lib/webrat/selenium/location_strategy_javascript
80
+ - lib/webrat/selenium/location_strategy_javascript/button.js
81
+ - lib/webrat/selenium/location_strategy_javascript/label.js
82
+ - lib/webrat/selenium/location_strategy_javascript/webrat.js
83
+ - lib/webrat/selenium/location_strategy_javascript/webratlink.js
84
+ - lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js
85
+ - lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js
86
+ - lib/webrat/selenium/selenium_extensions.js
87
+ - lib/webrat/selenium/selenium_session.rb
88
+ - lib/webrat/selenium.rb
89
+ - lib/webrat/sinatra.rb
57
90
  - lib/webrat.rb
58
- - lib/webrat/rails_extensions.rb
59
- - lib/webrat/session.rb
60
- - test/checks_test.rb
61
- - test/chooses_test.rb
62
- - test/clicks_button_test.rb
63
- - test/clicks_link_test.rb
64
- - test/fills_in_test.rb
65
- - test/helper.rb
66
- - test/reloads_test.rb
67
- - test/selects_test.rb
68
- - test/visits_test.rb
69
91
  has_rdoc: true
70
- homepage: http://rubyforge.org/projects/webrat
92
+ homepage: http://github.com/brynary/webrat
71
93
  post_install_message:
72
- rdoc_options:
73
- - --main
74
- - README.txt
94
+ rdoc_options: []
95
+
75
96
  require_paths:
76
97
  - lib
77
98
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -88,17 +109,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
109
  version:
89
110
  requirements: []
90
111
 
91
- rubyforge_project: webrat
92
- rubygems_version: 1.0.1
112
+ rubyforge_project:
113
+ rubygems_version: 1.3.0
93
114
  signing_key:
94
115
  specification_version: 2
95
- summary: Ruby Acceptance Testing for Web applications
96
- test_files:
97
- - test/checks_test.rb
98
- - test/chooses_test.rb
99
- - test/clicks_button_test.rb
100
- - test/clicks_link_test.rb
101
- - test/fills_in_test.rb
102
- - test/reloads_test.rb
103
- - test/selects_test.rb
104
- - test/visits_test.rb
116
+ summary: Webrat. Ruby Acceptance Testing for Web applications
117
+ test_files: []
118
+
data/Manifest.txt DELETED
@@ -1,20 +0,0 @@
1
- History.txt
2
- MIT-LICENSE.txt
3
- Manifest.txt
4
- README.txt
5
- Rakefile
6
- TODO.txt
7
- init.rb
8
- install.rb
9
- lib/webrat.rb
10
- lib/webrat/rails_extensions.rb
11
- lib/webrat/session.rb
12
- test/checks_test.rb
13
- test/chooses_test.rb
14
- test/clicks_button_test.rb
15
- test/clicks_link_test.rb
16
- test/fills_in_test.rb
17
- test/helper.rb
18
- test/reloads_test.rb
19
- test/selects_test.rb
20
- test/visits_test.rb