brynary-webrat 0.3.2.2 → 0.4.0

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.
@@ -0,0 +1,65 @@
1
+ require "webrat"
2
+
3
+ require "cgi"
4
+ gem "extlib"
5
+ require "extlib"
6
+ require "merb-core"
7
+
8
+ HashWithIndifferentAccess = Mash
9
+
10
+ module Webrat
11
+ class MerbSession < Session #:nodoc:
12
+ include Merb::Test::MakeRequest
13
+
14
+ attr_accessor :response
15
+
16
+ def get(url, data, headers = nil)
17
+ do_request(url, data, headers, "GET")
18
+ end
19
+
20
+ def post(url, data, headers = nil)
21
+ do_request(url, data, headers, "POST")
22
+ end
23
+
24
+ def put(url, data, headers = nil)
25
+ do_request(url, data, headers, "PUT")
26
+ end
27
+
28
+ def delete(url, data, headers = nil)
29
+ do_request(url, data, headers, "DELETE")
30
+ end
31
+
32
+ def response_body
33
+ @response.body.to_s
34
+ end
35
+
36
+ def response_code
37
+ @response.status
38
+ end
39
+
40
+ def do_request(url, data, headers, method)
41
+ @response = request(url,
42
+ :params => (data && data.any?) ? data : nil,
43
+ :headers => headers,
44
+ :method => method)
45
+ end
46
+
47
+ end
48
+ end
49
+
50
+ module Merb #:nodoc:
51
+ module Test #:nodoc:
52
+ module RequestHelper #:nodoc:
53
+ def request(uri, env = {})
54
+ @_webrat_session ||= Webrat::MerbSession.new
55
+ @_webrat_session.response = @_webrat_session.request(uri, env)
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ class Merb::Test::RspecStory #:nodoc:
62
+ def browser
63
+ @browser ||= Webrat::MerbSession.new
64
+ end
65
+ end
@@ -22,5 +22,3 @@ module Webrat
22
22
  end
23
23
  end
24
24
  end
25
-
26
- Webrat.configuration.mode = :rack
@@ -1,57 +1,75 @@
1
1
  require "webrat"
2
+
3
+ require "action_controller"
2
4
  require "action_controller/integration"
5
+ require "action_controller/record_identifier"
3
6
 
4
7
  module Webrat
5
8
  class RailsSession < Session #:nodoc:
6
-
9
+ include ActionController::RecordIdentifier
10
+
11
+ # The Rails version of within supports passing in a model and Webrat
12
+ # will apply a scope based on Rails' dom_id for that model.
13
+ #
14
+ # Example:
15
+ # within User.last do
16
+ # click_link "Delete"
17
+ # end
18
+ def within(selector_or_object, &block)
19
+ if selector_or_object.is_a?(String)
20
+ super
21
+ else
22
+ super('#' + dom_id(selector_or_object), &block)
23
+ end
24
+ end
25
+
7
26
  def doc_root
8
27
  File.expand_path(File.join(RAILS_ROOT, 'public'))
9
28
  end
10
-
29
+
11
30
  def saved_page_dir
12
31
  File.expand_path(File.join(RAILS_ROOT, "tmp"))
13
32
  end
14
-
33
+
15
34
  def get(url, data, headers = nil)
16
35
  do_request(:get, url, data, headers)
17
36
  end
18
-
37
+
19
38
  def post(url, data, headers = nil)
20
39
  do_request(:post, url, data, headers)
21
40
  end
22
-
41
+
23
42
  def put(url, data, headers = nil)
24
43
  do_request(:put, url, data, headers)
25
44
  end
26
-
45
+
27
46
  def delete(url, data, headers = nil)
28
47
  do_request(:delete, url, data, headers)
29
48
  end
30
-
49
+
31
50
  def response_body
32
51
  response.body
33
52
  end
34
-
53
+
35
54
  def response_code
36
55
  response.code.to_i
37
56
  end
38
-
57
+
39
58
  def xml_content_type?
40
59
  response.headers["Content-Type"].to_s =~ /xml/
41
60
  end
42
-
61
+
43
62
  protected
44
-
63
+
45
64
  def integration_session
46
65
  @context
47
66
  end
48
-
67
+
49
68
  def do_request(http_method, url, data, headers) #:nodoc:
50
69
  update_protocol(url)
51
- url = normalize_url(url)
52
- integration_session.request_via_redirect(http_method, url, data, headers)
70
+ integration_session.send(http_method, normalize_url(url), data, headers)
53
71
  end
54
-
72
+
55
73
  # remove protocol, host and anchor
56
74
  def normalize_url(href) #:nodoc:
57
75
  uri = URI.parse(href)
@@ -61,7 +79,7 @@ module Webrat
61
79
  end
62
80
  normalized_url
