aslakhellesoy-webrat 0.3.2.2 → 0.4.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/History.txt +158 -19
  2. data/Rakefile +36 -11
  3. data/lib/webrat.rb +8 -11
  4. data/lib/webrat/core/configuration.rb +67 -25
  5. data/lib/webrat/core/elements/area.rb +7 -7
  6. data/lib/webrat/core/elements/element.rb +11 -11
  7. data/lib/webrat/core/elements/field.rb +70 -51
  8. data/lib/webrat/core/elements/form.rb +17 -17
  9. data/lib/webrat/core/elements/label.rb +8 -8
  10. data/lib/webrat/core/elements/link.rb +13 -11
  11. data/lib/webrat/core/elements/select_option.rb +9 -9
  12. data/lib/webrat/core/locators.rb +2 -2
  13. data/lib/webrat/core/locators/area_locator.rb +10 -10
  14. data/lib/webrat/core/locators/button_locator.rb +13 -13
  15. data/lib/webrat/core/locators/field_by_id_locator.rb +8 -8
  16. data/lib/webrat/core/locators/field_labeled_locator.rb +19 -13
  17. data/lib/webrat/core/locators/field_locator.rb +7 -7
  18. data/lib/webrat/core/locators/field_named_locator.rb +10 -10
  19. data/lib/webrat/core/locators/form_locator.rb +6 -6
  20. data/lib/webrat/core/locators/label_locator.rb +9 -9
  21. data/lib/webrat/core/locators/link_locator.rb +12 -12
  22. data/lib/webrat/core/locators/locator.rb +5 -5
  23. data/lib/webrat/core/locators/select_option_locator.rb +11 -11
  24. data/lib/webrat/core/logging.rb +7 -4
  25. data/lib/webrat/core/matchers/have_content.rb +19 -15
  26. data/lib/webrat/core/matchers/have_selector.rb +44 -22
  27. data/lib/webrat/core/matchers/have_tag.rb +11 -61
  28. data/lib/webrat/core/matchers/have_xpath.rb +89 -35
  29. data/lib/webrat/core/methods.rb +13 -11
  30. data/lib/webrat/core/mime.rb +3 -3
  31. data/lib/webrat/core/save_and_open_page.rb +9 -9
  32. data/lib/webrat/core/scope.rb +82 -62
  33. data/lib/webrat/core/session.rb +61 -18
  34. data/lib/webrat/core/xml.rb +16 -16
  35. data/lib/webrat/core/xml/hpricot.rb +3 -3
  36. data/lib/webrat/core/xml/nokogiri.rb +14 -14
  37. data/lib/webrat/core/xml/rexml.rb +3 -3
  38. data/lib/webrat/core_extensions/blank.rb +1 -1
  39. data/lib/webrat/core_extensions/deprecate.rb +1 -1
  40. data/lib/webrat/core_extensions/detect_mapped.rb +4 -4
  41. data/lib/webrat/core_extensions/meta_class.rb +1 -1
  42. data/lib/webrat/core_extensions/tcp_socket.rb +27 -0
  43. data/lib/webrat/mechanize.rb +9 -9
  44. data/lib/webrat/merb.rb +5 -61
  45. data/lib/webrat/merb_session.rb +67 -0
  46. data/lib/webrat/rack.rb +45 -14
  47. data/lib/webrat/rails.rb +31 -11
  48. data/lib/webrat/rspec-rails.rb +2 -2
  49. data/lib/webrat/selenium.rb +27 -39
  50. data/lib/webrat/selenium/application_server_factory.rb +40 -0
  51. data/lib/webrat/selenium/application_servers.rb +5 -0
  52. data/lib/webrat/selenium/application_servers/base.rb +44 -0
  53. data/lib/webrat/selenium/application_servers/external.rb +24 -0
  54. data/lib/webrat/selenium/application_servers/merb.rb +48 -0
  55. data/lib/webrat/selenium/application_servers/rails.rb +42 -0
  56. data/lib/webrat/selenium/application_servers/sinatra.rb +35 -0
  57. data/lib/webrat/selenium/location_strategy_javascript/button.js +14 -7
  58. data/lib/webrat/selenium/location_strategy_javascript/label.js +1 -2
  59. data/lib/webrat/selenium/location_strategy_javascript/webratlink.js +4 -1
  60. data/lib/webrat/selenium/matchers.rb +4 -108
  61. data/lib/webrat/selenium/matchers/have_content.rb +66 -0
  62. data/lib/webrat/selenium/matchers/have_selector.rb +49 -0
  63. data/lib/webrat/selenium/matchers/have_tag.rb +72 -0
  64. data/lib/webrat/selenium/matchers/have_xpath.rb +45 -0
  65. data/lib/webrat/selenium/selenium_rc_server.rb +84 -0
  66. data/lib/webrat/selenium/selenium_session.rb +86 -68
  67. data/lib/webrat/selenium/silence_stream.rb +18 -0
  68. metadata +21 -16
  69. data/lib/webrat/core_extensions/hash_with_indifferent_access.rb +0 -131
  70. data/lib/webrat/sinatra.rb +0 -29
