emipair-webrat 0.0.1 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/.document +4 -0
  2. data/.gitignore +15 -0
  3. data/History.txt +34 -1
  4. data/Rakefile +45 -78
  5. data/VERSION +1 -0
  6. data/emipair-webrat.gemspec +354 -0
  7. data/lib/webrat.rb +3 -24
  8. data/lib/webrat/core/configuration.rb +5 -1
  9. data/lib/webrat/core/elements/field.rb +3 -3
  10. data/lib/webrat/core/logging.rb +3 -4
  11. data/lib/webrat/core/methods.rb +4 -4
  12. data/lib/webrat/core/mime.rb +11 -22
  13. data/lib/webrat/core/session.rb +27 -12
  14. data/lib/webrat/mechanize.rb +5 -1
  15. data/lib/webrat/{merb_session.rb → merb_adapter.rb} +4 -2
  16. data/lib/webrat/rack.rb +15 -14
  17. data/lib/webrat/rails.rb +7 -10
  18. data/lib/webrat/selenium.rb +0 -1
  19. data/lib/webrat/selenium/selenium_session.rb +8 -8
  20. data/lib/webrat/sinatra.rb +3 -36
  21. data/spec/fakes/{test_session.rb → test_adapter.rb} +6 -3
  22. data/spec/integration/mechanize/Rakefile +7 -0
  23. data/spec/integration/mechanize/config.ru +2 -0
  24. data/spec/integration/mechanize/sample_app.rb +20 -0
  25. data/spec/integration/mechanize/spec/mechanize_spec.rb +22 -0
  26. data/spec/integration/mechanize/spec/spec_helper.rb +27 -0
  27. data/spec/integration/merb/.gitignore +21 -0
  28. data/spec/integration/merb/Rakefile +35 -0
  29. data/spec/integration/merb/app/views/exceptions/not_acceptable.html.erb +63 -0
  30. data/spec/integration/merb/app/views/exceptions/not_found.html.erb +47 -0
  31. data/spec/integration/merb/app/views/layout/application.html.erb +12 -0
  32. data/spec/integration/merb/app/views/testing/show_form.html.erb +27 -0
  33. data/spec/integration/merb/app/views/testing/upload.html.erb +9 -0
  34. data/spec/integration/merb/spec/spec.opts +1 -0
  35. data/spec/integration/merb/spec/spec_helper.rb +2 -0
  36. data/spec/integration/merb/tasks/merb.thor/main.thor +150 -0
  37. data/spec/integration/rack/Rakefile +5 -0
  38. data/spec/integration/rack/app.rb +89 -0
  39. data/spec/integration/rack/test/helper.rb +21 -0
  40. data/spec/integration/rack/test/webrat_rack_test.rb +58 -52
  41. data/spec/integration/rails/.gitignore +3 -0
  42. data/spec/integration/rails/Rakefile +30 -0
  43. data/spec/integration/rails/app/views/buttons/show.html.erb +11 -0
  44. data/spec/integration/rails/app/views/fields/show.html.erb +9 -0
  45. data/spec/integration/rails/app/views/links/show.html.erb +5 -0
  46. data/spec/integration/rails/app/views/webrat/before_redirect_form.html.erb +4 -0
  47. data/spec/integration/rails/app/views/webrat/buttons.html.erb +11 -0
  48. data/spec/integration/rails/app/views/webrat/form.html.erb +28 -0
  49. data/spec/integration/rails/config/locales/en.yml +5 -0
  50. data/spec/integration/rails/public/404.html +30 -0
  51. data/spec/integration/rails/public/422.html +30 -0
  52. data/spec/integration/rails/public/500.html +33 -0
  53. data/spec/integration/rails/script/about +4 -0
  54. data/spec/integration/rails/script/console +3 -0
  55. data/spec/integration/rails/script/dbconsole +3 -0
  56. data/spec/integration/rails/script/destroy +3 -0
  57. data/spec/integration/rails/script/generate +3 -0
  58. data/spec/integration/rails/script/performance/benchmarker +3 -0
  59. data/spec/integration/rails/script/performance/profiler +3 -0
  60. data/spec/integration/rails/script/performance/request +3 -0
  61. data/spec/integration/rails/script/plugin +3 -0
  62. data/spec/integration/rails/script/process/inspector +3 -0
  63. data/spec/integration/rails/script/process/reaper +3 -0
  64. data/spec/integration/rails/script/process/spawner +3 -0
  65. data/spec/integration/rails/script/runner +3 -0
  66. data/spec/integration/rails/script/server +3 -0
  67. data/spec/integration/rails/test/test_helper.rb +2 -1
  68. data/spec/integration/sinatra/Rakefile +5 -0
  69. data/spec/integration/sinatra/test/test_helper.rb +2 -1
  70. data/spec/private/core/configuration_spec.rb +11 -2
  71. data/spec/private/core/field_spec.rb +1 -1
  72. data/spec/private/core/link_spec.rb +1 -1
  73. data/spec/private/core/session_spec.rb +5 -0
  74. data/spec/private/mechanize/{mechanize_session_spec.rb → mechanize_adapter_spec.rb} +4 -10
  75. data/spec/private/merb/{merb_session_spec.rb → merb_adapter_spec.rb} +5 -5
  76. data/spec/private/rails/{rails_session_spec.rb → rails_adapter_spec.rb} +17 -14
  77. data/spec/public/matchers/contain_spec.rb +4 -4
  78. data/spec/public/matchers/have_selector_spec.rb +2 -2
  79. data/spec/public/matchers/have_xpath_spec.rb +2 -2
  80. data/spec/rcov.opts +1 -0
  81. data/spec/spec.opts +2 -0
  82. data/spec/spec_helper.rb +3 -1
  83. metadata +172 -19
  84. data/lib/webrat/rack_test.rb +0 -32
  85. data/spec/integration/rack/rack_app.rb +0 -16
  86. data/spec/integration/rack/test/test_helper.rb +0 -20
  87. data/spec/private/core/logging_spec.rb +0 -10