63
81
  end
64
-
82
+
65
83
  def update_protocol(href) #:nodoc:
66
84
  if href =~ /^https:/
67
85
  integration_session.https!(true)
@@ -69,28 +87,17 @@ module Webrat
69
87
  integration_session.https!(false)
70
88
  end
71
89
  end
72
-
90
+
73
91
  def response #:nodoc:
74
92
  integration_session.response
75
93
  end
76
-
94
+
77
95
  end
78
96
  end
79
97
 
80
98
  module ActionController #:nodoc:
81
- module Integration #:nodoc:
82
- Session.class_eval do
83
- unless instance_methods.include?("put_via_redirect")
84
- require "webrat/rails/redirect_actions"
85
- include Webrat::RedirectActions
86
- end
87
- end
88
- end
89
-
90
99
  IntegrationTest.class_eval do
91
100
  include Webrat::Methods
92
101
  include Webrat::Matchers
93
102
  end
94
103
  end
95
-
96
- Webrat.configuration.mode = :rails
@@ -4,50 +4,45 @@ require "selenium/client"
4
4
  require "webrat/selenium/selenium_session"
5
5
  require "webrat/selenium/matchers"
6
6
 
7
- Webrat.configuration.mode = :selenium
8
-
9
7
  module Webrat
10
-
8
+
11
9
  def self.with_selenium_server #:nodoc:
12
10
  start_selenium_server
13
11
  yield
14
12
  stop_selenium_server
15
13
  end
16
-
14
+
17
15
  def self.start_selenium_server #:nodoc:
18
- remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 4444, 5)
19
- remote_control.jar_file = File.expand_path(__FILE__ + "../../../../vendor/selenium-server.jar")
20
- remote_control.start :background => true
21
- TCPSocket.wait_for_service :host => "0.0.0.0", :port => 4444
16
+ unless Webrat.configuration.selenium_server_address
17
+ remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", Webrat.configuration.selenium_server_port, 5)
18
+ remote_control.jar_file = File.expand_path(__FILE__ + "../../../../vendor/selenium-server.jar")
19
+ remote_control.start :background => true
20
+ end
21
+ TCPSocket.wait_for_service :host => (Webrat.configuration.selenium_server_address || "0.0.0.0"), :port => Webrat.configuration.selenium_server_port
22
22
  end
23
-
23
+
24
24
  def self.stop_selenium_server #:nodoc:
25
- remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", 4444, 5)
26
- remote_control.stop
25
+ ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", Webrat.configuration.selenium_server_port, 5).stop unless Webrat.configuration.selenium_server_address
27
26
  end
28
-
27
+
29
28
  def self.start_app_server #:nodoc:
30
29
  pid_file = File.expand_path(RAILS_ROOT + "/tmp/pids/mongrel_selenium.pid")
31
- system("mongrel_rails start -d --chdir=#{RAILS_ROOT} --port=3001 --environment=selenium --pid #{pid_file} &")
32
- TCPSocket.wait_for_service :host => "0.0.0.0", :port => 3001
30
+ system("mongrel_rails start -d --chdir=#{RAILS_ROOT} --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.application_environment} --pid #{pid_file} &")
31
+ TCPSocket.wait_for_service :host => Webrat.configuration.application_address, :port => Webrat.configuration.application_port.to_i
33
32
  end
34
-
33
+
35
34
  def self.stop_app_server #:nodoc:
36
35
  pid_file = File.expand_path(RAILS_ROOT + "/tmp/pids/mongrel_selenium.pid")
37
36
  system "mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}"
38
37
  end
39
-
38
+
40
39
  # To use Webrat's Selenium support, you'll need the selenium-client gem installed.
41
40
  # Activate it with (for example, in your <tt>env.rb</tt>):
42
41
  #
43
- # require "webrat/selenium"
44
- #
45
- # Then, if you're using Cucumber, configure it to use a
46
- # <tt>Webrat::Selenium::Rails::World</tt> as the scenario context by adding
47
- # the following to <tt>env.rb</tt>:
42
+ # require "webrat"
48
43
  #
49
- # World do
50
- # Webrat::Selenium::Rails::World.new
44
+ # Webrat.configure do |config|
45
+ # config.mode = :selenium
51
46
  # end
52
47
  #
53
48
  # == Dropping down to the selenium-client API
@@ -74,34 +69,26 @@ module Webrat
74
69
  # your Webrat::Selenium tests ignoring the concurrency issues that can plague in-browser
75
70
  # testing, so long as you're using the Webrat API.
76
71
  module Selenium
