bbc-capybara-mechanize 0.3.0.rc0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.mdown +76 -0
- data/lib/capybara/mechanize/browser.rb +221 -0
- data/lib/capybara/mechanize/cucumber.rb +5 -0
- data/lib/capybara/mechanize/driver.rb +21 -0
- data/lib/capybara/mechanize/version.rb +5 -0
- data/lib/capybara/mechanize.rb +24 -0
- data/lib/capybara/spec/extended_test_app.rb +41 -0
- data/spec/driver/mechanize_driver_spec.rb +151 -0
- data/spec/driver/remote_mechanize_driver_spec.rb +58 -0
- data/spec/session/mechanize_spec.rb +53 -0
- data/spec/session/remote_mechanize_spec.rb +45 -0
- data/spec/spec_helper.rb +34 -0
- metadata +106 -0
data/README.mdown
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
Capybara-mechanize
|
2
|
+
==================
|
3
|
+
|
4
|
+
This gems makes it possible to use Capybara for (partially) remote testing. It inherits most functionality from the RackTest driver and only uses [Mechanize](http://github.com/tenderlove/mechanize) for remote requests.
|
5
|
+
|
6
|
+
It is currently in use to test the integration between a Rails application and Twitter authorization and sharing.
|
7
|
+
|
8
|
+
This gem is a [Capybara](http://github.com/jnicklas/capybara) extension. I have been inspired by the Capybara driver and some earlier efforts for a Mechanize driver.
|
9
|
+
|
10
|
+
Thanks to [Pinkelstar](http://www.pinkelstar.com) for giving me the time and the need to develop this gem.
|
11
|
+
|
12
|
+
### Installation
|
13
|
+
|
14
|
+
gem install capybara-mechanize
|
15
|
+
|
16
|
+
### Usage without Cucumber
|
17
|
+
|
18
|
+
require 'capybara/mechanize'
|
19
|
+
|
20
|
+
### Usage with Cucumber and tags
|
21
|
+
|
22
|
+
A @mechanize tag is added to your hooks when you add the following line to your env.rb
|
23
|
+
|
24
|
+
require 'capybara/mechanize/cucumber'
|
25
|
+
|
26
|
+
The following scenario will then be using the Mechanize driver
|
27
|
+
|
28
|
+
@mechanize
|
29
|
+
Scenario: do something remote
|
30
|
+
When I click the remote link
|
31
|
+
|
32
|
+
### Remote testing
|
33
|
+
|
34
|
+
When you want to use this driver to test a remote application. You have to set the app_host:
|
35
|
+
|
36
|
+
Capybara.app_host = "http://www.yourapp.com"
|
37
|
+
|
38
|
+
Note that I haven't tested this case for my self yet. The Capybara tests pass for this situation though so it should work! Please provide me with feedback if it doesn't.
|
39
|
+
|
40
|
+
## Running tests
|
41
|
+
|
42
|
+
Until this library is merged with capybara there needs to be local app and you need to add the following to your host file:
|
43
|
+
|
44
|
+
127.0.0.1 capybara-testapp.heroku.com
|
45
|
+
|
46
|
+
Run bundler
|
47
|
+
|
48
|
+
bundle install
|
49
|
+
|
50
|
+
Run the app with the following line:
|
51
|
+
|
52
|
+
bundle exec ruby -rrubygems lib/capybara/spec/extended_test_app.rb
|
53
|
+
|
54
|
+
Then you are ready to run the test like so
|
55
|
+
|
56
|
+
rake spec
|
57
|
+
|
58
|
+
Todo
|
59
|
+
----
|
60
|
+
* Make the last 12 failing remote session spec pass, see remote_mechanize_spec and uncomment one line there to see them fail
|
61
|
+
* Test this driver with non-rack/non-ruby projects
|
62
|
+
|
63
|
+
Note on Patches/Pull Requests
|
64
|
+
-----------------------------
|
65
|
+
|
66
|
+
* Fork the project.
|
67
|
+
* Make your feature addition or bug fix.
|
68
|
+
* Add tests for it. This is important so I don't break it in a
|
69
|
+
future version unintentionally.
|
70
|
+
* Commit, do not mess with rakefile, version, or history.
|
71
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
72
|
+
* Send me a pull request. Bonus points for topic branches.
|
73
|
+
|
74
|
+
Copyright
|
75
|
+
---------
|
76
|
+
Copyright (c) 2010 Jeroen van Dijk. See LICENSE for details.
|
@@ -0,0 +1,221 @@
|
|
1
|
+
require 'capybara/rack_test/driver'
|
2
|
+
require 'mechanize'
|
3
|
+
|
4
|
+
class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
def_delegator :agent, :scheme_handlers
|
8
|
+
def_delegator :agent, :scheme_handlers=
|
9
|
+
|
10
|
+
def initialize(app, options)
|
11
|
+
@agent = ::Mechanize.new
|
12
|
+
@agent.redirect_ok = false
|
13
|
+
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset_host!
|
18
|
+
@last_remote_host = nil
|
19
|
+
@last_request_remote = nil
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
def current_url
|
24
|
+
last_request_remote? ? remote_response.current_url : super
|
25
|
+
end
|
26
|
+
|
27
|
+
def last_response
|
28
|
+
last_request_remote? ? remote_response : super
|
29
|
+
end
|
30
|
+
|
31
|
+
def follow_redirects!
|
32
|
+
5.times do
|
33
|
+
follow_redirect! if last_response.redirect?
|
34
|
+
end
|
35
|
+
raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
|
36
|
+
end
|
37
|
+
|
38
|
+
def follow_redirect!
|
39
|
+
unless last_response.redirect?
|
40
|
+
raise "Last response was not a redirect. Cannot follow_redirect!"
|
41
|
+
end
|
42
|
+
|
43
|
+
get(last_location_header)
|
44
|
+
end
|
45
|
+
|
46
|
+
def process(method, path, *options)
|
47
|
+
reset_cache!
|
48
|
+
send(method, path, *options)
|
49
|
+
follow_redirects!
|
50
|
+
end
|
51
|
+
|
52
|
+
def process_without_redirect(method, path, attributes, headers)
|
53
|
+
path = @last_path if path.nil? || path.empty?
|
54
|
+
|
55
|
+
if remote?(path)
|
56
|
+
process_remote_request(method, path, attributes, headers)
|
57
|
+
else
|
58
|
+
register_local_request
|
59
|
+
|
60
|
+
path = determine_path(path)
|
61
|
+
|
62
|
+
reset_cache!
|
63
|
+
send("racktest_#{method}", path, attributes, env.merge(headers))
|
64
|
+
end
|
65
|
+
|
66
|
+
@last_path = path
|
67
|
+
end
|
68
|
+
|
69
|
+
# TODO path Capybara to move this into its own method
|
70
|
+
def determine_path(path)
|
71
|
+
new_uri = URI.parse(path)
|
72
|
+
current_uri = URI.parse(current_url)
|
73
|
+
|
74
|
+
if new_uri.host
|
75
|
+
@current_host = new_uri.scheme + '://' + new_uri.host
|
76
|
+
end
|
77
|
+
|
78
|
+
if new_uri.relative?
|
79
|
+
path = request_path + path if path.start_with?('?')
|
80
|
+
|
81
|
+
unless path.start_with?('/')
|
82
|
+
folders = request_path.split('/')
|
83
|
+
path = (folders[0, folders.size - 1] << path).join('/')
|
84
|
+
end
|
85
|
+
path = current_host + path
|
86
|
+
end
|
87
|
+
path
|
88
|
+
end
|
89
|
+
|
90
|
+
alias :racktest_get :get
|
91
|
+
def get(path, attributes = {}, headers = {})
|
92
|
+
process_without_redirect(:get, path, attributes, headers)
|
93
|
+
end
|
94
|
+
|
95
|
+
alias :racktest_post :post
|
96
|
+
def post(path, attributes = {}, headers = {})
|
97
|
+
process_without_redirect(:post, path, post_data(attributes), headers)
|
98
|
+
end
|
99
|
+
|
100
|
+
alias :racktest_put :put
|
101
|
+
def put(path, attributes = {}, headers = {})
|
102
|
+
process_without_redirect(:put, path, attributes, headers)
|
103
|
+
end
|
104
|
+
|
105
|
+
alias :racktest_delete :delete
|
106
|
+
def delete(path, attributes = {}, headers = {})
|
107
|
+
process_without_redirect(:delete, path, attributes, headers)
|
108
|
+
end
|
109
|
+
|
110
|
+
def post_data(params)
|
111
|
+
params.inject({}) do |memo, param|
|
112
|
+
case param
|
113
|
+
when Hash
|
114
|
+
param.each {|attribute, value| memo[attribute] = value }
|
115
|
+
memo
|
116
|
+
when Array
|
117
|
+
case param.last
|
118
|
+
when Hash
|
119
|
+
param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
|
120
|
+
else
|
121
|
+
memo[param.first] = param.last
|
122
|
+
end
|
123
|
+
memo
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def remote?(url)
|
129
|
+
if Capybara.app_host
|
130
|
+
true
|
131
|
+
else
|
132
|
+
host = URI.parse(url).host
|
133
|
+
|
134
|
+
if host.nil?
|
135
|
+
last_request_remote?
|
136
|
+
else
|
137
|
+
!Capybara::Mechanize.local_hosts.include?(host)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
attr_reader :agent
|
143
|
+
|
144
|
+
private
|
145
|
+
|
146
|
+
def last_location_header
|
147
|
+
last_request_remote? ? remote_response.page.response['Location'] : last_response['Location']
|
148
|
+
end
|
149
|
+
|
150
|
+
def last_request_remote?
|
151
|
+
!!@last_request_remote
|
152
|
+
end
|
153
|
+
|
154
|
+
def register_local_request
|
155
|
+
@last_remote_host = nil
|
156
|
+
@last_request_remote = false
|
157
|
+
end
|
158
|
+
|
159
|
+
def process_remote_request(method, url, attributes, headers)
|
160
|
+
if remote?(url)
|
161
|
+
remote_uri = URI.parse(url)
|
162
|
+
|
163
|
+
if remote_uri.host.nil?
|
164
|
+
remote_host = @last_remote_host || Capybara.app_host || Capybara.default_host
|
165
|
+
url = File.join(remote_host, url)
|
166
|
+
url = "http://#{url}" unless url.include?("http")
|
167
|
+
else
|
168
|
+
@last_remote_host = "#{remote_uri.host}:#{remote_uri.port}"
|
169
|
+
end
|
170
|
+
|
171
|
+
reset_cache!
|
172
|
+
begin
|
173
|
+
args = []
|
174
|
+
args << attributes unless attributes.empty?
|
175
|
+
args << headers unless headers.empty?
|
176
|
+
@agent.send(method, url, *args)
|
177
|
+
rescue => e
|
178
|
+
raise "Received the following error for a #{method.to_s.upcase} request to #{url}: '#{e.message}'"
|
179
|
+
end
|
180
|
+
@last_request_remote = true
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def remote_response
|
185
|
+
ResponseProxy.new(@agent.current_page) if @agent.current_page
|
186
|
+
end
|
187
|
+
|
188
|
+
class ResponseProxy
|
189
|
+
extend Forwardable
|
190
|
+
|
191
|
+
def_delegator :page, :body
|
192
|
+
|
193
|
+
attr_reader :page
|
194
|
+
|
195
|
+
def initialize(page)
|
196
|
+
@page = page
|
197
|
+
end
|
198
|
+
|
199
|
+
def current_url
|
200
|
+
page.uri.to_s
|
201
|
+
end
|
202
|
+
|
203
|
+
def headers
|
204
|
+
# Hax the content-type contains utf8, so Capybara specs are failing, need to ask mailinglist
|
205
|
+
headers = page.response
|
206
|
+
headers["content-type"].gsub!(';charset=utf-8', '') if headers["content-type"]
|
207
|
+
headers
|
208
|
+
end
|
209
|
+
|
210
|
+
def status
|
211
|
+
page.code.to_i
|
212
|
+
end
|
213
|
+
|
214
|
+
def redirect?
|
215
|
+
[301, 302].include?(status)
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'capybara/mechanize/browser'
|
2
|
+
|
3
|
+
class Capybara::Mechanize::Driver < Capybara::RackTest::Driver
|
4
|
+
|
5
|
+
def initialize(app = nil, options = {})
|
6
|
+
if !app && !Capybara.app_host
|
7
|
+
raise ArgumentError, "You have to set at least Capybara.app_host or Capybara.app"
|
8
|
+
end
|
9
|
+
|
10
|
+
@app, @options = app, options
|
11
|
+
end
|
12
|
+
|
13
|
+
def remote?(url)
|
14
|
+
browser.remote?(url)
|
15
|
+
end
|
16
|
+
|
17
|
+
def browser
|
18
|
+
@browser ||= Capybara::Mechanize::Browser.new(app, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'capybara'
|
2
|
+
|
3
|
+
module Capybara::Mechanize
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# Host that should be considered local (includes default_host)
|
7
|
+
def local_hosts
|
8
|
+
@local_hosts ||= begin
|
9
|
+
default_host = URI.parse(Capybara.default_host || "").host || Capybara.default_host
|
10
|
+
[default_host].compact
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def local_hosts=(hosts)
|
15
|
+
@local_hosts = hosts
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'capybara/mechanize/driver'
|
21
|
+
|
22
|
+
Capybara.register_driver :mechanize do |app|
|
23
|
+
Capybara::Mechanize::Driver.new(app)
|
24
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'capybara/spec/test_app'
|
2
|
+
|
3
|
+
class ExtendedTestApp < TestApp#< Sinatra::Base
|
4
|
+
set :environment, :production # so we don't get debug info that makes our test pass!
|
5
|
+
|
6
|
+
get %r{/redirect_to/(.*)} do
|
7
|
+
redirect params[:captures]
|
8
|
+
end
|
9
|
+
|
10
|
+
get '/form_with_relative_action_to_host' do
|
11
|
+
%{<form action="/request_info/host" method="post">
|
12
|
+
<input type="submit" value="submit" />
|
13
|
+
</form>}
|
14
|
+
end
|
15
|
+
|
16
|
+
get '/request_info/form_with_no_action' do
|
17
|
+
%{<form method="post">
|
18
|
+
<input type="submit" value="submit" />
|
19
|
+
</form>}
|
20
|
+
end
|
21
|
+
|
22
|
+
get '/relative_link_to_host' do
|
23
|
+
%{<a href="/request_info/host">host</a>}
|
24
|
+
end
|
25
|
+
|
26
|
+
get '/request_info/*' do
|
27
|
+
current_request_info
|
28
|
+
end
|
29
|
+
|
30
|
+
post '/request_info/*' do
|
31
|
+
current_request_info
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def current_request_info
|
38
|
+
"Current host is #{request.url}, method #{request.request_method.downcase}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Capybara::Driver::Mechanize, in local model" do
|
4
|
+
before do
|
5
|
+
@driver = Capybara::Mechanize::Driver.new(ExtendedTestApp)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should throw an error when no rack app is given without an app host" do
|
9
|
+
running do
|
10
|
+
Capybara::Mechanize::Driver.new
|
11
|
+
end.should raise_error(ArgumentError, "You have to set at least Capybara.app_host or Capybara.app")
|
12
|
+
end
|
13
|
+
|
14
|
+
it_should_behave_like "driver"
|
15
|
+
it_should_behave_like "driver with header support"
|
16
|
+
it_should_behave_like "driver with status code support"
|
17
|
+
it_should_behave_like "driver with cookies support"
|
18
|
+
it_should_behave_like "driver with infinite redirect detection"
|
19
|
+
|
20
|
+
it "should default to local mode for relative paths" do
|
21
|
+
@driver.should_not be_remote('/')
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should default to local mode for the default host" do
|
25
|
+
@driver.should_not be_remote('http://www.example.com')
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with an app_host" do
|
29
|
+
|
30
|
+
before do
|
31
|
+
Capybara.app_host = 'http://www.remote.com'
|
32
|
+
end
|
33
|
+
|
34
|
+
after do
|
35
|
+
Capybara.app_host = nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should treat urls as remote" do
|
39
|
+
@driver.should be_remote('http://www.remote.com')
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "with a default url, no app host" do
|
44
|
+
before :each do
|
45
|
+
Capybara.default_host = 'www.local.com'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should allow local hosts to be set" do
|
49
|
+
Capybara::Mechanize.local_hosts = ['subdomain.local.com']
|
50
|
+
@driver.should_not be_remote('http://subdomain.local.com')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should treat urls with the same host names as local" do
|
54
|
+
@driver.should_not be_remote('http://www.local.com')
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should treat other urls as remote" do
|
58
|
+
@driver.should be_remote('http://www.remote.com')
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should treat relative paths as remote if the previous request was remote" do
|
62
|
+
@driver.visit(REMOTE_TEST_URL)
|
63
|
+
@driver.should be_remote('/some_relative_link')
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should treat relative paths as local if the previous request was local" do
|
67
|
+
@driver.visit('http://www.local.com')
|
68
|
+
@driver.should_not be_remote('/some_relative_link')
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should receive the right host" do
|
72
|
+
@driver.visit('http://www.local.com/host')
|
73
|
+
should_be_a_local_get
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should consider relative paths to be local when the previous request was local" do
|
77
|
+
@driver.visit('http://www.local.com/host')
|
78
|
+
@driver.visit('/host')
|
79
|
+
|
80
|
+
should_be_a_local_get
|
81
|
+
@driver.should_not be_remote('/first_local')
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should consider relative paths to be remote when the previous request was remote" do
|
85
|
+
@driver.visit("#{REMOTE_TEST_URL}/host")
|
86
|
+
@driver.get('/host')
|
87
|
+
|
88
|
+
should_be_a_remote_get
|
89
|
+
@driver.should be_remote('/second_remote')
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should always switch to the right context" do
|
93
|
+
@driver.visit('http://www.local.com/host')
|
94
|
+
@driver.get('/host')
|
95
|
+
@driver.get("#{REMOTE_TEST_URL}/host")
|
96
|
+
@driver.get('/host')
|
97
|
+
@driver.get('http://www.local.com/host')
|
98
|
+
|
99
|
+
should_be_a_local_get
|
100
|
+
@driver.should_not be_remote('/second_local')
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should follow redirects from local to remote" do
|
104
|
+
@driver.visit("http://www.local.com/redirect_to/#{REMOTE_TEST_URL}/host")
|
105
|
+
should_be_a_remote_get
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should follow redirects from remote to local" do
|
109
|
+
@driver.visit("#{REMOTE_TEST_URL}/redirect_to/http://www.local.com/host")
|
110
|
+
should_be_a_local_get
|
111
|
+
end
|
112
|
+
|
113
|
+
after :each do
|
114
|
+
Capybara.default_host = nil
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should raise a useful error for sites that return a 404, because it is probably a misconfiguration" do
|
118
|
+
lambda {
|
119
|
+
@driver.visit("http://iamreallysurethatthisdoesntexist.com/canttouchthis")
|
120
|
+
}.should raise_error(%r{Received the following error for a GET request to http://iamreallysurethatthisdoesntexist.com/canttouchthis:})
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should include the right host when remote" do
|
125
|
+
@driver.visit("#{REMOTE_TEST_URL}/host")
|
126
|
+
should_be_a_remote_get
|
127
|
+
end
|
128
|
+
|
129
|
+
describe '#reset!' do
|
130
|
+
before :each do
|
131
|
+
Capybara.default_host = 'http://www.local.com'
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should reset remote host' do
|
135
|
+
@driver.visit("#{REMOTE_TEST_URL}/host")
|
136
|
+
should_be_a_remote_get
|
137
|
+
@driver.reset!
|
138
|
+
@driver.visit("/host")
|
139
|
+
should_be_a_local_get
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def should_be_a_remote_get
|
144
|
+
@driver.body.should include(REMOTE_TEST_URL)
|
145
|
+
end
|
146
|
+
|
147
|
+
def should_be_a_local_get
|
148
|
+
@driver.body.should include("www.local.com")
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Mechanize::Driver do
|
4
|
+
before(:each) do
|
5
|
+
Capybara.app_host = REMOTE_TEST_URL
|
6
|
+
end
|
7
|
+
|
8
|
+
after(:each) do
|
9
|
+
Capybara.app_host = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
before do
|
13
|
+
@driver = Capybara::Mechanize::Driver.new
|
14
|
+
end
|
15
|
+
|
16
|
+
context "in remote mode" do
|
17
|
+
it "should not throw an error when no rack app is given" do
|
18
|
+
running do
|
19
|
+
Capybara::Mechanize::Driver.new
|
20
|
+
end.should_not raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should pass arguments through to a get request" do
|
24
|
+
@driver.visit("#{REMOTE_TEST_URL}/form/get", {:form => "success"})
|
25
|
+
@driver.body.should include('success')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should pass arguments through to a post request" do
|
29
|
+
@driver.post("#{REMOTE_TEST_URL}/form", {:form => "success"})
|
30
|
+
@driver.body.should include('success')
|
31
|
+
end
|
32
|
+
|
33
|
+
context "for a post request" do
|
34
|
+
|
35
|
+
it "should transform nested map in post data" do
|
36
|
+
@driver.post("#{REMOTE_TEST_URL}/form", {:form => {:key => "value"}})
|
37
|
+
@driver.body.should include('key: value')
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
context "process remote request" do
|
43
|
+
|
44
|
+
it "should transform nested map in post data" do
|
45
|
+
@driver.submit(:post, "#{REMOTE_TEST_URL}/form", {:form => {:key => "value"}})
|
46
|
+
@driver.body.should include('key: value')
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
it_should_behave_like "driver"
|
52
|
+
it_should_behave_like "driver with header support"
|
53
|
+
it_should_behave_like "driver with status code support"
|
54
|
+
it_should_behave_like "driver with cookies support"
|
55
|
+
it_should_behave_like "driver with infinite redirect detection"
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Session do
|
4
|
+
context 'with mechanize driver' do
|
5
|
+
before do
|
6
|
+
@session = Capybara::Session.new(:mechanize, TestApp)
|
7
|
+
Capybara.default_host = 'http://www.local.com'
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#driver' do
|
11
|
+
it "should be a mechanize driver" do
|
12
|
+
@session.driver.should be_an_instance_of(Capybara::Mechanize::Driver)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#mode' do
|
17
|
+
it "should remember the mode" do
|
18
|
+
@session.mode.should == :mechanize
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#click_link' do
|
23
|
+
it "should use data-method if available" do
|
24
|
+
@session.visit "/with_html"
|
25
|
+
@session.click_link "A link with data-method"
|
26
|
+
@session.body.should include('The requested object was deleted')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should use the last remote url when following relative links" do
|
31
|
+
@session.visit("#{REMOTE_TEST_URL}/relative_link_to_host")
|
32
|
+
@session.click_link "host"
|
33
|
+
@session.body.should include("Current host is #{REMOTE_TEST_URL}/request_info/host, method get")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should use the last remote url when submitting a form with a relative action" do
|
37
|
+
@session.visit("#{REMOTE_TEST_URL}/form_with_relative_action_to_host")
|
38
|
+
@session.click_button "submit"
|
39
|
+
@session.body.should include("Current host is #{REMOTE_TEST_URL}/request_info/host, method post")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should use the last url when submitting a form with no action" do
|
43
|
+
@session.visit("#{REMOTE_TEST_URL}/request_info/form_with_no_action")
|
44
|
+
@session.click_button "submit"
|
45
|
+
@session.body.should include("Current host is #{REMOTE_TEST_URL}/request_info/form_with_no_action, method post")
|
46
|
+
end
|
47
|
+
|
48
|
+
it_should_behave_like "session"
|
49
|
+
it_should_behave_like "session without javascript support"
|
50
|
+
it_should_behave_like "session with headers support"
|
51
|
+
it_should_behave_like "session with status code support"
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Capybara::Session do
|
4
|
+
context 'with remote mechanize driver' do
|
5
|
+
before(:each) do
|
6
|
+
Capybara.app_host = REMOTE_TEST_URL
|
7
|
+
end
|
8
|
+
|
9
|
+
after(:each) do
|
10
|
+
Capybara.app_host = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
before do
|
15
|
+
@session = Capybara::Session.new(:mechanize)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#driver' do
|
19
|
+
it "should be a mechanize driver" do
|
20
|
+
@session.driver.should be_an_instance_of(Capybara::Mechanize::Driver)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#mode' do
|
25
|
+
it "should remember the mode" do
|
26
|
+
@session.mode.should == :mechanize
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#click_link' do
|
31
|
+
it "should use data-method if available" do
|
32
|
+
@session.visit "/with_html"
|
33
|
+
@session.click_link "A link with data-method"
|
34
|
+
@session.body.should include('The requested object was deleted')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Pending: Still 16 failing tests here (result is 706 examples, 16 failures, instead of 385 examples)
|
39
|
+
# it_should_behave_like "session"
|
40
|
+
|
41
|
+
it_should_behave_like "session without javascript support"
|
42
|
+
it_should_behave_like "session with headers support"
|
43
|
+
it_should_behave_like "session with status code support"
|
44
|
+
end
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'capybara'
|
3
|
+
require 'capybara/mechanize'
|
4
|
+
require 'artifice'
|
5
|
+
|
6
|
+
require 'sinatra'
|
7
|
+
|
8
|
+
require 'capybara/spec/extended_test_app'
|
9
|
+
|
10
|
+
# TODO move this stuff into capybara
|
11
|
+
require 'capybara/spec/driver'
|
12
|
+
require 'capybara/spec/session'
|
13
|
+
|
14
|
+
alias :running :lambda
|
15
|
+
|
16
|
+
Capybara.default_wait_time = 0 # less timeout so tests run faster
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
config.before(:all) do
|
20
|
+
Artifice.activate_with(ExtendedTestApp)
|
21
|
+
end
|
22
|
+
|
23
|
+
config.after do
|
24
|
+
Capybara.default_selector = :xpath
|
25
|
+
Capybara::Mechanize.local_hosts = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
config.after(:all) do
|
29
|
+
Artifice.deactivate
|
30
|
+
end
|
31
|
+
# config.filter_run :focus => true
|
32
|
+
end
|
33
|
+
|
34
|
+
REMOTE_TEST_URL = "http://localhost"
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bbc-capybara-mechanize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: true
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
- rc0
|
10
|
+
version: 0.3.0.rc0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jeroen van Dijk
|
14
|
+
- Matthew Crouch
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-08-31 00:00:00 +01:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: mechanize
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: capybara
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
version: 1.0.0
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
description: RackTest driver for Capybara, but with remote request support thanks to mechanize
|
53
|
+
email: jeroen@jeevidee.nl
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- lib/capybara/mechanize/browser.rb
|
62
|
+
- lib/capybara/mechanize/cucumber.rb
|
63
|
+
- lib/capybara/mechanize/driver.rb
|
64
|
+
- lib/capybara/mechanize/version.rb
|
65
|
+
- lib/capybara/mechanize.rb
|
66
|
+
- lib/capybara/spec/extended_test_app.rb
|
67
|
+
- spec/driver/mechanize_driver_spec.rb
|
68
|
+
- spec/driver/remote_mechanize_driver_spec.rb
|
69
|
+
- spec/session/mechanize_spec.rb
|
70
|
+
- spec/session/remote_mechanize_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- README.mdown
|
73
|
+
has_rdoc: true
|
74
|
+
homepage: https://github.com/mobzilla/capybara-mechanize
|
75
|
+
licenses: []
|
76
|
+
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options:
|
79
|
+
- --charset=UTF-8
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.3.7
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: RackTest driver for Capybara with remote request support
|
105
|
+
test_files: []
|
106
|
+
|