capybara-mechanize 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,10 +37,27 @@ When you want to use this driver to test a remote application. You have to set t
37
37
 
38
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
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
+
40
58
  Todo
41
59
  ----
42
- * Make the redirect specs pass
43
- * Add specs for local to remote redirect (functionality is there though and works in our case)
60
+ * Make the last 12 failing remote session spec pass, see remote_mechanize_spec and uncomment one line there to see them fail
44
61
  * Test this driver with non-rack/non-ruby projects
45
62
 
46
63
  Note on Patches/Pull Requests
@@ -6,13 +6,19 @@ class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
6
6
  def_delegator :agent, :scheme_handlers
7
7
  def_delegator :agent, :scheme_handlers=
8
8
 
9
- def initialize(*args)
10
- super
9
+ def initialize(app = nil)
11
10
  @agent = ::Mechanize.new
12
11
  @agent.redirect_ok = false
12
+
13
+ if app
14
+ # Delegate the RackApp to the RackTest driver
15
+ super(app)
16
+ elsif !Capybara.app_host
17
+ raise ArgumentError, "You have to set at least Capybara.app_host or Capybara.app"
18
+ end
13
19
  end
14
20
 
15
- def cleanup!
21
+ def reset!
16
22
  @agent.cookie_jar.clear!
17
23
  super
18
24
  end
@@ -30,7 +36,7 @@ class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
30
36
  unless response.redirect?
31
37
  raise "Last response was not a redirect. Cannot follow_redirect!"
32
38
  end
33
-
39
+
34
40
  location = if last_request_remote?
35
41
  remote_response.page.response['Location']
36
42
  else
@@ -43,7 +49,8 @@ class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
43
49
  def get(url, params = {}, headers = {})
44
50
  if remote?(url)
45
51
  process_remote_request(:get, url)
46
- else
52
+ else
53
+ register_local_request
47
54
  super
48
55
  end
49
56
  end
@@ -51,7 +58,26 @@ class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
51
58
  def post(url, params = {}, headers = {})
52
59
  if remote?(url)
53
60
  process_remote_request(:post, url, params, headers)
54
- else
61
+ else
62
+ register_local_request
63
+ super
64
+ end
65
+ end
66
+
67
+ def put(url, params = {}, headers = {})
68
+ if remote?(url)
69
+ process_remote_request(:put, url)
70
+ else
71
+ register_local_request
72
+ super
73
+ end
74
+ end
75
+
76
+ def delete(url, params = {}, headers = {})
77
+ if remote?(url)
78
+ process_remote_request(:delete, url, params, headers)
79
+ else
80
+ register_local_request
55
81
  super
56
82
  end
57
83
  end
@@ -63,7 +89,12 @@ class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
63
89
  false
64
90
  else
65
91
  host = URI.parse(url).host
66
- !(host.nil? || host.include?(Capybara.default_host))
92
+
93
+ if host.nil? && last_request_remote?
94
+ true
95
+ else
96
+ !(host.nil? || host.include?(Capybara.default_host))
97
+ end
67
98
  end
68
99
  end
69
100
 
@@ -74,15 +105,28 @@ class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
74
105
  def last_request_remote?
75
106
  !!@last_request_remote
76
107
  end
108
+
109
+ def register_local_request
110
+ @last_remote_host = nil
111
+ @last_request_remote = false
112
+ end
77
113
 
78
114
  def process_remote_request(method, url, *options)
79
115
  if remote?(url)
80
- url = File.join((Capybara.app_host || Capybara.default_host), url) if URI.parse(url).host.nil?
116
+ remote_uri = URI.parse(url)
117
+
118
+ if remote_uri.host.nil?
119
+ remote_host = @last_remote_host || Capybara.app_host || Capybara.default_host
120
+ url = File.join(remote_host, url)
121
+ url = "http://#{url}" unless url.include?("http")
122
+ else
123
+ @last_remote_host = "#{remote_uri.host}:#{remote_uri.port}"
124
+ end
125
+
81
126
  reset_cache
