webrat 0.4.0 → 0.4.1

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.
data/History.txt CHANGED
@@ -1,3 +1,30 @@
1
+ == 0.4.1 / 2009-01-31
2
+
3
+ * Minor enhancements
4
+
5
+ * Support Sinatra 0.9 (Harry Vangberg)
6
+ * Update query param parsing to work with latest Edge Rails
7
+ * Added #redirected_to method to easily check where an external redirect was
8
+ redirected to (Adam Greene)
9
+ * Recognize input tags with type button (Lena Herrmann)
10
+ * Add uncheck method to Selenium mode (Lee Bankewitz)
11
+
12
+ * Bug fixes
13
+
14
+ * Make requests to a Rails app using a full URL instead of a relative path. This change
15
+ is helpful for Rails apps that use subdomains. (John Hwang and Zach Dennis)
16
+ * Follow redirects that are on the same domain but a different subdomain
17
+ (Adam Greene)
18
+ * rescue from Webrat::TimeoutError in selenium matchers which allows NegativeMatchers
19
+ to behave correctly (Noah Davis)
20
+ * Switch to using selenium.click instead of .check when checking a checkbox
21
+ (Noah Davis)
22
+ * Create tmp/pids directory if directory does not exist. (Amos King and Mike Gaffney)
23
+ * Setup deprecated writers for the selenium_environment= and selenium_port= config
24
+ * Ensure the previous pages params aren't passed through redirect (Daniel Lucraft and
25
+ Bryan Helmkamp)
26
+ * Labels should only search for fields within the current scope (Kyle Hargraves)
27
+
1
28
  == 0.4.0 / 2009-01-18
2
29
 
3
30
  * _IMPORTANT_ Breaking change:
data/Rakefile CHANGED
@@ -72,7 +72,7 @@ end
72
72
  desc 'Install the package as a gem.'
73
73
  task :install_gem => [:clean, :package] do
74
74
  gem_filename = Dir['pkg/*.gem'].first
75
- sh "sudo gem install --local #{gem_filename}"
75
+ sh "sudo gem install --no-rdoc --no-ri --local #{gem_filename}"
76
76
  end
77
77
 
78
78
  desc "Delete generated RDoc"
@@ -87,7 +87,8 @@ end
87
87
 
88
88
  desc "Run specs using jruby"
89
89
  task "spec:jruby" do
90
- system "jruby -S rake spec"
90
+ result = system "jruby -S rake spec"
91
+ raise "JRuby tests failed" unless result
91
92
  end
92
93
 
93
94
  desc "Run each spec in isolation to test for dependency issues"
@@ -109,10 +110,21 @@ namespace :spec do
109
110
 
110
111
  namespace :integration do
111
112
  desc "Run the Rails integration specs"
112
- task :rails do
113
- Dir.chdir "spec/integration/rails" do
114
- result = system "rake test:integration"
115
- raise "Rails integration tests failed" unless result
113
+ task :rails => ['rails:webrat'] #,'rails:selenium'] currently not running selenium as it doesn't pass.
114
+
115
+ namespace :rails do
116
+ task :selenium do
117
+ Dir.chdir "spec/integration/rails" do
118
+ result = system "rake test_unit:selenium"
119
+ raise "Rails integration tests failed" unless result
120
+ end
121
+ end
122
+
123
+ task :webrat do
124
+ Dir.chdir "spec/integration/rails" do
125
+ result = system "rake test_unit:rails"
126
+ raise "Rails integration tests failed" unless result
127
+ end
116
128
  end
117
129
  end
118
130
 
data/lib/webrat.rb CHANGED
@@ -7,7 +7,7 @@ module Webrat
7
7
  class WebratError < StandardError
8
8
  end
9
9
 
10
- VERSION = '0.4.0'
10
+ VERSION = '0.4.1'
11
11
 
12
12
  def self.require_xml
13
13
  gem "nokogiri", ">= 1.0.6"
@@ -32,10 +32,12 @@ module Webrat
32
32
  # Which rails environment should the selenium tests be run in? Defaults to selenium.
33
33
  attr_accessor :application_environment