77
-
78
- module Rails #:nodoc:
79
- class World < ::ActionController::IntegrationTest
80
- include Webrat::Selenium::Matchers
81
-
82
- def initialize #:nodoc:
83
- @_result = Test::Unit::TestResult.new
84
- end
85
-
86
- def response
87
- webrat_session.response
88
- end
89
-
90
- def wait_for(*args, &block)
91
- webrat_session.wait_for(*args, &block)
92
- end
72
+ module Methods
73
+ def response
74
+ webrat_session.response
75
+ end
76
+
77
+ def wait_for(*args, &block)
78
+ webrat_session.wait_for(*args, &block)
79
+ end
93
80
 
94
- def save_and_open_screengrab
95
- webrat_session.save_and_open_screengrab
96
- end
81
+ def save_and_open_screengrab
82
+ webrat_session.save_and_open_screengrab
97
83
  end
98
84
  end
99
85
  end
100
-
101
86
  end
102
87
 
103
88
  module ActionController #:nodoc:
104
89
  IntegrationTest.class_eval do
105
90
  include Webrat::Methods
91
+ include Webrat::Selenium::Methods
92
+ include Webrat::Selenium::Matchers
106
93
  end
107
94
  end
@@ -30,6 +30,16 @@ module Webrat
30
30
  HaveXpath.new(xpath)
31
31
  end
32
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
+
33
43
  class HaveSelector
34
44
  def initialize(expected)
35
45
  @expected = expected
@@ -58,6 +68,20 @@ module Webrat
58
68
  HaveSelector.new(content)
59
69
  end
60
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
+
61
85
  class HasContent #:nodoc:
62
86
  def initialize(content)
63
87
  @content = content
@@ -103,6 +127,20 @@ module Webrat
103
127
  HasContent.new(content)
104
128
  end
105
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
+
106
144
  end
107
145
  end
108
146
  end
@@ -3,56 +3,56 @@ require "webrat/core/save_and_open_page"
3
3
  module Webrat
4
4
  class TimeoutError < WebratError
5
5
  end
6
-
6
+
7
7
  class SeleniumResponse
8
8
  attr_reader :body
9
9
  attr_reader :session
10
-
10
+
11
11
  def initialize(session, body)
12
12
  @session = session
13
13
  @body = body
14
14
  end
15
-
15
+
16
16
  def selenium
17
17
  session.selenium
18
18
  end
19
19
  end
20
-
20
+
21
21
  class SeleniumSession
22
22
  include Webrat::SaveAndOpenPage
23
-
23
+
24
24
  def initialize(*args) # :nodoc:
25
25
  end
26
-
26
+
27
27
  def simulate
28
28
  end
29
-
29
+
30
30
  def automate
31
31
  yield
32
32
  end
33
-
33
+
34
34
  def visit(url)
35
35
  selenium.open(url)
36
36
  end
37
-
37
+
38
38
  webrat_deprecate :visits, :visit
39
-
39
+
40
40
  def fill_in(field_identifier, options)
41
41
  locator = "webrat=#{Regexp.escape(field_identifier)}"
42
42
  selenium.wait_for_element locator, 5
43
43
  selenium.type(locator, "#{options[:with]}")
44
44
  end
45
-
45
+
46
46
  webrat_deprecate :fills_in, :fill_in
47
-
47
+
48
48
  def response
49
49
  SeleniumResponse.new(self, response_body)
50
50
  end
51
-
51
+
52
52
  def response_body #:nodoc:
53
53
  selenium.get_html_source
54
54
  end
55
-
55
+
56
56
  def click_button(button_text_or_regexp = nil, options = {})
57
57
  if button_text_or_regexp.is_a?(Hash) && options == {}
58
58
  pattern, options = nil, button_text_or_regexp
@@ -61,11 +61,11 @@ module Webrat
61
61
  end
62
62
  pattern ||= '*'
63
63
  locator = "button=#{pattern}"
64
-
64
+
65
65
  selenium.wait_for_element locator, 5
66
66
  selenium.click locator
67
67
  end
68
-
68
+
69
69
  webrat_deprecate :clicks_button, :click_button
70
70
 
71
71
  def click_link(link_text_or_regexp, options = {})
@@ -74,53 +74,53 @@ module Webrat
74
74
  selenium.wait_for_element locator, 5
75
75
  selenium.click locator
76
76
  end
77
-
77
+
78
78
  webrat_deprecate :clicks_link, :click_link
79
-
79
+
80
80
  def click_link_within(selector, link_text, options = {})
81
81
  locator = "webratlinkwithin=#{selector}|#{link_text}"
82
82
  selenium.wait_for_element locator, 5
83
83
  selenium.click locator
84
84
  end
85
-
85
+
86
86
  webrat_deprecate :clicks_link_within, :click_link_within
87
-
87
+
88
88
  def select(option_text, options = {})
89
89
  id_or_name_or_label = options[:from]
90
-
90
+
91
91
  if id_or_name_or_label