@@ -0,0 +1,40 @@
1
+ module Webrat
2
+ module Selenium
3
+
4
+ class ApplicationServerFactory
5
+
6
+ def self.app_server_instance
7
+ case Webrat.configuration.application_framework
8
+ when :sinatra
9
+ require "webrat/selenium/application_servers/sinatra"
10
+ return Webrat::Selenium::ApplicationServers::Sinatra.new
11
+ when :merb
12
+ require "webrat/selenium/application_servers/merb"
13
+ return Webrat::Selenium::ApplicationServers::Merb.new
14
+ when :rails
15
+ require "webrat/selenium/application_servers/rails"
16
+ return Webrat::Selenium::ApplicationServers::Rails.new
17
+ when :external
18
+ require "webrat/selenium/application_servers/external"
19
+ return Webrat::Selenium::ApplicationServers::External.new
20
+ else
21
+ raise WebratError.new(<<-STR)
22
+ Unknown Webrat application_framework: #{Webrat.configuration.application_framework.inspect}
23
+
24
+ Please ensure you have a Webrat configuration block that specifies an application_framework
25
+ in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).
26
+
27
+ For example:
28
+
29
+ Webrat.configure do |config|
30
+ # ...
31
+ config.application_framework = :rails
32
+ end
33
+ STR
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ require "webrat/selenium/application_servers/base"
2
+ require "webrat/selenium/application_servers/sinatra"
3
+ require "webrat/selenium/application_servers/merb"
4
+ require "webrat/selenium/application_servers/rails"
5
+ require "webrat/selenium/application_servers/external"
@@ -0,0 +1,44 @@
1
+ module Webrat
2
+ module Selenium
3
+ module ApplicationServers
4
+ class Base
5
+ include Webrat::Selenium::SilenceStream
6
+
7
+ def boot
8
+ start
9
+ wait
10
+ stop_at_exit
11
+ end
12
+
13
+ def stop_at_exit
14
+ at_exit do
15
+ stop
16
+ end
17
+ end
18
+
19
+ def wait
20
+ $stderr.print "==> Waiting for #{Webrat.configuration.application_framework} application server on port #{Webrat.configuration.application_port}... "
21
+ wait_for_socket
22
+ $stderr.print "Ready!\n"
23
+ end
24
+
25
+ def wait_for_socket
26
+ silence_stream(STDOUT) do
27
+ TCPSocket.wait_for_service_with_timeout \
28
+ :host => Webrat.configuration.application_address,
29
+ :port => Webrat.configuration.application_port.to_i,
30
+ :timeout => 30 # seconds
31
+ end
32
+ rescue SocketError
33
+ fail
34
+ end
35
+
36
+ def prepare_pid_file(file_path, pid_file_name)
37
+ FileUtils.mkdir_p File.expand_path(file_path)
38
+ File.expand_path("#{file_path}/#{pid_file_name}")
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,24 @@
1
+ module Webrat
2
+ module Selenium
3
+ module ApplicationServers
4
+ class External < Webrat::Selenium::ApplicationServers::Base
5
+ def start
6
+ warn "Webrat Ignoring Start Of Application Server Due to External Mode"
7
+ end
8
+
9
+ def stop
10
+ end
11
+
12
+ def fail
13
+ end
14
+
15
+ def pid_file
16
+ end
17
+
18
+ def wait
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,48 @@
1
+ module Webrat
2
+ module Selenium
3
+ module ApplicationServers
4
+ class Merb < Webrat::Selenium::ApplicationServers::Base
5
+
6
+ def start
7
+ system start_command
8
+ end
9
+
10
+ def stop
11
+ silence_stream(STDOUT) do
12
+ pid = File.read(pid_file)
13
+ system("kill -9 #{pid}")
14
+ FileUtils.rm_f pid_file
15
+ end
16
+ end
17
+
18
+ def fail
19
+ $stderr.puts
20
+ $stderr.puts
21
+ $stderr.puts "==> Failed to boot the Merb application server... exiting!"
22
+ $stderr.puts
23
+ $stderr.puts "Verify you can start a Merb server on port #{Webrat.configuration.application_port} with the following command:"
24
+ $stderr.puts
25
+ $stderr.puts " #{start_command}"
26
+ exit
27
+ end
28
+
29
+ def pid_file
30
+ "log/merb.#{Webrat.configuration.application_port}.pid"
31
+ end
32
+
33
+ def start_command
34
+ "#{merb_command} -d -p #{Webrat.configuration.application_port} -e #{Webrat.configuration.application_environment}"
35
+ end
36
+
37
+ def merb_command
38
+ if File.exist?('bin/merb')
39
+ merb_cmd = 'bin/merb'
40
+ else
41
+ merb_cmd = 'merb'
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,42 @@
1
+ module Webrat
2
+ module Selenium
3
+ module ApplicationServers
4
+ class Rails < Webrat::Selenium::ApplicationServers::Base
5
+
6
+ def start
7
+ system start_command
8
+ end
9
+
10
+ def stop
11
+ silence_stream(STDOUT) do
12
+ system stop_command
13
+ end
14
+ end
15
+
16
+ def fail
17
+ $stderr.puts
18
+ $stderr.puts
19
+ $stderr.puts "==> Failed to boot the Rails application server... exiting!"
20
+ $stderr.puts
21
+ $stderr.puts "Verify you can start a Rails server on port #{Webrat.configuration.application_port} with the following command:"
22
+ $stderr.puts
23
+ $stderr.puts " #{start_command}"
24
+ exit
25
+ end
26
+
27
+ def pid_file
28
+ prepare_pid_file("#{RAILS_ROOT}/tmp/pids", "mongrel_selenium.pid")
29
+ end
30
+
31
+ def start_command
32
+ "mongrel_rails start -d --chdir='#{RAILS_ROOT}' --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.application_environment} --pid #{pid_file} &"
33
+ end
34
+
35
+ def stop_command
36
+ "mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}"
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,35 @@
1
+ module Webrat
2
+ module Selenium
3
+ module ApplicationServers
4
+ class Sinatra < Webrat::Selenium::ApplicationServers::Base
5
+
6
+ def start
7
+ fork do
8
+ File.open('rack.pid', 'w') { |fp| fp.write Process.pid }
9
+ exec 'rackup', File.expand_path(Dir.pwd + '/config.ru'), '-p', Webrat.configuration.application_port.to_s
10
+ end
11
+ end
12
+
13
+ def stop
14
+ silence_stream(STDOUT) do
15
+ pid = File.read(pid_file)
16
+ system("kill -9 #{pid}")
17
+ FileUtils.rm_f pid_file
18
+ end
19
+ end
20
+
21
+ def fail
22
+ $stderr.puts
23
+ $stderr.puts
24
+ $stderr.puts "==> Failed to boot the Sinatra application server... exiting!"
25
+ exit
26
+ end
27
+
28
+ def pid_file
29
+ prepare_pid_file(Dir.pwd, 'rack.pid')
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,12 +1,19 @@
1
1
  if (locator == '*') {
2
2
  return selenium.browserbot.locationStrategies['xpath'].call(this, "//input[@type='submit']", inDocument, inWindow)
3
3
  }