34
34
  webrat_deprecate :selenium_environment, :application_environment
35
+ webrat_deprecate :selenium_environment=, :application_environment=
35
36
 
36
37
  # Which port is the application running on for selenium testing? Defaults to 3001.
37
38
  attr_accessor :application_port
38
39
  webrat_deprecate :selenium_port, :application_port
40
+ webrat_deprecate :selenium_port=, :application_port=
39
41
 
40
42
  # Which server the application is running on for selenium testing? Defaults to localhost
41
43
  attr_accessor :application_address
@@ -45,6 +45,7 @@ module Webrat
45
45
  when "file" then FileField
46
46
  when "reset" then ResetField
47
47
  when "submit" then ButtonField
48
+ when "button" then ButtonField
48
49
  when "image" then ButtonField
49
50
  else TextField
50
51
  end
@@ -98,10 +99,11 @@ module Webrat
98
99
  protected
99
100
 
100
101
  def rails_request_parser
101
- if defined?(ActionController::RequestParser) # For Rails > 2.2
102
- ActionController::RequestParser
103
- else
102
+ if defined?(ActionController::AbstractRequest)
104
103
  ActionController::AbstractRequest
104
+ else
105
+ # For Rails > 2.2
106
+ ActionController::UrlEncodedPairParser
105
107
  end
106
108
  end
107
109
 
@@ -176,7 +178,7 @@ module Webrat
176
178
  class ButtonField < Field #:nodoc:
177
179
 
178
180
  def self.xpath_search
179
- [".//button", ".//input[@type = 'submit']", ".//input[@type = 'image']"]
181
+ [".//button", ".//input[@type = 'submit']", ".//input[@type = 'button']", ".//input[@type = 'image']"]
180
182
  end
181
183
 
182
184
  def to_param
@@ -23,7 +23,7 @@ module Webrat
23
23
  if for_id.blank?
24
24
  Webrat::XML.xpath_at(@element, *Field.xpath_search)
25
25
  else
26
- Webrat::XML.css_search(@session.dom, "#" + for_id).first
26
+ Webrat::XML.css_search(@session.current_dom, "#" + for_id).first
27
27
  end
28
28
  end
29
29
 
@@ -112,7 +112,7 @@ For example:
112
112
  @http_method = http_method
113
113
  @data = data
114
114
 
115
- request_page(response_location, :get, data) if internal_redirect?
115
+ request_page(response_location, :get, {}) if internal_redirect?
116
116
 
117
117
  return response
118
118
  end
@@ -125,8 +125,17 @@ For example:
125
125
  response_code / 100 == 3
126
126
  end
127
127
 
128
- def internal_redirect? #:nodoc:
129
- redirect? && current_host == response_location_host
128
+ def internal_redirect?
129
+ return false unless redirect?
130
+ #should keep internal_redirects if the subdomain changes
131
+ current_host_domain = current_host.split('.')[-2..-1].join('.') rescue current_host
132
+ response_location_host_domain = response_location_host.split('.')[-2..-1].join('.') rescue response_location_host
133
+ current_host_domain == response_location_host_domain
134
+ end
135
+
136
+ #easy helper to pull out where we were redirected to
137
+ def redirected_to
138
+ redirect? ? response_location : nil
130
139
  end
131
140
 
132
141
  def exception_caught? #:nodoc:
data/lib/webrat/rails.rb CHANGED
@@ -73,11 +73,13 @@ module Webrat
73
73
  # remove protocol, host and anchor
74
74
  def normalize_url(href) #:nodoc:
75
75
  uri = URI.parse(href)
76
- normalized_url = uri.path
77
- if uri.query
78
- normalized_url += "?" + uri.query
79
- end
80
- normalized_url
76
+ normalized_url = []
77
+ normalized_url << "#{uri.scheme}://" if uri.scheme
78
+ normalized_url << uri.host if uri.host
79
+ normalized_url << ":#{uri.port}" if uri.port && ![80,443].include?(uri.port)
80
+ normalized_url << uri.path if uri.path
81
+ normalized_url << "?#{uri.query}" if uri.query
82
+ normalized_url.join
81
83
  end
