nimboids-capybara 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/History.txt +289 -0
  2. data/README.rdoc +722 -0
  3. data/lib/capybara.rb +252 -0
  4. data/lib/capybara/cucumber.rb +28 -0
  5. data/lib/capybara/driver/base.rb +64 -0
  6. data/lib/capybara/driver/node.rb +74 -0
  7. data/lib/capybara/dsl.rb +168 -0
  8. data/lib/capybara/node/actions.rb +162 -0
  9. data/lib/capybara/node/base.rb +63 -0
  10. data/lib/capybara/node/document.rb +25 -0
  11. data/lib/capybara/node/element.rb +201 -0
  12. data/lib/capybara/node/finders.rb +197 -0
  13. data/lib/capybara/node/matchers.rb +417 -0
  14. data/lib/capybara/node/simple.rb +132 -0
  15. data/lib/capybara/rack_test/browser.rb +121 -0
  16. data/lib/capybara/rack_test/driver.rb +80 -0
  17. data/lib/capybara/rack_test/form.rb +80 -0
  18. data/lib/capybara/rack_test/node.rb +105 -0
  19. data/lib/capybara/rails.rb +17 -0
  20. data/lib/capybara/rspec.rb +26 -0
  21. data/lib/capybara/rspec/features.rb +22 -0
  22. data/lib/capybara/rspec/matchers.rb +154 -0
  23. data/lib/capybara/selector.rb +89 -0
  24. data/lib/capybara/selenium/driver.rb +163 -0
  25. data/lib/capybara/selenium/node.rb +91 -0
  26. data/lib/capybara/server.rb +90 -0
  27. data/lib/capybara/session.rb +321 -0
  28. data/lib/capybara/spec/driver.rb +301 -0
  29. data/lib/capybara/spec/fixtures/capybara.jpg +3 -0
  30. data/lib/capybara/spec/fixtures/test_file.txt +1 -0
  31. data/lib/capybara/spec/public/test.js +43 -0
  32. data/lib/capybara/spec/session.rb +154 -0
  33. data/lib/capybara/spec/session/all_spec.rb +78 -0
  34. data/lib/capybara/spec/session/attach_file_spec.rb +73 -0
  35. data/lib/capybara/spec/session/check_spec.rb +65 -0
  36. data/lib/capybara/spec/session/choose_spec.rb +26 -0
  37. data/lib/capybara/spec/session/click_button_spec.rb +304 -0
  38. data/lib/capybara/spec/session/click_link_or_button_spec.rb +36 -0
  39. data/lib/capybara/spec/session/click_link_spec.rb +119 -0
  40. data/lib/capybara/spec/session/current_host_spec.rb +68 -0
  41. data/lib/capybara/spec/session/current_url_spec.rb +15 -0
  42. data/lib/capybara/spec/session/fill_in_spec.rb +125 -0
  43. data/lib/capybara/spec/session/find_button_spec.rb +18 -0
  44. data/lib/capybara/spec/session/find_by_id_spec.rb +18 -0
  45. data/lib/capybara/spec/session/find_field_spec.rb +26 -0
  46. data/lib/capybara/spec/session/find_link_spec.rb +19 -0
  47. data/lib/capybara/spec/session/find_spec.rb +149 -0
  48. data/lib/capybara/spec/session/first_spec.rb +105 -0
  49. data/lib/capybara/spec/session/has_button_spec.rb +32 -0
  50. data/lib/capybara/spec/session/has_content_spec.rb +106 -0
  51. data/lib/capybara/spec/session/has_css_spec.rb +243 -0
  52. data/lib/capybara/spec/session/has_field_spec.rb +192 -0
  53. data/lib/capybara/spec/session/has_link_spec.rb +37 -0
  54. data/lib/capybara/spec/session/has_select_spec.rb +129 -0
  55. data/lib/capybara/spec/session/has_selector_spec.rb +129 -0
  56. data/lib/capybara/spec/session/has_table_spec.rb +96 -0
  57. data/lib/capybara/spec/session/has_xpath_spec.rb +123 -0
  58. data/lib/capybara/spec/session/headers.rb +19 -0
  59. data/lib/capybara/spec/session/javascript.rb +289 -0
  60. data/lib/capybara/spec/session/response_code.rb +19 -0
  61. data/lib/capybara/spec/session/select_spec.rb +113 -0
  62. data/lib/capybara/spec/session/text_spec.rb +19 -0
  63. data/lib/capybara/spec/session/uncheck_spec.rb +21 -0
  64. data/lib/capybara/spec/session/unselect_spec.rb +61 -0
  65. data/lib/capybara/spec/session/within_spec.rb +178 -0
  66. data/lib/capybara/spec/test_app.rb +142 -0
  67. data/lib/capybara/spec/views/buttons.erb +4 -0
  68. data/lib/capybara/spec/views/fieldsets.erb +29 -0
  69. data/lib/capybara/spec/views/form.erb +365 -0
  70. data/lib/capybara/spec/views/frame_one.erb +8 -0
  71. data/lib/capybara/spec/views/frame_two.erb +8 -0
  72. data/lib/capybara/spec/views/header_links.erb +7 -0
  73. data/lib/capybara/spec/views/host_links.erb +12 -0
  74. data/lib/capybara/spec/views/popup_one.erb +8 -0
  75. data/lib/capybara/spec/views/popup_two.erb +8 -0
  76. data/lib/capybara/spec/views/postback.erb +13 -0
  77. data/lib/capybara/spec/views/tables.erb +122 -0
  78. data/lib/capybara/spec/views/with_html.erb +78 -0
  79. data/lib/capybara/spec/views/with_html_entities.erb +1 -0
  80. data/lib/capybara/spec/views/with_js.erb +48 -0
  81. data/lib/capybara/spec/views/with_scope.erb +36 -0
  82. data/lib/capybara/spec/views/with_simple_html.erb +1 -0
  83. data/lib/capybara/spec/views/within_frames.erb +10 -0
  84. data/lib/capybara/spec/views/within_popups.erb +25 -0
  85. data/lib/capybara/util/save_and_open_page.rb +44 -0
  86. data/lib/capybara/util/timeout.rb +27 -0
  87. data/lib/capybara/version.rb +3 -0
  88. data/spec/basic_node_spec.rb +77 -0
  89. data/spec/capybara_spec.rb +46 -0
  90. data/spec/driver/rack_test_driver_spec.rb +89 -0
  91. data/spec/driver/selenium_driver_spec.rb +50 -0
  92. data/spec/dsl_spec.rb +253 -0
  93. data/spec/fixtures/selenium_driver_rspec_failure.rb +8 -0
  94. data/spec/fixtures/selenium_driver_rspec_success.rb +8 -0
  95. data/spec/rspec/features_spec.rb +45 -0
  96. data/spec/rspec/matchers_spec.rb +495 -0
  97. data/spec/rspec_spec.rb +53 -0
  98. data/spec/save_and_open_page_spec.rb +155 -0
  99. data/spec/server_spec.rb +89 -0
  100. data/spec/session/rack_test_session_spec.rb +55 -0
  101. data/spec/session/selenium_session_spec.rb +26 -0
  102. data/spec/spec_helper.rb +30 -0
  103. data/spec/string_spec.rb +77 -0
  104. data/spec/timeout_spec.rb +28 -0
  105. metadata +346 -0
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ Capybara.app = TestApp
4
+
5
+ describe 'capybara/rspec', :type => :request do
6
+ it "should include Capybara in rspec" do
7
+ visit('/foo')
8
+ page.body.should include('Another World')
9
+ end
10
+
11
+ context "resetting session" do
12
+ it "sets a cookie in one example..." do
13
+ visit('/set_cookie')
14
+ page.body.should include('Cookie set to test_cookie')
15
+ end
16
+
17
+ it "...then it is not availbable in the next" do
18
+ visit('/get_cookie')
19
+ page.body.should_not include('test_cookie')
20
+ end
21
+ end
22
+
23
+ context "setting the current driver" do
24
+ it "sets the current driver in one example..." do
25
+ Capybara.current_driver = :selenium
26
+ end
27
+
28
+ it "...then it has returned to the default in the next example" do
29
+ Capybara.current_driver.should == :rack_test
30
+ end
31
+ end
32
+
33
+ it "switches to the javascript driver when giving it as metadata", :js => true do
34
+ Capybara.current_driver.should == Capybara.javascript_driver
35
+ end
36
+
37
+ it "switches to the given driver when giving it as metadata", :driver => :culerity do
38
+ Capybara.current_driver.should == :culerity
39
+ end
40
+ end
41
+
42
+ describe 'capybara/rspec', :type => :other do
43
+ it "should not include Capybara" do
44
+ expect { visit('/') }.to raise_error(NoMethodError)
45
+ end
46
+ end
47
+
48
+ feature "Feature DSL" do
49
+ scenario "is pulled in" do
50
+ visit('/foo')
51
+ page.body.should include('Another World')
52
+ end
53
+ end
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ require 'capybara/util/save_and_open_page'
4
+ require 'launchy'
5
+ describe Capybara do
6
+ describe ".save_page & .save_and_open_page" do
7
+ before do
8
+ @temp_file = mock("FILE")
9
+ @temp_file.stub!(:write)
10
+ @temp_file.stub!(:close)
11
+
12
+ @html = <<-HTML
13
+ <html>
14
+ <head>
15
+ <script type="text/javascript" src="/javascripts/prototype.js?123"/>
16
+ </head>
17
+ <body>
18
+ <h1>test</h1>
19
+ <p>
20
+ Some images (note differing whitespace closing tag):
21
+ <img src="/images/image1.jpeg" />
22
+ <img src="/images/image2.jpeg"/>
23
+ </p>
24
+ <p>
25
+ Some more in a non-existent directory:
26
+ <img src="/img/image3.jpeg" />
27
+ <img src="/img/image4.jpeg"/>
28
+ </p>
29
+ <p>
30
+ <a href="/not-here/foo.html">
31
+ A link to a file in a non-existent directory.
32
+ </a>
33
+ </p>
34
+ </body>
35
+ <html>
36
+ HTML
37
+
38
+ Launchy.stub(:open)
39
+ end
40
+
41
+ def default_file_expectations
42
+ @temp_file.stub!(:path).and_return('page.html')
43
+
44
+ File.should_receive(:exist?).and_return true
45
+ File.should_receive(:new).and_return @temp_file
46
+ end
47
+
48
+ describe "defaults" do
49
+ before do
50
+ default_file_expectations
51
+ end
52
+
53
+ it "should create a new temporary file" do
54
+ @temp_file.should_receive(:write).with @html
55
+ Capybara.save_page @html, 'page.html'
56
+ end
57
+
58
+ it "should open the file in the browser" do
59
+ Capybara.should_receive(:open_in_browser).with('page.html')
60
+ Capybara.save_and_open_page @html, 'page.html'
61
+ end
62
+ end
63
+
64
+ describe "custom output path" do
65
+ before do
66
+ @custom_path = File.join('tmp', 'capybara')
67
+ @custom_name = File.join(@custom_path, 'page.html')
68
+
69
+ @temp_file.stub!(:path).and_return(@custom_name)
70
+
71
+ Capybara.should_receive(:save_and_open_page_path).at_least(:once).and_return(@custom_path)
72
+ end
73
+
74
+ it "should create a new temporary file in the custom path" do
75
+ File.should_receive(:directory?).and_return true
76
+ File.should_receive(:exist?).and_return true
77
+ File.should_receive(:new).and_return @temp_file
78
+
79
+ @temp_file.should_receive(:write).with @html
80
+ Capybara.save_page @html, 'page.html'
81
+ end
82
+
83
+ it "should open the file - in the custom path - in the browser" do
84
+ Capybara.should_receive(:open_in_browser).with(@custom_name)
85
+ Capybara.save_and_open_page @html, 'page.html'
86
+ end
87
+
88
+ it "should be possible to configure output path" do
89
+ Capybara.should respond_to(:save_and_open_page_path)
90
+ default_setting = Capybara.save_and_open_page_path
91
+ lambda {
92
+ Capybara.save_and_open_page_path = File.join('tmp', 'capybara')
93
+ Capybara.save_and_open_page_path.should == File.join('tmp', 'capybara')
94
+ }.should_not raise_error
95
+ Capybara.save_and_open_page_path = default_setting
96
+ end
97
+ end
98
+
99
+ describe "rewrite_css_and_image_references" do
100
+ before do
101
+ default_file_expectations
102
+ @asset_root_dir = "/path/to/rails/public"
103
+ end
104
+
105
+ def mock_asset_root_with(directories)
106
+ @asset_root = Pathname.new(@asset_root_dir)
107
+ Capybara.should_receive(:asset_root).and_return @asset_root
108
+
109
+ dir = mock('asset_root mock dir')
110
+ Dir.should_receive(:new).with(@asset_root).and_return dir
111
+
112
+ dirents = [ '.', '..', 'file.html' ] + directories
113
+ dir.should_receive(:entries).and_return dirents
114
+
115
+ directories_regexp = directories.join('|')
116
+ FileTest.should_receive(:directory?) \
117
+ .at_least(dirents.size - 2).times \
118
+ .and_return { |dir|
119
+ dir =~ %r!#{@asset_root_dir}/(#{directories_regexp})$!
120
+ }
121
+ end
122
+
123
+ def expected_html_for_asset_root_with(directories)
124
+ mock_asset_root_with(directories)
125
+
126
+ expected_html = @html.clone
127
+ if not directories.empty?
128
+ directories_regexp = directories.join('|')
129
+ expected_html.gsub!(/"(\/(#{directories_regexp})\/)/,
130
+ '"%s\1' % @asset_root_dir)
131
+ end
132
+
133
+ return expected_html
134
+ end
135
+
136
+ def test_with_directories(directories)
137
+ @temp_file.should_receive(:write) \
138
+ .with expected_html_for_asset_root_with(directories)
139
+ Capybara.save_page @html, 'page.html'
140
+ end
141
+
142
+ context "asset_root contains some directories" do
143
+ it "should rewrite relative paths to absolute local paths" do
144
+ test_with_directories([ 'javascripts', 'images' ])
145
+ end
146
+ end
147
+
148
+ context "asset_root path contains no directories" do
149
+ it "should not rewrite any relative paths" do
150
+ test_with_directories([ ])
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capybara::Server do
4
+
5
+ it "should spool up a rack server" do
6
+ @app = proc { |env| [200, {}, "Hello Server!"]}
7
+ @server = Capybara::Server.new(@app).boot
8
+
9
+ @res = Net::HTTP.start(@server.host, @server.port) { |http| http.get('/') }
10
+
11
+ @res.body.should include('Hello Server')
12
+ end
13
+
14
+ it "should do nothing when no server given" do
15
+ running do
16
+ @server = Capybara::Server.new(nil).boot
17
+ end.should_not raise_error
18
+ end
19
+
20
+ it "should use specified port" do
21
+ Capybara.server_port = 22789
22
+
23
+ @app = proc { |env| [200, {}, "Hello Server!"]}
24
+ @server = Capybara::Server.new(@app).boot
25
+
26
+ @res = Net::HTTP.start(@server.host, 22789) { |http| http.get('/') }
27
+ @res.body.should include('Hello Server')
28
+
29
+ Capybara.server_port = nil
30
+ end
31
+
32
+ it "should find an available port" do
33
+ @app1 = proc { |env| [200, {}, "Hello Server!"]}
34
+ @app2 = proc { |env| [200, {}, "Hello Second Server!"]}
35
+
36
+ @server1 = Capybara::Server.new(@app1).boot
37
+ @server2 = Capybara::Server.new(@app2).boot
38
+
39
+ @res1 = Net::HTTP.start(@server1.host, @server1.port) { |http| http.get('/') }
40
+ @res1.body.should include('Hello Server')
41
+
42
+ @res2 = Net::HTTP.start(@server2.host, @server2.port) { |http| http.get('/') }
43
+ @res2.body.should include('Hello Second Server')
44
+ end
45
+
46
+ it "should use the server if it already running" do
47
+ @app1 = proc { |env| [200, {}, "Hello Server!"]}
48
+ @app2 = proc { |env| [200, {}, "Hello Second Server!"]}
49
+
50
+ @server1a = Capybara::Server.new(@app1).boot
51
+ @server1b = Capybara::Server.new(@app1).boot
52
+ @server2a = Capybara::Server.new(@app2).boot
53
+ @server2b = Capybara::Server.new(@app2).boot
54
+
55
+ @res1 = Net::HTTP.start(@server1b.host, @server1b.port) { |http| http.get('/') }
56
+ @res1.body.should include('Hello Server')
57
+
58
+ @res2 = Net::HTTP.start(@server2b.host, @server2b.port) { |http| http.get('/') }
59
+ @res2.body.should include('Hello Second Server')
60
+
61
+ @server1a.port.should == @server1b.port
62
+ @server2a.port.should == @server2b.port
63
+ end
64
+
65
+ it "should wait specified time for the app to boot" do
66
+ pending 'this test does not work: https://groups.google.com/d/msg/ruby-capybara/QrSKTbjh5rY/egvcVFYiWZMJ'
67
+
68
+ @slow_app = proc { |env| sleep(1); [200, {}, "Hello Slow Server!"] }
69
+
70
+ Capybara.server_boot_timeout = 1.5
71
+ @server = Capybara::Server.new(@slow_app).boot
72
+
73
+ @res = Net::HTTP.start(@server.host, @server.port) { |http| http.get('/') }
74
+ @res.body.should include('Hello Slow Server')
75
+ end
76
+
77
+ it "should raise an exception if boot timeout is exceeded" do
78
+ pending 'this test does not work: https://groups.google.com/d/msg/ruby-capybara/QrSKTbjh5rY/egvcVFYiWZMJ'
79
+
80
+ @slow_app = proc { |env| sleep(1); [200, {}, "Hello Slow Server!"] }
81
+
82
+ Capybara.server_boot_timeout = 0.5
83
+ server = Capybara::Server.new(@slow_app)
84
+ server.stub(:exit).and_return(:timeout)
85
+ server.stub(:puts)
86
+ server.boot.should == :timeout
87
+ end
88
+
89
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capybara::Session do
4
+ context 'with rack test driver' do
5
+ before do
6
+ @session = TestSessions::RackTest
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::RackTest::Driver)
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
+ 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 include('The requested object was deleted')
26
+ end
27
+
28
+ it "should not use data-method if option is false" do
29
+ @session.driver.options[:respect_data_method] = false
30
+ @session.visit "/with_html"
31
+ @session.click_link "A link with data-method"
32
+ @session.body.should include('Not deleted')
33
+ end
34
+
35
+ after do
36
+ @session.driver.options[:respect_data_method] = true
37
+ end
38
+ end
39
+
40
+ describe "#attach_file" do
41
+ context "with multipart form" do
42
+ it "should submit an empty form-data section if no file is submitted" do
43
+ @session.visit("/form")
44
+ @session.click_button("Upload Empty")
45
+ @session.body.should include('Successfully ignored empty file field.')
46
+ end
47
+ end
48
+ end
49
+
50
+ it_should_behave_like "session"
51
+ it_should_behave_like "session without javascript support"
52
+ it_should_behave_like "session with headers support"
53
+ it_should_behave_like "session with status code support"
54
+ end
55
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capybara::Session do
4
+ context 'with selenium driver' do
5
+ before do
6
+ @session = TestSessions::Selenium
7
+ end
8
+
9
+ describe '#driver' do
10
+ it "should be a selenium driver" do
11
+ @session.driver.should be_an_instance_of(Capybara::Selenium::Driver)
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
+ it_should_behave_like "session without status code support"
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ $:.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
2
+
3
+ require 'rubygems'
4
+ require "bundler/setup"
5
+
6
+ require 'rspec'
7
+ require 'capybara'
8
+
9
+ RSpec.configure do |config|
10
+ config.before do
11
+ Capybara.configure do |config|
12
+ config.default_selector = :xpath
13
+ end
14
+ end
15
+ end
16
+
17
+ # Required here instead of in rspec_spec to avoid RSpec deprecation warning
18
+ require 'capybara/rspec'
19
+
20
+ require 'capybara/spec/driver'
21
+ require 'capybara/spec/session'
22
+
23
+ alias :running :lambda
24
+
25
+ Capybara.default_wait_time = 0 # less timeout so tests run faster
26
+
27
+ module TestSessions
28
+ RackTest = Capybara::Session.new(:rack_test, TestApp)
29
+ Selenium = Capybara::Session.new(:selenium, TestApp)
30
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capybara do
4
+ describe '.string' do
5
+ let :string do
6
+ Capybara.string <<-STRING
7
+ <div id="page">
8
+ <div id="content">
9
+ <h1 data="fantastic">Awesome</h1>
10
+ <p>Yes it is</p>
11
+ </div>
12
+
13
+ <div id="footer" style="display: none">
14
+ <p>c2010</p>
15
+ <p>Jonas Nicklas</p>
16
+ <input type="text" name="foo" value="bar"/>
17
+ <select name="animal">
18
+ <option>Monkey</option>
19
+ <option selected="selected">Capybara</option>
20
+ </select>
21
+ </div>
22
+ </div>
23
+ STRING
24
+ end
25
+
26
+ it "allows using matchers" do
27
+ string.should have_css('#page')
28
+ string.should_not have_css('#does-not-exist')
29
+ end
30
+
31
+ it "allows using custom matchers" do
32
+ Capybara.add_selector :lifeform do
33
+ xpath { |name| "//option[contains(.,'#{name}')]" }
34
+ end
35
+ string.should have_selector(:page)
36
+ string.should_not have_selector(:'does-not-exist')
37
+ string.should have_selector(:lifeform, "Monkey")
38
+ string.should_not have_selector(:lifeform, "Gorilla")
39
+ end
40
+
41
+ it "allows using matchers with text option" do
42
+ string.should have_css('h1', :text => 'Awesome')
43
+ string.should_not have_css('h1', :text => 'Not so awesome')
44
+ end
45
+
46
+ it "allows finding only visible nodes" do
47
+ string.all('//p', :text => 'c2010', :visible => true).should be_empty
48
+ string.all('//p', :text => 'c2010', :visible => false).should have(1).element
49
+ end
50
+
51
+ it "allows finding elements and extracting text from them" do
52
+ string.find('//h1').text.should == 'Awesome'
53
+ end
54
+
55
+ it "allows finding elements and extracting attributes from them" do
56
+ string.find('//h1')[:data].should == 'fantastic'
57
+ end
58
+
59
+ it "allows finding elements and extracting the tag name from them" do
60
+ string.find('//h1').tag_name.should == 'h1'
61
+ end
62
+
63
+ it "allows finding elements and extracting the path" do
64
+ string.find('//h1').path.should == '/html/body/div/div[1]/h1'
65
+ end
66
+
67
+ it "allows finding elements and extracting the value" do
68
+ string.find('//input').value.should == 'bar'
69
+ string.find('//select').value.should == 'Capybara'
70
+ end
71
+
72
+ it "allows finding elements and checking if they are visible" do
73
+ string.find('//h1').should be_visible
74
+ string.find('//input').should_not be_visible
75
+ end
76
+ end
77
+ end