4
+ var buttons = inDocument.getElementsByTagName('button');
4
5
  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;
6
+ var result = $A(inputs).concat($A(buttons)).find(function(candidate){
7
+ var type = candidate.getAttribute('type');
8
+ if (type == 'submit' || type == 'image' || type == 'button') {
9
+ var matches_id = PatternMatcher.matches(locator, candidate.id);
10
+ var matches_value = PatternMatcher.matches(locator, candidate.value);
11
+ var matches_html = PatternMatcher.matches(locator, candidate.innerHTML);
12
+ var matches_alt = PatternMatcher.matches(locator, candidate.alt);
13
+ if (matches_id || matches_value || matches_html || matches_alt) {
14
+ return true;
15
+ }
16
+ }
17
+ return false;
12
18
  });
19
+ return result;
@@ -1,8 +1,7 @@
1
1
  var allLabels = inDocument.getElementsByTagName("label");
2
2
  var candidateLabels = $A(allLabels).select(function(candidateLabel){
3
- var regExp = new RegExp('^' + locator + '\\b', 'i');
4
3
  var labelText = getText(candidateLabel).strip();
5
- return (labelText.search(regExp) >= 0);
4
+ return labelText.toLowerCase() == locator.toLowerCase();
6
5
  });
7
6
  if (candidateLabels.length == 0) {
8
7
  return null;
@@ -1,6 +1,9 @@
1
1
  var links = inDocument.getElementsByTagName('a');
2
2
  var candidateLinks = $A(links).select(function(candidateLink) {
3
- return PatternMatcher.matches(locator, getText(candidateLink));
3
+ var textMatched = PatternMatcher.matches(locator, getText(candidateLink));
4
+ var idMatched = PatternMatcher.matches(locator, candidateLink.id);
5
+ var titleMatched = PatternMatcher.matches(locator, candidateLink.title);
6
+ return textMatched || idMatched || titleMatched;
4
7
  });
5
8
  if (candidateLinks.length == 0) {
6
9
  return null;
@@ -1,108 +1,4 @@
1
- module Webrat
2
- module Selenium
3
- module Matchers
4
-
5
- class HaveXpath
6
- def initialize(expected)
7
- @expected = expected
8
- end
9
-
10
- def matches?(response)
11
- response.session.wait_for do
12
- response.selenium.is_element_present("xpath=#{@expected}")
13
- end
14
- end
15
-
16
- # ==== Returns
17
- # String:: The failure message.
18
- def failure_message
19
- "expected following text to match xpath #{@expected}:\n#{@document}"
20
- end
21
-
22
- # ==== Returns
23
- # String:: The failure message to be displayed in negative matches.
24
- def negative_failure_message
25
- "expected following text to not match xpath #{@expected}:\n#{@document}"
26
- end
27
- end
28
-
29
- def have_xpath(xpath)
30
- HaveXpath.new(xpath)
31
- end
32
-
33
- class HaveSelector
34
- def initialize(expected)
35
- @expected = expected
36
- end
37
-
38
- def matches?(response)
39
- response.session.wait_for do
40
- response.selenium.is_element_present("css=#{@expected}")
41
- end
42
- end
43
-
44
- # ==== Returns
45
- # String:: The failure message.
46
- def failure_message
47
- "expected following text to match selector #{@expected}:\n#{@document}"
48
- end
49
-
50
- # ==== Returns
51
- # String:: The failure message to be displayed in negative matches.
52
- def negative_failure_message
53
- "expected following text to not match selector #{@expected}:\n#{@document}"
54
- end
55
- end
56
-
57
- def have_selector(content)
58
- HaveSelector.new(content)
59
- end
60
-
61
- class HasContent #:nodoc:
62
- def initialize(content)
63
- @content = content
64
- end
65
-
66
- def matches?(response)
67
- if @content.is_a?(Regexp)
68
- text_finder = "regexp:#{@content.source}"
69
- else
70
- text_finder = @content
71
- end
72
-
73
- response.session.wait_for do
74
- response.selenium.is_text_present(text_finder)
75
- end
76
- end
77
-
78
- # ==== Returns
79
- # String:: The failure message.
80
- def failure_message
81
- "expected the following element's content to #{content_message}:\n#{@element}"
82
- end
83
-
84
- # ==== Returns
85
- # String:: The failure message to be displayed in negative matches.
86
- def negative_failure_message
87
- "expected the following element's content to not #{content_message}:\n#{@element}"
88
- end
89
-
90
- def content_message
91
- case @content
92
- when String
93
- "include \"#{@content}\""
94
- when Regexp
95
- "match #{@content.inspect}"
96
- end
97
- end
98
- end
99
-
100
- # Matches the contents of an HTML document with
101
- # whatever string is supplied
102
- def contain(content)
103
- HasContent.new(content)
104
- end
105
-
106
- end
107
- end
108
- end
1
+ require "webrat/selenium/matchers/have_xpath"
2
+ require "webrat/selenium/matchers/have_selector"
3
+ # require "webrat/selenium/matchers/have_tag"
4
+ require "webrat/selenium/matchers/have_content"
@@ -0,0 +1,66 @@
1
+ module Webrat
2
+ module Selenium
3
+ module Matchers
4
+ class HasContent #:nodoc:
5
+ def initialize(content)
6
+ @content = content
7
+ end
8
+
9
+ def matches?(response)
10
+ if @content.is_a?(Regexp)
11
+ text_finder = "regexp:#{@content.source}"
12
+ else
13
+ text_finder = @content
14
+ end
15
+
16
+ response.session.wait_for do
17
+ response.selenium.is_text_present(text_finder)
18
+ end
19
+ rescue Webrat::TimeoutError
20
+ false
21
+ end
22
+
23
+ # ==== Returns
24
+ # String:: The failure message.
25
+ def failure_message
26
+ "expected the following element's content to #{content_message}:\n#{@element}"
27
+ end
28
+
29
+ # ==== Returns
30
+ # String:: The failure message to be displayed in negative matches.
31
+ def negative_failure_message
32
+ "expected the following element's content to not #{content_message}:\n#{@element}"
33
+ end
34
+
35
+ def content_message
36
+ case @content
37
+ when String
38
+ "include \"#{@content}\""
39
+ when Regexp
40
+ "match #{@content.inspect}"
41
+ end
42
+ end
43
+ end
44
+
45
+ # Matches the contents of an HTML document with
46
+ # whatever string is supplied
47
+ def contain(content)
48
+ HasContent.new(content)
49
+ end
50
+
51
+ # Asserts that the body of the response contain
52
+ # the supplied string or regexp
53
+ def assert_contain(content)
54
+ hc = HasContent.new(content)
55
+ assert hc.matches?(response), hc.failure_message
56
+ end
57
+
58
+ # Asserts that the body of the response
59
+ # does not contain the supplied string or regepx
60
+ def assert_not_contain(content)
61
+ hc = HasContent.new(content)
62
+ assert !hc.matches?(response), hc.negative_failure_message
63
+ end
64
+ end
65
+ end
66
+ end