82
84
 
83
85
  def update_protocol(href) #:nodoc:
@@ -26,7 +26,7 @@ module Webrat
26
26
  end
27
27
 
28
28
  def self.start_app_server #:nodoc:
29
- pid_file = File.expand_path(RAILS_ROOT + "/tmp/pids/mongrel_selenium.pid")
29
+ pid_file = prepare_pid_file("#{RAILS_ROOT}/tmp/pids", "mongrel_selenium.pid")
30
30
  system("mongrel_rails start -d --chdir=#{RAILS_ROOT} --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.application_environment} --pid #{pid_file} &")
31
31
  TCPSocket.wait_for_service :host => Webrat.configuration.application_address, :port => Webrat.configuration.application_port.to_i
32
32
  end
@@ -36,6 +36,11 @@ module Webrat
36
36
  system "mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}"
37
37
  end
38
38
 
39
+ def self.prepare_pid_file(file_path, pid_file_name)
40
+ FileUtils.mkdir_p File.expand_path(file_path)
41
+ File.expand_path("#{file_path}/#{pid_file_name}")
42
+ end
43
+
39
44
  # To use Webrat's Selenium support, you'll need the selenium-client gem installed.
40
45
  # Activate it with (for example, in your <tt>env.rb</tt>):
41
46
  #