@@ -1,31 +1,10 @@
1
- require "rubygems"
2
-
3
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
4
-
5
1
  module Webrat
6
2
  # The common base class for all exceptions raised by Webrat.
7
3
  class WebratError < StandardError
8
4
  end
9
-
10
- VERSION = '0.4.4'
11
-
12
- def self.require_xml
13
- if on_java?
14
- gem "nokogiri", ">= 1.2.4"
15
- else
16
- gem "nokogiri", ">= 1.0.6"
17
- end
18
-
19
- require "nokogiri"
20
- require "webrat/core/xml/nokogiri"
21
- end
22
-
23
- def self.on_java?
24
- RUBY_PLATFORM =~ /java/
25
- end
26
-
27
5
  end
28
6
 
29
- Webrat.require_xml
30
-
7
+ require "rack"
8
+ require "nokogiri"
9
+ require "webrat/core/xml/nokogiri"
31
10
  require "webrat/core"
@@ -57,6 +57,9 @@ module Webrat
57
57
  # Set the timeout for waiting for the browser process to start
58
58
  attr_accessor :selenium_browser_startup_timeout
59
59
 
60
+ # Set the default timeout for selenium waits
61
+ attr_accessor :selenium_timeout
62
+
60
63
  # How many redirects to the same URL should be halted as an infinite redirect
61
64
  # loop? Defaults to 10
62
65
  attr_accessor :infinite_redirect_limit
@@ -72,6 +75,7 @@ module Webrat
72
75
  self.infinite_redirect_limit = 10
73
76
  self.selenium_browser_key = '*firefox'
74
77
  self.selenium_browser_startup_timeout = 5
78
+ self.selenium_timeout = 5
75
79
  end
76
80
 
77
81
  def parse_with_nokogiri? #:nodoc:
@@ -91,7 +95,7 @@ module Webrat
91
95
  # with Merb 1.0.8 until it's updated to use the new Webrat.configure
92
96
  # syntax
93
97
  if @mode == :merb
94
- require("webrat/merb_session")
98
+ require("webrat/merb_adapter")
95
99
  else
96
100
  require("webrat/#{mode}")
97
101
  end
@@ -87,10 +87,8 @@ module Webrat
87
87
  parse_rails_request_params("#{name}=#{escaped_value}")
88
88
  when :merb
89
89
  ::Merb::Parse.query("#{name}=#{escaped_value}")
90
- when :mechanize
91
- { name => value }
92
90
  else
93
- { name => escaped_value }
91
+ { name => value }
94
92
  end
95
93
  end
96
94
 
@@ -368,6 +366,8 @@ module Webrat
368
366
  when :merb
369
367
  # TODO: support content_type
370
368
  File.new(@value)
369
+ when :rack
370
+ Rack::Test::UploadedFile.new(@value, content_type)
371
371
  end
372
372
  end
373
373
 
@@ -1,3 +1,5 @@
1
+ require "logger"
2
+
1
3
  module Webrat
2
4
  module Logging #:nodoc:
3
5
 
@@ -13,10 +15,7 @@ module Webrat
13
15
  when :merb