82
127
  @agent.send *( [method, url] + options)
128
+
83
129
  @last_request_remote = true
84
- else
85
- @last_request_remote = false
86
130
  end
87
131
  end
88
132
 
@@ -1 +1,11 @@
1
- require 'capybara/driver/mechanize'
1
+ require 'capybara'
2
+
3
+ module Capybara
4
+ module Driver
5
+ autoload :Mechanize, 'capybara/driver/mechanize_driver'
6
+ end
7
+ end
8
+
9
+ Capybara.register_driver :mechanize do |app|
10
+ Capybara::Driver::Mechanize.new(app)
11
+ end
@@ -8,7 +8,21 @@ class ExtendedTestApp < TestApp#< Sinatra::Base
8
8
  end
9
9
 
10
10
  get '/host' do
11
- "current host is #{request.host}"
11
+ "current host is #{request.host}:#{request.port}, method get"
12
+ end
13
+
14
+ get '/form_with_relative_action_to_host' do
15
+ %{<form action="/host" method="post">
16
+ <input type="submit" value="submit" />
17
+ </form>}
18
+ end
19
+
20
+ get '/relative_link_to_host' do
21
+ %{<a href="/host">host</a>}
22
+ end
23
+
24
+ post '/host' do
25
+ "current host is #{request.host}:#{request.port}, method post"
12
26
  end
13
27
  end
14
28
 
@@ -5,10 +5,10 @@ describe "Capybara::Driver::Mechanize, in local model" do
5
5
  @driver = Capybara::Driver::Mechanize.new(ExtendedTestApp)
6
6
  end
7
7
 
8
- it "should throw an error when no rack app is given" do
8
+ it "should throw an error when no rack app is given without an app host" do
9
9
  running do
10
- Capybara::Driver::Mechanize.new(nil)
11
- end.should raise_error(ArgumentError)
10
+ Capybara::Driver::Mechanize.new
11
+ end.should raise_error(ArgumentError, "You have to set at least Capybara.app_host or Capybara.app")
12
12
  end
13
13
 
14
14
  it_should_behave_like "driver"
@@ -32,31 +32,62 @@ describe "Capybara::Driver::Mechanize, in local model" do
32
32
  end
33
33
 
34
34
  it "should treat urls as remote" do
35
- @driver.remote?('http://www.remote.com').should be true
35
+ @driver.should be_remote('http://www.remote.com')
36
36
  end
37
37
  end
38
38
 
39
39
  context "with a default url, no app host" do
40
40
  before :each do
41
- Capybara.default_host = 'local.com'
41
+ Capybara.default_host = 'www.local.com'
42
42
  end
43
43
 
44
44
  it "should treat urls with the same host names as local" do
45
- @driver.remote?('http://www.local.com').should be false
45
+ @driver.should_not be_remote('http://www.local.com')
46
46
  end
47
47
 
48
48
  it "should treat other urls as remote" do
49
- @driver.remote?('http://www.remote.com').should be true
49
+ @driver.should be_remote('http://www.remote.com')
50
+ end
51
+
52
+ it "should treat relative paths as remote if the previous request was remote" do
53
+ @driver.visit('http://www.remote.com')
54
+ @driver.should be_remote('/some_relative_link')
55
+ end
56
+
57
+ it "should treat relative paths as local if the previous request was local" do
58
+ @driver.visit('http://www.local.com')
59
+ @driver.should_not be_remote('/some_relative_link')
50
60
  end
51
61
 
52
62
  it "should receive the right host" do
53
63
  @driver.visit('http://www.local.com/host')