@@ -1,146 +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
- def assert_have_xpath(expected)
34
- hs = HaveXpath.new(expected)
35
- assert hs.matches?(response), hs.failure_message
36
- end
37
-
38
- def assert_have_no_xpath(expected)
39
- hs = HaveXpath.new(expected)
40
- assert !hs.matches?(response), hs.negative_failure_message
41
- end
42
-
43
- class HaveSelector
44
- def initialize(expected)
45
- @expected = expected
46
- end
47
-
48
- def matches?(response)
49
- response.session.wait_for do
50
- response.selenium.is_element_present("css=#{@expected}")
51
- end
52
- end
53
-
54
- # ==== Returns
55
- # String:: The failure message.
56
- def failure_message
57
- "expected following text to match selector #{@expected}:\n#{@document}"
58
- end
59
-
60
- # ==== Returns
61
- # String:: The failure message to be displayed in negative matches.
62
- def negative_failure_message
63
- "expected following text to not match selector #{@expected}:\n#{@document}"
64
- end
65
- end
66
-
67
- def have_selector(content)
68
- HaveSelector.new(content)
69
- end
70
-
71
- # Asserts that the body of the response contains
72
- # the supplied selector
73
- def assert_have_selector(expected)
74
- hs = HaveSelector.new(expected)
75
- assert hs.matches?(response), hs.failure_message
76
- end
77
-
78
- # Asserts that the body of the response
79
- # does not contain the supplied string or regepx
80
- def assert_have_no_selector(expected)
81
- hs = HaveSelector.new(expected)
82
- assert !hs.matches?(response), hs.negative_failure_message
83
- end
84
-
85
- class HasContent #:nodoc:
86
- def initialize(content)
87
- @content = content
88
- end
89
-
90
- def matches?(response)
91
- if @content.is_a?(Regexp)
92
- text_finder = "regexp:#{@content.source}"
93
- else
94
- text_finder = @content
95
- end
96
-
97
- response.session.wait_for do
98
- response.selenium.is_text_present(text_finder)
99
- end
100
- end
101
-
102
- # ==== Returns
103
- # String:: The failure message.
104
- def failure_message
105
- "expected the following element's content to #{content_message}:\n#{@element}"
106
- end
107
-
108
- # ==== Returns
109
- # String:: The failure message to be displayed in negative matches.
110
- def negative_failure_message
111
- "expected the following element's content to not #{content_message}:\n#{@element}"
112
- end
113
-
114
- def content_message
115
- case @content
116
- when String
117
- "include \"#{@content}\""
118
- when Regexp
119
- "match #{@content.inspect}"
120
- end
121
- end
122
- end
123
-
124
- # Matches the contents of an HTML document with
125
- # whatever string is supplied
126
- def contain(content)
127
- HasContent.new(content)
128
- end
129
-
130
- # Asserts that the body of the response contain
131
- # the supplied string or regexp
132
- def assert_contain(content)
133
- hc = HasContent.new(content)
134
- assert hc.matches?(response), hc.failure_message
135
- end
136
-
137
- # Asserts that the body of the response
138
- # does not contain the supplied string or regepx
139
- def assert_not_contain(content)
140
- hc = HasContent.new(content)
141
- assert !hc.matches?(response), hc.negative_failure_message
142
- end
143
-
144
- end
145
- end
146
- 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
@@ -0,0 +1,49 @@
1
+ module Webrat
2
+ module Selenium
3
+ module Matchers
4
+ class HaveSelector
5
+ def initialize(expected)
6
+ @expected = expected
7
+ end
8
+
9
+ def matches?(response)
10
+ response.session.wait_for do
11
+ response.selenium.is_element_present("css=#{@expected}")
12
+ end
13
+ rescue Webrat::TimeoutError
14
+ false
15
+ end
16
+
17
+ # ==== Returns
18
+ # String:: The failure message.
19
+ def failure_message
20
+ "expected following text to match selector #{@expected}:\n#{@document}"
21
+ end
22
+
23
+ # ==== Returns
24
+ # String:: The failure message to be displayed in negative matches.
25
+ def negative_failure_message
26
+ "expected following text to not match selector #{@expected}:\n#{@document}"
27
+ end
28
+ end
29
+
30
+ def have_selector(content)
31
+ HaveSelector.new(content)
32
+ end
33
+
34
+ # Asserts that the body of the response contains
35
+ # the supplied selector
36
+ def assert_have_selector(expected)
37
+ hs = HaveSelector.new(expected)
38
+ assert hs.matches?(response), hs.failure_message
39
+ end
40
+
41
+ # Asserts that the body of the response
42
+ # does not contain the supplied string or regepx
43
+ def assert_have_no_selector(expected)
44
+ hs = HaveSelector.new(expected)
45
+ assert !hs.matches?(response), hs.negative_failure_message
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,72 @@
1
+ module Webrat
2
+ module Selenium
3
+ module Matchers
4
+
5
+ class HaveTag < HaveSelector #:nodoc:
6
+ # ==== Returns
7
+ # String:: The failure message.
8
+ def failure_message
9
+ "expected following output to contain a #{tag_inspect} tag:\n#{@document}"
10
+ end
11
+
12
+ # ==== Returns
13
+ # String:: The failure message to be displayed in negative matches.
14
+ def negative_failure_message
15
+ "expected following output to omit a #{tag_inspect}:\n#{@document}"
16
+ end
17
+
18
+ def tag_inspect
19
+ options = @expected.last.dup
20
+ content = options.delete(:content)
21
+
22
+ html = "<#{@expected.first}"
23
+ options.each do |k,v|
24
+ html << " #{k}='#{v}'"
25
+ end
26
+
27
+ if content
28
+ html << ">#{content}</#{@expected.first}>"
29
+ else
30
+ html << "/>"
31
+ end
32
+
33
+ html
34
+ end
35
+
36
+ def query
37
+ options = @expected.last.dup
38
+ selector = @expected.first.to_s
39
+
40
+ selector << ":contains('#{options.delete(:content)}')" if options[:content]
41
+
42
+ options.each do |key, value|
43
+ selector << "[#{key}='#{value}']"
44
+ end
45
+
46
+ Nokogiri::CSS::Parser.parse(selector).map { |ast| ast.to_xpath }
47
+ end
48
+ end
49
+
50
+ def have_tag(name, attributes = {}, &block)
51
+ HaveTag.new([name, attributes], &block)
52
+ end
53
+
54
+ alias_method :match_tag, :have_tag
55
+
56
+ # Asserts that the body of the response contains
57
+ # the supplied tag with the associated selectors
58
+ def assert_have_tag(name, attributes = {})
59
+ ht = HaveTag.new([name, attributes])
60
+ assert ht.matches?(response), ht.failure_message
61
+ end
62
+
63
+ # Asserts that the body of the response
64
+ # does not contain the supplied string or regepx
65
+ def assert_have_no_tag(name, attributes = {})
66
+ ht = HaveTag.new([name, attributes])
67
+ assert !ht.matches?(response), ht.negative_failure_message
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,45 @@
1
+ module Webrat
2
+ module Selenium
3
+ module Matchers
4
+ class HaveXpath
5
+ def initialize(expected)
6
+ @expected = expected
7
+ end
8
+
9
+ def matches?(response)
10
+ response.session.wait_for do
11
+ response.selenium.is_element_present("xpath=#{@expected}")
12
+ end
13
+ rescue Webrat::TimeoutError
14
+ false
15
+ end
16
+
17
+ # ==== Returns
18
+ # String:: The failure message.
19
+ def failure_message
20
+ "expected following text to match xpath #{@expected}:\n#{@document}"
21
+ end
22
+
23
+ # ==== Returns
24
+ # String:: The failure message to be displayed in negative matches.
25
+ def negative_failure_message
26
+ "expected following text to not match xpath #{@expected}:\n#{@document}"
27
+ end
28
+ end
29
+
30
+ def have_xpath(xpath)
31
+ HaveXpath.new(xpath)
32
+ end
33
+
34
+ def assert_have_xpath(expected)
35
+ hs = HaveXpath.new(expected)
36
+ assert hs.matches?(response), hs.failure_message
37
+ end
38
+
39
+ def assert_have_no_xpath(expected)
40
+ hs = HaveXpath.new(expected)
41
+ assert !hs.matches?(response), hs.negative_failure_message
42
+ end
43
+ end
44
+ end
45
+ end
@@ -111,8 +111,9 @@ module Webrat
111
111
  def check(label_text)