14
16
  Merb.logger
15
17
  else
16
- @logger ||= begin
17
- require "logger"
18
- ::Logger.new("webrat.log")
19
- end
18
+ @logger ||= ::Logger.new("webrat.log")
20
19
  end
21
20
  end
22
21
 
@@ -16,10 +16,10 @@ module Webrat
16
16
  end
17
17
 
18
18
  def webrat_session
19
- if Webrat.configuration.mode == :rack_test
20
- @_webrat_session ||= ::Webrat::RackTestSession.new(rack_test_session)
21
- else
22
- @_webrat_session ||= ::Webrat.session_class.new(self)
19
+ @_webrat_session ||= begin
20
+ session = Webrat.session_class.new
21
+ session.adapter = Webrat.adapter_class.new(self) if session.respond_to?(:adapter=)
22
+ session
23
23
  end
24
24
  end
25
25
 
@@ -1,29 +1,18 @@
1
1
  module Webrat #:nodoc:
2
2
  module MIME #:nodoc:
3
+ MIME_TYPES = Rack::Mime::MIME_TYPES.dup.merge(
4
+ ".multipart_form" => "multipart/form-data",
5
+ ".url_encoded_form" => "application/x-www-form-urlencoded"
6
+ ).freeze
3
7
 
4
- def self.mime_type(string_or_symbol) #:nodoc:
5
- if string_or_symbol.is_a?(String)
6
- string_or_symbol
7
- else
8
- case string_or_symbol
9
- when :text then "text/plain"
10
- when :html then "text/html"
11
- when :js then "text/javascript"
12
- when :css then "text/css"
13
- when :ics then "text/calendar"
14
- when :csv then "text/csv"
15
- when :xml then "application/xml"
16
- when :rss then "application/rss+xml"
17
- when :atom then "application/atom+xml"
18
- when :yaml then "application/x-yaml"
19
- when :multipart_form then "multipart/form-data"
20
- when :url_encoded_form then "application/x-www-form-urlencoded"
21
- when :json then "application/json"
22
- else
23
- raise ArgumentError.new("Invalid Mime type: #{string_or_symbol.inspect}")
24
- end
25
- end
8
+ def mime_type(type)
9
+ return type if type.nil? || type.to_s.include?("/")
10
+ type = ".#{type}" unless type.to_s[0] == ?.
11
+ MIME_TYPES.fetch(type) { |type|
12
+ raise ArgumentError.new("Invalid Mime type: #{type}")
13
+ }
26
14
  end
27
15
 
16
+ module_function :mime_type
28
17
  end
29
18
  end
@@ -13,21 +13,30 @@ module Webrat
13
13
  end
14
14
 
15
15
  def self.session_class
16
+ if Webrat.configuration.mode == :selenium
17
+ SeleniumSession
18
+ else
19
+ Session
20
+ end
21
+ end
22
+
23
+ def self.adapter_class
16
24
  case Webrat.configuration.mode
17
25
  when :rails
18
- RailsSession
26
+ RailsAdapter
19
27
  when :merb
20
- MerbSession
21
- when :selenium
22
- SeleniumSession
28
+ MerbAdapter
23
29
  when :rack
24
- RackSession
30
+ RackAdapter
31
+ when :rack_test
32
+ warn("The :rack_test mode is deprecated. Please use :rack instead")
33
+ require "webrat/rack"
34
+ RackAdapter
25
35
  when :sinatra
26
- SinatraSession
36
+ warn("The :sinatra mode is deprecated. Please use :rack instead")
37
+ SinatraAdapter
27
38
  when :mechanize
28
- MechanizeSession
29
- when :rack_test
30
- RackTestSession
39
+ MechanizeAdapter
31
40
  else
32
41
  raise WebratError.new(<<-STR)
33
42
  Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
@@ -50,16 +59,22 @@ For example:
50
59
  extend Forwardable
51
60
  include Logging
52
61
  include SaveAndOpenPage
62
+
63
+ attr_accessor :adapter
64
+
53
65
  attr_reader :current_url
54
66
  attr_reader :elements
55
67
 
56
- def initialize(context = nil) #:nodoc:
68
+ def_delegators :@adapter, :response, :response_code, :response_body,
69
+ :response_body=, :response_code=,
70
+ :get, :post, :put, :delete
71
+
72
+ def initialize(adapter = nil)
73
+ @adapter = adapter
57
74
  @http_method = :get
58
75
  @data = {}
59
76
  @default_headers = {}
60
77
  @custom_headers = {}
61
- @context = context
62
-
63
78
  reset
64
79
  end
