capybara-mechanize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
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
+ Todo
33
+ ----
34
+ * Make the redirect specs pass
35
+ * Add specs for local to remote redirect (functionality is there though and works in our case)
36
+ * Test this driver with non-rack/non-ruby projects
37
+
38
+ Note on Patches/Pull Requests
39
+ -----------------------------
40
+
41
+ * Fork the project.
42
+ * Make your feature addition or bug fix.
43
+ * Add tests for it. This is important so I don't break it in a
44
+ future version unintentionally.
45
+ * Commit, do not mess with rakefile, version, or history.
46
+ (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)
47
+ * Send me a pull request. Bonus points for topic branches.
48
+
49
+ Copyright
50
+ ---------
51
+ Copyright (c) 2010 Jeroen van Dijk. See LICENSE for details.
@@ -0,0 +1,67 @@
1
+ require 'mechanize'
2
+
3
+ class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
4
+
5
+ def initialize(*args)
6
+ super
7
+ @agent = ::Mechanize.new
8
+ end
9
+
10
+ def visit(url)
11
+ get url
12
+ end
13
+
14
+ def response
15
+ response_proxy || super
16
+ end
17
+
18
+ def get(url, params = {}, headers = {})
19
+ process_remote_request(:get, url) || super
20
+ end
21
+
22
+ def post(url, params = {}, headers = {})
23
+ process_remote_request(:post, url, params, headers) || super
24
+ end
25
+
26
+ def remote?(url)
27
+ if !Capybara.app_host.nil?
28
+ true
29
+ elsif Capybara.default_host.nil?
30
+ false
31
+ else
32
+ host = URI.parse(url).host
33
+ !(host.nil? || host.include?(Capybara.default_host))
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def process_remote_request(method, url, *options)
40
+ if remote?(url)
41
+ reset_cache
42
+ @agent.send *( [method, url] + options)
43
+ follow_redirects!
44
+ true
45
+ end
46
+ end
47
+
48
+ def response_proxy
49
+ ResponseProxy.new(@agent.current_page) if @agent.current_page
50
+ end
51
+
52
+ class ResponseProxy
53
+ def initialize(page)
54
+ @page = page
55
+ end
56
+
57
+ def redirect?
58
+ %w(301 302).include?(@page.code)
59
+ end
60
+
61
+ def method_missing(method, *args, &block)
62
+ @page.send(method, *args, &block)
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1 @@
1
+ require 'capybara/driver/mechanize'
@@ -0,0 +1,5 @@
1
+ require 'capybara/mechanize'
2
+
3
+ Before('@mechanize') do
4
+ Capybara.current_driver = :mechanize
5
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capybara::Driver::Mechanize do
4
+ before do
5
+ @driver = Capybara::Driver::Mechanize.new(TestApp)
6
+ end
7
+
8
+ it "should throw an error when no rack app is given" do
9
+ running do
10
+ Capybara::Driver::Mechanize.new(nil)
11
+ end.should raise_error(ArgumentError)
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
+
19
+ # Pending:
20
+ # it_should_behave_like "driver with infinite redirect detection"
21
+
22
+
23
+ it "should default to local mode" do
24
+ @driver.remote?('http://www.local.com').should be false
25
+ end
26
+
27
+ context "with an app_host" do
28
+
29
+ before do
30
+ Capybara.app_host = 'remote.com'
31
+ end
32
+
33
+ after do
34
+ Capybara.app_host = nil
35
+ end
36
+
37
+ it "should treat urls as remote" do
38
+ @driver.remote?('http://www.remote.com').should be true
39
+ end
40
+ end
41
+
42
+ context "with a default url, no app host" do
43
+ before :each do
44
+ Capybara.default_host = 'local.com'
45
+ end
46
+
47
+ it "should treat urls with the same host names as local" do
48
+ @driver.remote?('http://www.local.com').should be false
49
+ end
50
+
51
+ it "should treat other urls as remote" do
52
+ @driver.remote?('http://www.remote.com').should be true
53
+ end
54
+
55
+ after :each do
56
+ Capybara.default_host = nil
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,34 @@
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
+ end
8
+
9
+ describe '#driver' do
10
+ it "should be a mechanize driver" do
11
+ @session.driver.should be_an_instance_of(Capybara::Driver::Mechanize)
12
+ end
13
+ end
14
+
15
+ describe '#mode' do
16
+ it "should remember the mode" do
17
+ @session.mode.should == :mechanize
18
+ end
19
+ end
20
+
21
+ describe '#click_link' do
22
+ it "should use data-method if available" do
23
+ @session.visit "/with_html"
24
+ @session.click_link "A link with data-method"
25
+ @session.body.should == 'The requested object was deleted'
26
+ end
27
+ end
28
+
29
+ it_should_behave_like "session"
30
+ it_should_behave_like "session without javascript support"
31
+ it_should_behave_like "session with headers support"
32
+ it_should_behave_like "session with status code support"
33
+ end
34
+ end
@@ -0,0 +1 @@
1
+ --color --backtrace
@@ -0,0 +1,19 @@
1
+ require 'capybara'
2
+ require 'capybara/mechanize'
3
+
4
+ require 'sinatra'
5
+ require 'spec'
6
+
7
+ # TODO move this stuff into capybara
8
+ require 'capybara/spec/driver'
9
+ require 'capybara/spec/session'
10
+
11
+ alias :running :lambda
12
+
13
+ Capybara.default_wait_time = 0 # less timeout so tests run faster
14
+
15
+ Spec::Runner.configure do |config|
16
+ config.after do
17
+ Capybara.default_selector = :xpath
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capybara-mechanize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jeroen van Dijk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-12 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mechanize
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
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
+ hash: 1
46
+ segments:
47
+ - 0
48
+ - 3
49
+ - 9
50
+ version: 0.3.9
51
+ type: :runtime
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
+ description: RackTest driver for Capybara, but with remote request support thanks to mechanize
70
+ email: jeroen@jeevidee.nl
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files: []
76
+
77
+ files:
78
+ - lib/capybara/driver/mechanize.rb
79
+ - lib/capybara/mechanize/cucumber.rb
80
+ - lib/capybara/mechanize.rb
81
+ - spec/driver/mechanize_spec.rb
82
+ - spec/session/mechanize_spec.rb
83
+ - spec/spec.opts
84
+ - spec/spec_helper.rb
85
+ - README.mdown
86
+ has_rdoc: true
87
+ homepage: http://github.com/jeroenvandijk/capybara-mechanize
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options:
92
+ - --charset=UTF-8
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project:
116
+ rubygems_version: 1.3.7
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: RackTest driver for Capybara with remote request support
120
+ test_files: []
121
+