112
112
  locator = "webrat=#{label_text}"
113
113
  selenium.wait_for_element locator, 5
114
- selenium.check locator
114
+ selenium.click locator
115
115
  end
116
+ alias_method :uncheck, :check
116
117
 
117
118
  webrat_deprecate :checks, :check
118
119
 
@@ -1,6 +1,6 @@
1
1
  require 'webrat/rack'
2
2
  require 'sinatra'
3
- require 'sinatra/test/methods'
3
+ require 'sinatra/test'
4
4
 
5
5
  class Sinatra::Application
6
6
  # Override this to prevent Sinatra from barfing on the options passed from RSpec
@@ -13,16 +13,17 @@ disable :reload
13
13
 
14
14
  module Webrat
15
15
  class SinatraSession < RackSession #:nodoc:
16
- include Sinatra::Test::Methods
16
+ include Sinatra::Test
17
17
 
18
18
  attr_reader :request, :response
19
19
 
20
20
  %w(get head post put delete).each do |verb|
21
+ alias_method "orig_#{verb}", verb
21
22
  define_method(verb) do |*args| # (path, data, headers = nil)
22
23
  path, data, headers = *args
23
24
  data = data.inject({}) {|data, (key,value)| data[key] = Rack::Utils.unescape(value); data }
24
25
  params = data.merge(:env => headers || {})
25
- self.__send__("#{verb}_it", path, params)
26
+ self.__send__("orig_#{verb}", path, params)
26
27
  end
27
28
  end
28
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webrat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Helmkamp
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-18 00:00:00 -05:00
12
+ date: 2009-01-31 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -100,6 +100,11 @@ files:
100
100
  - lib/webrat/selenium/location_strategy_javascript/webratlink.js
101
101
  - lib/webrat/selenium/location_strategy_javascript/webratlinkwithin.js
102
102
  - lib/webrat/selenium/location_strategy_javascript/webratselectwithoption.js
103
+ - lib/webrat/selenium/matchers
104
+ - lib/webrat/selenium/matchers/have_content.rb
105
+ - lib/webrat/selenium/matchers/have_selector.rb
106
+ - lib/webrat/selenium/matchers/have_tag.rb
107
+ - lib/webrat/selenium/matchers/have_xpath.rb
103
108
  - lib/webrat/selenium/matchers.rb
104
109
  - lib/webrat/selenium/selenium_extensions.js
105
110
  - lib/webrat/selenium/selenium_session.rb