65
80
 
@@ -1,11 +1,15 @@
1
1
  require "mechanize"
2
2
 
3
3
  module Webrat #:nodoc:
4
- class MechanizeSession < Session #:nodoc:
4
+ class MechanizeAdapter #:nodoc:
5
+ extend Forwardable
5
6
 
6
7
  attr_accessor :response
7
8
  alias :page :response
8
9
 
10
+ def initialize(*args)
11
+ end
12
+
9
13
  def request_page(url, http_method, data) #:nodoc:
10
14
  super(absolute_url(url), http_method, data)
11
15
  end
@@ -3,7 +3,7 @@ require "merb-core"
3
3
  require "webrat/merb_multipart_support"
4
4
 
5
5
  module Webrat
6
- class MerbSession < Session #:nodoc:
6
+ class MerbAdapter #:nodoc:
7
7
  include Merb::Test::MakeRequest
8
8
 
9
9
  # Include Webrat's own version of multipart_post/put because the officially
@@ -12,6 +12,8 @@ module Webrat
12
12
 
13
13
  attr_accessor :response
14
14
 
15
+ def initialize(context=nil); end
16
+
15
17
  def get(url, data, headers = nil)
16
18
  do_request(url, data, headers, "GET")
17
19
  end
@@ -72,7 +74,7 @@ module Merb #:nodoc:
72
74
  module Test #:nodoc:
73
75
  module RequestHelper #:nodoc:
74
76
  def request(uri, env = {})
75
- @_webrat_session ||= Webrat::MerbSession.new
77
+ @_webrat_session ||= Webrat::MerbAdapter.new
76
78
  @_webrat_session.response = @_webrat_session.request(uri, env)
77
79
  end
78
80
  end
@@ -1,24 +1,25 @@
1
- require 'webrat'
1
+ require "rack/test"
2
2
 
3
- class CGIMethods #:nodoc:
4
- def self.parse_query_parameters(params)
5
- hash = {}
6
- params.split('&').each do |p|
7
- pair = p.split('=')
8
- hash[pair[0]] = pair[1]
3
+ module Webrat
4
+ class RackAdapter
5
+ extend Forwardable
6
+
7
+ def_delegators :@session, :get, :post, :put, :delete
8
+
9
+ def initialize(session) #:nodoc:
10
+ @session = session
9
11
  end
10
- hash
11
- end
12
- end
13
12
 
14
- module Webrat
15
- class RackSession < Session #:nodoc:
16
13
  def response_body
17
- @response.body
14
+ response.body
18
15
  end
19
16
 
20
17
  def response_code
21
- @response.status
18
+ response.status
19
+ end
20
+
21
+ def response
22
+ @session.last_response
22
23
  end
23
24
  end
24
25
  end
@@ -1,13 +1,15 @@
1
1
  require "webrat"
2
2
 
3
- require "action_controller"
4
- require "action_controller/integration"
5
- require "action_controller/record_identifier"
6
-
7
3
  module Webrat
8
- class RailsSession < Session #:nodoc:
4
+ class RailsAdapter #:nodoc:
9
5
  include ActionController::RecordIdentifier
10
6
 
7
+ attr_reader :integration_session
8
+
9
+ def initialize(session)
10
+ @integration_session = session
11
+ end
12
+
11
13
  # The Rails version of within supports passing in a model and Webrat
12
14
  # will apply a scope based on Rails' dom_id for that model.
13
15
  #
@@ -61,10 +63,6 @@ module Webrat
61
63
 
62
64
  protected
63
65
 
64
- def integration_session
65
- @context
66
- end
67
-
68
66
  def do_request(http_method, url, data, headers) #:nodoc:
69
67
  update_protocol(url)
70
68
  integration_session.send(http_method, normalize_url(url), data, headers)
@@ -93,7 +91,6 @@ module Webrat
93
91
  def response #:nodoc:
94
92
  integration_session.response
95
93
  end
96
-
97
94
  end
98
95
  end
99
96
 
@@ -1,5 +1,4 @@
1
1
  require "webrat"
2
- gem "selenium-client", ">=1.2.16"
3
2
  require "selenium/client"
4
3
  require "webrat/selenium/silence_stream"
5
4
  require "webrat/selenium/selenium_session"
@@ -43,7 +43,7 @@ module Webrat
43
43
 
44
44
  def fill_in(field_identifier, options)
45
45
  locator = "webrat=#{field_identifier}"
46
- selenium.wait_for_element locator, :timeout_in_seconds => 5
46
+ selenium.wait_for_element locator, :timeout_in_seconds => Webrat.configuration.selenium_timeout
47
47
  selenium.type(locator, "#{options[:with]}")