92
92
  select_locator = "webrat=#{id_or_name_or_label}"
93
93
  else
94
94
  select_locator = "webratselectwithoption=#{option_text}"
95
95
  end
96
-
96
+
97
97
  selenium.wait_for_element select_locator, 5
98
98
  selenium.select(select_locator, option_text)
99
99
  end
100
-
100
+
101
101
  webrat_deprecate :selects, :select
102
-
102
+
103
103
  def choose(label_text)
104
104
  locator = "webrat=#{label_text}"
105
105
  selenium.wait_for_element locator, 5
106
106
  selenium.click locator
107
107
  end
108
-
108
+
109
109
  webrat_deprecate :chooses, :choose
110
-
110
+
111
111
  def check(label_text)
112
112
  locator = "webrat=#{label_text}"
113
113
  selenium.wait_for_element locator, 5
114
114
  selenium.check locator
115
115
  end
116
-
116
+
117
117
  webrat_deprecate :checks, :check
118
118
 
119
119
  def fire_event(field_identifier, event)
120
120
  locator = "webrat=#{Regexp.escape(field_identifier)}"
121
121
  selenium.fire_event(locator, "#{event}")
122
122
  end
123
-
123
+
124
124
  def key_down(field_identifier, key_code)
125
125
  locator = "webrat=#{Regexp.escape(field_identifier)}"
126
126
  selenium.key_down(locator, key_code)
@@ -130,7 +130,7 @@ module Webrat
130
130
  locator = "webrat=#{Regexp.escape(field_identifier)}"
131
131
  selenium.key_up(locator, key_code)
132
132
  end
133
-
133
+
134
134
  def wait_for(params={})
135
135
  timeout = params[:timeout] || 5
136
136
  message = params[:message] || "Timeout exceeded"
@@ -154,21 +154,21 @@ module Webrat
154
154
  raise Webrat::TimeoutError.new(message + " (after #{timeout} sec)")
155
155
  true
156
156
  end
157
-
157
+
158
158
  def selenium
159
159
  return $browser if $browser
160
160
  setup
161
161
  $browser
162
162
  end
163
-
163
+
164
164
  webrat_deprecate :browser, :selenium
165
-
166
-
165
+
166
+
167
167
  def save_and_open_screengrab
168
168
  return unless File.exist?(saved_page_dir)
169
-
169
+
170
170
  filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.png"
171
-
171
+
172
172
  if $browser.chrome_backend?
173
173
  $browser.capture_entire_page_screenshot(filename, '')
174
174
  else
@@ -176,25 +176,31 @@ module Webrat
176
176
  end
177
177
  open_in_browser(filename)
178
178
  end
179
-
179
+
180
180
  protected
181
-
181
+
182
182
  def setup #:nodoc:
183
183
  silence_stream(STDOUT) do
184
184
  Webrat.start_selenium_server
185
185
  Webrat.start_app_server
186
186
  end
187
-
188
- $browser = ::Selenium::Client::Driver.new("localhost", 4444, "*firefox", "http://0.0.0.0:3001")
189
- $browser.set_speed(0)
187
+
188
+ create_browser
190
189
  $browser.start
191
190
  teardown_at_exit
192
-
191
+
193
192
  extend_selenium
194
193
  define_location_strategies
195
194
  $browser.window_maximize
196
195
  end
197
-
196
+
197
+
198
+ def create_browser
199
+ $browser = ::Selenium::Client::Driver.new(Webrat.configuration.selenium_server_address || "localhost",
200
+ Webrat.configuration.selenium_server_port, Webrat.configuration.selenium_browser_key, "http://#{Webrat.configuration.application_address}:#{Webrat.configuration.application_port}")
201
+ $browser.set_speed(0) unless Webrat.configuration.selenium_server_address
202
+ end
203
+
198
204
  def teardown_at_exit #:nodoc:
199
205
  at_exit do
200
206
  silence_stream(STDOUT) do
@@ -204,21 +210,21 @@ module Webrat
204
210
  end
205
211
  end
206
212
  end
207
-
213
+
208
214
  def adjust_if_regexp(text_or_regexp) #:nodoc:
209
215
  if text_or_regexp.is_a?(Regexp)
210
216
  "evalregex:#{text_or_regexp.inspect}"
211
217
  else
212
218
  "evalregex:/#{text_or_regexp}/"
213
- end
219
+ end
214
220
  end
215
-
221
+
216
222
  def extend_selenium #:nodoc:
217
223
  extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js")
218
224
  extenions_js = File.read(extensions_file)
219
225
  selenium.get_eval(extenions_js)
220
226
  end
221
-
227
+
222
228
  def define_location_strategies #:nodoc:
223
229
  Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
224
230
  strategy_js = File.read(file)