h13ronim-capybara 0.3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/History.txt +34 -0
  2. data/Manifest.txt +87 -0
  3. data/README.rdoc +405 -0
  4. data/Rakefile +29 -0
  5. data/config.ru +6 -0
  6. data/lib/capybara.rb +58 -0
  7. data/lib/capybara/cucumber.rb +32 -0
  8. data/lib/capybara/driver/base.rb +37 -0
  9. data/lib/capybara/driver/celerity_driver.rb +143 -0
  10. data/lib/capybara/driver/culerity_driver.rb +25 -0
  11. data/lib/capybara/driver/rack_test_driver.rb +250 -0
  12. data/lib/capybara/driver/selenium_driver.rb +149 -0
  13. data/lib/capybara/dsl.rb +60 -0
  14. data/lib/capybara/node.rb +60 -0
  15. data/lib/capybara/rails.rb +17 -0
  16. data/lib/capybara/save_and_open_page.rb +33 -0
  17. data/lib/capybara/searchable.rb +53 -0
  18. data/lib/capybara/server.rb +114 -0
  19. data/lib/capybara/session.rb +275 -0
  20. data/lib/capybara/wait_until.rb +23 -0
  21. data/lib/capybara/xpath.rb +176 -0
  22. data/script/console +10 -0
  23. data/script/destroy +14 -0
  24. data/script/generate +14 -0
  25. data/spec/capybara_spec.rb +18 -0
  26. data/spec/driver/celerity_driver_spec.rb +17 -0
  27. data/spec/driver/culerity_driver_spec.rb +12 -0
  28. data/spec/driver/rack_test_driver_spec.rb +11 -0
  29. data/spec/driver/remote_culerity_driver_spec.rb +23 -0
  30. data/spec/driver/remote_selenium_driver_spec.rb +18 -0
  31. data/spec/driver/selenium_driver_spec.rb +11 -0
  32. data/spec/drivers_spec.rb +127 -0
  33. data/spec/dsl/all_spec.rb +69 -0
  34. data/spec/dsl/attach_file_spec.rb +64 -0
  35. data/spec/dsl/check_spec.rb +39 -0
  36. data/spec/dsl/choose_spec.rb +26 -0
  37. data/spec/dsl/click_button_spec.rb +236 -0
  38. data/spec/dsl/click_link_spec.rb +108 -0
  39. data/spec/dsl/click_spec.rb +24 -0
  40. data/spec/dsl/current_url_spec.rb +8 -0
  41. data/spec/dsl/fill_in_spec.rb +103 -0
  42. data/spec/dsl/find_button_spec.rb +16 -0
  43. data/spec/dsl/find_by_id_spec.rb +16 -0
  44. data/spec/dsl/find_field_spec.rb +22 -0
  45. data/spec/dsl/find_link_spec.rb +17 -0
  46. data/spec/dsl/find_spec.rb +57 -0
  47. data/spec/dsl/has_button_spec.rb +32 -0
  48. data/spec/dsl/has_content_spec.rb +101 -0
  49. data/spec/dsl/has_css_spec.rb +107 -0
  50. data/spec/dsl/has_field_spec.rb +96 -0
  51. data/spec/dsl/has_link_spec.rb +33 -0
  52. data/spec/dsl/has_xpath_spec.rb +123 -0
  53. data/spec/dsl/locate_spec.rb +59 -0
  54. data/spec/dsl/select_spec.rb +71 -0
  55. data/spec/dsl/uncheck_spec.rb +21 -0
  56. data/spec/dsl/within_spec.rb +153 -0
  57. data/spec/dsl_spec.rb +140 -0
  58. data/spec/fixtures/capybara.jpg +0 -0
  59. data/spec/fixtures/test_file.txt +1 -0
  60. data/spec/public/jquery-ui.js +35 -0
  61. data/spec/public/jquery.js +19 -0
  62. data/spec/public/test.js +33 -0
  63. data/spec/save_and_open_page_spec.rb +43 -0
  64. data/spec/searchable_spec.rb +61 -0
  65. data/spec/server_spec.rb +53 -0
  66. data/spec/session/celerity_session_spec.rb +27 -0
  67. data/spec/session/culerity_session_spec.rb +25 -0
  68. data/spec/session/rack_test_session_spec.rb +25 -0
  69. data/spec/session/selenium_session_spec.rb +25 -0
  70. data/spec/session_spec.rb +77 -0
  71. data/spec/session_with_headers_support_spec.rb +13 -0
  72. data/spec/session_with_javascript_support_spec.rb +192 -0
  73. data/spec/session_without_headers_support_spec.rb +15 -0
  74. data/spec/session_without_javascript_support_spec.rb +15 -0
  75. data/spec/spec_helper.rb +27 -0
  76. data/spec/test_app.rb +71 -0
  77. data/spec/views/buttons.erb +4 -0
  78. data/spec/views/fieldsets.erb +29 -0
  79. data/spec/views/form.erb +232 -0
  80. data/spec/views/postback.erb +13 -0
  81. data/spec/views/tables.erb +122 -0
  82. data/spec/views/with_html.erb +38 -0
  83. data/spec/views/with_js.erb +39 -0
  84. data/spec/views/with_scope.erb +36 -0
  85. data/spec/views/with_simple_html.erb +1 -0
  86. data/spec/wait_until_spec.rb +28 -0
  87. data/spec/xpath_spec.rb +271 -0
  88. metadata +282 -0
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Capybara::Session do
4
+ context 'with rack test driver' do
5
+ before do
6
+ @session = Capybara::Session.new(:rack_test, TestApp)
7
+ end
8
+
9
+ describe '#driver' do
10
+ it "should be a rack test driver" do
11
+ @session.driver.should be_an_instance_of(Capybara::Driver::RackTest)
12
+ end
13
+ end
14
+
15
+ describe '#mode' do
16
+ it "should remember the mode" do
17
+ @session.mode.should == :rack_test
18
+ end
19
+ end
20
+
21
+ it_should_behave_like "session"
22
+ it_should_behave_like "session without javascript support"
23
+ it_should_behave_like "session with headers support"
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Capybara::Session do
4
+ context 'with selenium driver' do
5
+ before do
6
+ @session = Capybara::Session.new(:selenium, TestApp)
7
+ end
8
+
9
+ describe '#driver' do
10
+ it "should be a selenium driver" do
11
+ @session.driver.should be_an_instance_of(Capybara::Driver::Selenium)
12
+ end
13
+ end
14
+
15
+ describe '#mode' do
16
+ it "should remember the mode" do
17
+ @session.mode.should == :selenium
18
+ end
19
+ end
20
+
21
+ it_should_behave_like "session"
22
+ it_should_behave_like "session with javascript support"
23
+ it_should_behave_like "session without headers support"
24
+ end
25
+ end
@@ -0,0 +1,77 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+ require 'nokogiri'
3
+
4
+ shared_examples_for "session" do
5
+ def extract_results(session)
6
+ YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.text
7
+ end
8
+
9
+ describe '#app' do
10
+ it "should remember the application" do
11
+ @session.app.should == TestApp
12
+ end
13
+ end
14
+
15
+ describe '#visit' do
16
+ it "should fetch a response from the driver" do
17
+ @session.visit('/')
18
+ @session.body.should include('Hello world!')
19
+ @session.visit('/foo')
20
+ @session.body.should include('Another World')
21
+ end
22
+ end
23
+
24
+ describe '#body' do
25
+ it "should return the unmodified page body" do
26
+ @session.visit('/')
27
+ @session.body.should include('Hello world!')
28
+ end
29
+ end
30
+
31
+ describe '#source' do
32
+ it "should return the unmodified page source" do
33
+ @session.visit('/')
34
+ @session.source.should include('Hello world!')
35
+ end
36
+ end
37
+
38
+ it_should_behave_like "all"
39
+ it_should_behave_like "attach_file"
40
+ it_should_behave_like "check"
41
+ it_should_behave_like "choose"
42
+ it_should_behave_like "click"
43
+ it_should_behave_like "click_button"
44
+ it_should_behave_like "click_link"
45
+ it_should_behave_like "fill_in"
46
+ it_should_behave_like "find_button"
47
+ it_should_behave_like "find_field"
48
+ it_should_behave_like "find_link"
49
+ it_should_behave_like "find_by_id"
50
+ it_should_behave_like "find"
51
+ it_should_behave_like "has_content"
52
+ it_should_behave_like "has_css"
53
+ it_should_behave_like "has_css"
54
+ it_should_behave_like "has_xpath"
55
+ it_should_behave_like "has_link"
56
+ it_should_behave_like "has_button"
57
+ it_should_behave_like "has_field"
58
+ it_should_behave_like "has_select"
59
+ it_should_behave_like "has_table"
60
+ it_should_behave_like "select"
61
+ it_should_behave_like "uncheck"
62
+ it_should_behave_like "unselect"
63
+ it_should_behave_like "locate"
64
+ it_should_behave_like "within"
65
+ it_should_behave_like "current_url"
66
+ end
67
+
68
+
69
+ describe Capybara::Session do
70
+ context 'with non-existant driver' do
71
+ it "should raise an error" do
72
+ running {
73
+ Capybara::Session.new(:quox, TestApp).driver
74
+ }.should raise_error(Capybara::DriverNotFoundError)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+
4
+ shared_examples_for "session with headers support" do
5
+
6
+ describe '#response_headers' do
7
+ it "should return response headers" do
8
+ @session.visit('/with_simple_html')
9
+ @session.response_headers['Content-Type'].should == 'text/html'
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,192 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ require 'nokogiri'
4
+
5
+ shared_examples_for "session with javascript support" do
6
+ describe 'all JS specs' do
7
+ before do
8
+ Capybara.default_wait_time = 1
9
+ end
10
+
11
+ after do
12
+ Capybara.default_wait_time = 0
13
+ end
14
+
15
+ describe '#find' do
16
+ it "should allow triggering of custom JS events" do
17
+ pending "cannot figure out how to do this with selenium" if @session.mode == :selenium
18
+ @session.visit('/with_js')
19
+ @session.find(:css, '#with_focus_event').trigger(:focus)
20
+ @session.should have_css('#focus_event_triggered')
21
+ end
22
+ end
23
+
24
+ describe '#body' do
25
+ it "should return the current state of the page" do
26
+ @session.visit('/with_js')
27
+ @session.body.should include('I changed it')
28
+ @session.body.should_not include('This is text')
29
+ end
30
+ end
31
+
32
+ describe '#source' do
33
+ it "should return the original, unmodified source of the page" do
34
+ pending "cannot figure out how to do this with selenium" if @session.mode == :selenium
35
+ @session.visit('/with_js')
36
+ @session.source.should include('This is text')
37
+ @session.source.should_not include('I changed it')
38
+ end
39
+ end
40
+
41
+ describe "#evaluate_script" do
42
+ it "should return the evaluated script" do
43
+ @session.visit('/with_js')
44
+ @session.evaluate_script("1+3").should == 4
45
+ end
46
+ end
47
+
48
+ describe '#locate' do
49
+ it "should wait for asynchronous load" do
50
+ @session.visit('/with_js')
51
+ @session.click_link('Click me')
52
+ @session.locate("//a[contains(.,'Has been clicked')]")[:href].should == '#'
53
+ end
54
+ end
55
+
56
+ describe '#wait_until' do
57
+ before do
58
+ @default_timeout = Capybara.default_wait_time
59
+ end
60
+
61
+ after do
62
+ Capybara.default_wait_time = @default_wait_time
63
+ end
64
+
65
+ it "should wait for block to return true" do
66
+ @session.visit('/with_js')
67
+ @session.select('My Waiting Option', :from => 'waiter')
68
+ @session.evaluate_script('activeRequests == 1').should be_true
69
+ @session.wait_until do
70
+ @session.evaluate_script('activeRequests == 0')
71
+ end
72
+ @session.evaluate_script('activeRequests == 0').should be_true
73
+ end
74
+
75
+ it "should raise Capybara::TimeoutError if block doesn't return true within timeout" do
76
+ @session.visit('/with_html')
77
+ Proc.new do
78
+ @session.wait_until(0.1) do
79
+ @session.find('//div[@id="nosuchthing"]')
80
+ end
81
+ end.should raise_error(::Capybara::TimeoutError)
82
+ end
83
+
84
+ it "should accept custom timeout in seconds" do
85
+ start = Time.now
86
+ Capybara.default_wait_time = 5
87
+ begin
88
+ @session.wait_until(0.1) { false }
89
+ rescue Capybara::TimeoutError; end
90
+ (Time.now - start).should be_close(0.1, 0.1)
91
+ end
92
+
93
+ it "should default to Capybara.default_wait_time before timeout" do
94
+ start = Time.now
95
+ Capybara.default_wait_time = 0.2
96
+ begin
97
+ @session.wait_until { false }
98
+ rescue Capybara::TimeoutError; end
99
+ (Time.now - start).should be_close(0.2, 0.1)
100
+ end
101
+ end
102
+
103
+ describe '#click' do
104
+ it "should wait for asynchronous load" do
105
+ @session.visit('/with_js')
106
+ @session.click_link('Click me')
107
+ @session.click('Has been clicked')
108
+ end
109
+ end
110
+
111
+ describe '#click_link' do
112
+ it "should wait for asynchronous load" do
113
+ @session.visit('/with_js')
114
+ @session.click_link('Click me')
115
+ @session.click_link('Has been clicked')
116
+ end
117
+ end
118
+
119
+ describe '#click_button' do
120
+ it "should wait for asynchronous load" do
121
+ @session.visit('/with_js')
122
+ @session.click_link('Click me')
123
+ @session.click_button('New Here')
124
+ end
125
+ end
126
+
127
+ describe '#fill_in' do
128
+ it "should wait for asynchronous load" do
129
+ @session.visit('/with_js')
130
+ @session.click_link('Click me')
131
+ @session.fill_in('new_field', :with => 'Testing...')
132
+ end
133
+ end
134
+
135
+ describe '#check' do
136
+ it "should trigger associated events" do
137
+ @session.visit('/with_js')
138
+ @session.check('checkbox_with_event')
139
+ @session.should have_css('#checkbox_event_triggered');
140
+ end
141
+ end
142
+
143
+ describe '#has_xpath?' do
144
+ it "should wait for content to appear" do
145
+ @session.visit('/with_js')
146
+ @session.click_link('Click me')
147
+ @session.should have_xpath("//input[@type='submit' and @value='New Here']")
148
+ end
149
+ end
150
+
151
+ describe '#has_no_xpath?' do
152
+ it "should wait for content to disappear" do
153
+ @session.visit('/with_js')
154
+ @session.click_link('Click me')
155
+ @session.should have_no_xpath("//p[@id='change']")
156
+ end
157
+ end
158
+
159
+ describe '#has_css?' do
160
+ it "should wait for content to appear" do
161
+ @session.visit('/with_js')
162
+ @session.click_link('Click me')
163
+ @session.should have_css("input[type='submit'][value='New Here']")
164
+ end
165
+ end
166
+
167
+ describe '#has_no_xpath?' do
168
+ it "should wait for content to disappear" do
169
+ @session.visit('/with_js')
170
+ @session.click_link('Click me')
171
+ @session.should have_no_css("p#change")
172
+ end
173
+ end
174
+
175
+ describe '#has_content?' do
176
+ it "should wait for content to appear" do
177
+ @session.visit('/with_js')
178
+ @session.click_link('Click me')
179
+ @session.should have_content("Has been clicked")
180
+ end
181
+ end
182
+
183
+ describe '#has_no_content?' do
184
+ it "should wait for content to disappear" do
185
+ @session.visit('/with_js')
186
+ @session.click_link('Click me')
187
+ @session.should have_no_content("I changed it")
188
+ end
189
+ end
190
+
191
+ end
192
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ require 'nokogiri'
4
+
5
+ shared_examples_for "session without headers support" do
6
+ describe "#evaluate_script" do
7
+ before{ @session.visit('/with_simple_html') }
8
+ it "should raise an error" do
9
+ running {
10
+ @session.response_headers
11
+ }.should raise_error(Capybara::NotSupportedByDriverError)
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,15 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ require 'nokogiri'
4
+
5
+ shared_examples_for "session without javascript support" do
6
+ describe "#evaluate_script" do
7
+ before{ @session.visit('/with_js') }
8
+ it "should raise an error" do
9
+ running {
10
+ @session.evaluate_script("1+5")
11
+ }.should raise_error(Capybara::NotSupportedByDriverError)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,27 @@
1
+ $:.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
2
+ $:.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+ require 'capybara'
8
+ require 'test_app'
9
+ require 'drivers_spec'
10
+ require 'session_spec'
11
+ Dir[File.dirname(__FILE__)+'/dsl/*'].each { |group|
12
+ require group
13
+ }
14
+ require 'session_with_javascript_support_spec'
15
+ require 'session_without_javascript_support_spec'
16
+ require 'session_with_headers_support_spec'
17
+ require 'session_without_headers_support_spec'
18
+
19
+ alias :running :lambda
20
+
21
+ Capybara.default_wait_time = 0 # less timeout so tests run faster
22
+
23
+ Spec::Runner.configure do |config|
24
+ config.after do
25
+ Capybara.default_selector = :xpath
26
+ end
27
+ end
@@ -0,0 +1,71 @@
1
+ require 'sinatra/base'
2
+ require 'rack'
3
+ require 'yaml'
4
+
5
+ class TestApp < Sinatra::Base
6
+ set :root, File.dirname(__FILE__)
7
+ set :static, true
8
+
9
+ get '/' do
10
+ 'Hello world!'
11
+ end
12
+
13
+ get '/foo' do
14
+ 'Another World'
15
+ end
16
+
17
+ get '/redirect' do
18
+ redirect '/redirect_again'
19
+ end
20
+
21
+ get '/redirect_again' do
22
+ redirect '/landed'
23
+ end
24
+
25
+ get '/landed' do
26
+ "You landed"
27
+ end
28
+
29
+ get '/with-quotes' do
30
+ %q{"No," he said, "you can't do that."}
31
+ end
32
+
33
+ get '/form/get' do
34
+ '<pre id="results">' + params[:form].to_yaml + '</pre>'
35
+ end
36
+
37
+ get '/favicon.ico' do
38
+ nil
39
+ end
40
+
41
+ post '/redirect' do
42
+ redirect '/redirect_again'
43
+ end
44
+
45
+ get '/redirect_back' do
46
+ redirect back
47
+ end
48
+
49
+ get '/:view' do |view|
50
+ erb view.to_sym
51
+ end
52
+
53
+ post '/form' do
54
+ '<pre id="results">' + params[:form].to_yaml + '</pre>'
55
+ end
56
+
57
+ post '/upload' do
58
+ begin
59
+ buffer = []
60
+ buffer << "Content-type: #{params[:form][:document][:type]}"
61
+ buffer << "File content: #{params[:form][:document][:tempfile].read}"
62
+ buffer.join(' | ')
63
+ rescue
64
+ 'No file uploaded'
65
+ end
66
+ end
67
+ end
68
+
69
+ if __FILE__ == $0
70
+ Rack::Handler::Mongrel.run TestApp, :Port => 8070
71
+ end