48
48
  end
49
49
 
@@ -70,7 +70,7 @@ module Webrat
70
70
  pattern ||= '*'
71
71
  locator = "button=#{pattern}"
72
72
 
73
- selenium.wait_for_element locator, :timeout_in_seconds => 5
73
+ selenium.wait_for_element locator, :timeout_in_seconds => Webrat.configuration.selenium_timeout
74
74
  selenium.click locator
75
75
  end
76
76
 
@@ -79,7 +79,7 @@ module Webrat
79
79
  def click_link(link_text_or_regexp, options = {})
80
80
  pattern = adjust_if_regexp(link_text_or_regexp)
81
81
  locator = "webratlink=#{pattern}"
82
- selenium.wait_for_element locator, :timeout_in_seconds => 5
82
+ selenium.wait_for_element locator, :timeout_in_seconds => Webrat.configuration.selenium_timeout
83
83
  selenium.click locator
84
84
  end
85
85
 
@@ -87,7 +87,7 @@ module Webrat
87
87
 
88
88
  def click_link_within(selector, link_text, options = {})
89
89
  locator = "webratlinkwithin=#{selector}|#{link_text}"
90
- selenium.wait_for_element locator, :timeout_in_seconds => 5
90
+ selenium.wait_for_element locator, :timeout_in_seconds => Webrat.configuration.selenium_timeout
91
91
  selenium.click locator
92
92
  end
93
93
 
@@ -102,7 +102,7 @@ module Webrat
102
102
  select_locator = "webratselectwithoption=#{option_text}"
103
103
  end
104
104
 
105
- selenium.wait_for_element select_locator, :timeout_in_seconds => 5
105
+ selenium.wait_for_element select_locator, :timeout_in_seconds => Webrat.configuration.selenium_timeout
106
106
  selenium.select(select_locator, option_text)
107
107
  end
108
108
 
@@ -110,7 +110,7 @@ module Webrat
110
110
 
111
111
  def choose(label_text)
112
112
  locator = "webrat=#{label_text}"
113
- selenium.wait_for_element locator, :timeout_in_seconds => 5
113
+ selenium.wait_for_element locator, :timeout_in_seconds => Webrat.configuration.selenium_timeout
114
114
  selenium.click locator
115
115
  end
116
116
 
@@ -118,7 +118,7 @@ module Webrat
118
118
 
119
119
  def check(label_text)
120
120
  locator = "webrat=#{label_text}"
121
- selenium.wait_for_element locator, :timeout_in_seconds => 5
121
+ selenium.wait_for_element locator, :timeout_in_seconds => Webrat.configuration.selenium_timeout
122
122
  selenium.click locator
123
123
  end
124
124
  alias_method :uncheck, :check
@@ -141,7 +141,7 @@ module Webrat
141
141
  end
142
142
 
143
143
  def wait_for(params={})
144
- timeout = params[:timeout] || 5
144
+ timeout = params[:timeout] || Webrat.configuration.selenium_timeout
145
145
  message = params[:message] || "Timeout exceeded"
146
146
 
147
147
  begin_time = Time.now
@@ -1,44 +1,11 @@
1
1
  require "webrat/rack"
2
- require "sinatra/test"
3
2
 
4
3
  module Webrat
5
- class SinatraSession < RackSession
6
- include Sinatra::Test
7
-
8
- attr_reader :request, :response
9
-
10
- def initialize(context = nil)
11
- super(context)
12
-
4
+ class SinatraAdapter < RackAdapter
5
+ def initialize(context)
13
6
  app = context.respond_to?(:app) ? context.app : Sinatra::Application
14
- @browser = Sinatra::TestHarness.new(app)
15
- end
16
-
17
- %w(get head post put delete).each do |verb|
18
- class_eval <<-RUBY
19
- def #{verb}(path, data, headers = {})
20
- params = data.inject({}) do |data, (key,value)|
21
- data[key] = Rack::Utils.unescape(value)
22
- data
23
- end
24
- headers["HTTP_HOST"] = "www.example.com"
25
- @browser.#{verb}(path, params, headers)
26
- end
27
- RUBY
28
- end
29
-
30
- def response_body
31
- @browser.body
32
- end
33
7
 
34
- def response_code
35
- @browser.status
8
+ super(Rack::Test::Session.new(Rack::MockSession.new(app, "www.example.com")))
36
9
  end
37
-
38
- private
39
-
40
- def response
41
- @browser.response
42
- end
43
10
  end
44
11
  end