54
- @driver.body.should include('local.com')
64
+ @driver.body.should == "current host is www.local.com:80, method get"
65
+ end
66
+
67
+ it "should always switch to the right context" do
68
+ @driver.visit('http://www.local.com/host')
69
+ should_be_a_local_get
70
+
71
+ @driver.visit('/host')
72
+ should_be_a_local_get
73
+ @driver.should_not be_remote('/first_local')
74
+
75
+ @driver.visit("#{REMOTE_TEST_URL}/host")
76
+ should_be_a_remote_get
77
+ @driver.should be_remote('/first_remote')
78
+
79
+ @driver.visit('/host')
80
+ should_be_a_remote_get
81
+ @driver.should be_remote('/second_remote')
82
+
83
+ @driver.visit('http://www.local.com/host')
84
+ should_be_a_local_get
85
+ @driver.should_not be_remote('/second_local')
55
86
  end
56
87
 
57
88
  it "should follow redirects from local to remote" do
58
89
  @driver.visit("http://www.local.com/redirect_to/#{REMOTE_TEST_URL}/host")
59
- @driver.body.should include(REMOTE_TEST_HOST)
90
+ should_be_a_remote_get
60
91
  end
61
92
 
62
93
  after :each do
@@ -66,13 +97,20 @@ describe "Capybara::Driver::Mechanize, in local model" do
66
97
 
67
98
  it "should include the right host when remote" do
68
99
  @driver.visit("#{REMOTE_TEST_URL}/host")
69
- @driver.body.should include(REMOTE_TEST_HOST)
100
+ should_be_a_remote_get
70
101
  end
71
102
 
72
-
73
103
  it "should follow redirects from remote to local" do
74
104
  @driver.visit("#{REMOTE_TEST_URL}/redirect_to/http://www.local.com/host")
75
- @driver.body.should include('local.com')
105
+ should_be_a_local_get
106
+ end
107
+
108
+ def should_be_a_remote_get
109
+ @driver.body.should == "current host is #{REMOTE_TEST_HOST}, method get"
110
+ end
111
+
112
+ def should_be_a_local_get
113
+ @driver.body.should == "current host is www.local.com:80, method get"
76
114
  end
77
115
 
78
116
  end
@@ -10,10 +10,16 @@ describe Capybara::Driver::Mechanize do
10
10
  end
11
11
 
12
12
  before do
13
- @driver = Capybara::Driver::Mechanize.new(TestApp)
13
+ @driver = Capybara::Driver::Mechanize.new
14
14
  end
15
15
 
16
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::Driver::Mechanize.new
20
+ end.should_not raise_error(ArgumentError)
21
+ end
22
+
17
23
  it_should_behave_like "driver"
18
24
  it_should_behave_like "driver with header support"
19
25
  it_should_behave_like "driver with status code support"
@@ -4,6 +4,7 @@ describe Capybara::Session do
4
4
  context 'with mechanize driver' do
5
5
  before do
6
6
  @session = Capybara::Session.new(:mechanize, TestApp)
7
+ Capybara.default_host = "www.local.com"
7
8
  end
8
9
 
9
10
  describe '#driver' do
@@ -25,6 +26,18 @@ describe Capybara::Session do
25
26
  @session.body.should == 'The requested object was deleted'
26
27
  end
27
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 == "current host is #{REMOTE_TEST_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 == "current host is #{REMOTE_TEST_HOST}, method post"
40
+ end
28
41
 
29
42
  it_should_behave_like "session"
30
43
  it_should_behave_like "session without javascript support"
@@ -12,7 +12,7 @@ describe Capybara::Session do
12
12
 
13
13
 
14
14
  before do
15
- @session = Capybara::Session.new(:mechanize, TestApp)
15
+ @session = Capybara::Session.new(:mechanize)
16
16
  end
17
17
 
18
18
  describe '#driver' do
@@ -29,15 +29,13 @@ describe Capybara::Session do
29
29
 
30
30
  describe '#click_link' do
31
31
  it "should use data-method if available" do
32
- pending "Needs to be implemented" do
33
- @session.visit "/with_html"
34
- @session.click_link "A link with data-method"
35
- @session.body.should == 'The requested object was deleted'
36
- end
32
+ @session.visit "/with_html"
33
+ @session.click_link "A link with data-method"
34
+ @session.body.should == 'The requested object was deleted'
37
35
  end
38
36
  end
39
37
 
40
- # Pending: Still 12 failing tests here:
38
+ # Pending: Still 90 (and before the update of capybara to 0.4.0 16) failing tests here (result is 702 examples, 90 failures, instead of 381 examples)
41
39
  # it_should_behave_like "session"
42
40
 
43
41
  it_should_behave_like "session without javascript support"
@@ -1,8 +1,8 @@
1
+ require 'bundler/setup'
1
2
  require 'capybara'
2
3
  require 'capybara/mechanize'
3
4
 
4
5
  require 'sinatra'
5
- require 'spec'
6
6
 
7
7
  require 'capybara/spec/extended_test_app'
8
8
 
@@ -14,10 +14,16 @@ alias :running :lambda
14
14
 
15
15
  Capybara.default_wait_time = 0 # less timeout so tests run faster
16
16
 
17
- Spec::Runner.configure do |config|
17
+ RSpec.configure do |config|
18
18
  config.after do
19
19
  Capybara.default_selector = :xpath
20
20
  end
21
+ # config.filter_run :focus => true
21
22
  end
22
- REMOTE_TEST_HOST = "capybara-testapp.heroku.com"
23
- REMOTE_TEST_URL = "http://#{REMOTE_TEST_HOST}:8070"
23
+
24
+ # Until this library is merged with capybara there needs to be a local app and you need to add
25
+ # 127.0.0.1 capybara-testapp.heroku.com to your host file
26
+ # Run the app with the following line:
27
+ # ruby -rrubygems lib/capybara/spec/extended_test_app.rb
28
+ REMOTE_TEST_HOST = "capybara-testapp.heroku.com:8070"
29
+ REMOTE_TEST_URL = "http://#{REMOTE_TEST_HOST}"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-mechanize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 3
10
- version: 0.2.3
9
+ - 4
10
+ version: 0.2.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeroen van Dijk
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-12 00:00:00 +02:00
18
+ date: 2010-09-27 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -24,7 +24,7 @@ dependencies:
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - "="
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  hash: 23
30
30
  segments:
@@ -40,32 +40,16 @@ dependencies:
40
40
  requirement: &id002 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - "="
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- hash: 1
45
+ hash: 15
46
46
  segments:
47
47
  - 0
48
- - 3
49
- - 9
50
- version: 0.3.9
48
+ - 4
49
+ - 0
50
+ version: 0.4.0
51
51
  type: :runtime
52
52
  version_requirements: *id002
53
- - !ruby/object:Gem::Dependency
54
- name: rspec
55
- prerelease: false
56
- requirement: &id003 !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - "="
60
- - !ruby/object:Gem::Version
61
- hash: 27
62
- segments:
63
- - 1
64
- - 3
65
- - 0
66
- version: 1.3.0
67
- type: :development
68
- version_requirements: *id003
69
53
  description: RackTest driver for Capybara, but with remote request support thanks to mechanize
70
54
  email: jeroen@jeevidee.nl
71
55
  executables: []
@@ -75,7 +59,7 @@ extensions: []
75
59
  extra_rdoc_files: []
76
60
 
77
61
  files:
78
- - lib/capybara/driver/mechanize.rb
62
+ - lib/capybara/driver/mechanize_driver.rb
79
63
  - lib/capybara/mechanize/cucumber.rb
80
64
  - lib/capybara/mechanize.rb
81
65
  - lib/capybara/spec/extended_test_app.rb
@@ -83,7 +67,6 @@ files:
83
67
  - spec/driver/remote_mechanize_driver_spec.rb
84
68
  - spec/session/mechanize_spec.rb
85
69
  - spec/session/remote_mechanize_spec.rb
86
- - spec/spec.opts
87
70
  - spec/spec_helper.rb
88
71
  - README.mdown
89
72
  has_rdoc: true
@@ -1 +0,0 @@
1
- --color --backtrace