File: rack_test_spec.rb

package info (click to toggle)
ruby-capybara 3.36.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,380 kB
  • sloc: ruby: 23,399; javascript: 748; makefile: 11
file content (283 lines) | stat: -rw-r--r-- 9,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# frozen_string_literal: true

require 'spec_helper'

module TestSessions
  RackTest = Capybara::Session.new(:rack_test, TestApp)
end

skipped_tests = %i[
  js
  modals
  screenshot
  frames
  windows
  send_keys
  server
  hover
  about_scheme
  download
  css
  scroll
  spatial
  html_validation
  shadow_dom
  active_element
]
Capybara::SpecHelper.run_specs TestSessions::RackTest, 'RackTest', capybara_skip: skipped_tests do |example|
  case example.metadata[:full_description]
  when /has_css\? should support case insensitive :class and :id options/
    skip "Nokogiri doesn't support case insensitive CSS attribute matchers"
  when /#click_button should follow permanent redirects that maintain method/
    skip "Rack < 2 doesn't support 308" if Gem.loaded_specs['rack'] && Gem.loaded_specs['rack'].version < Gem::Version.new('2.0.0')
  end
end

RSpec.describe Capybara::Session do # rubocop:disable RSpec/MultipleDescribes
  include Capybara::RSpecMatchers
  context 'with rack test driver' do
    let(:session) { TestSessions::RackTest }

    describe '#driver' do
      it 'should be a rack test driver' do
        expect(session.driver).to be_an_instance_of(Capybara::RackTest::Driver)
      end
    end

    describe '#mode' do
      it 'should remember the mode' do
        expect(session.mode).to eq(:rack_test)
      end
    end

    describe '#click_link' do
      after do
        session.driver.options[:respect_data_method] = false
      end

      it 'should use data-method if option is true' do
        session.driver.options[:respect_data_method] = true
        session.visit '/with_html'
        session.click_link 'A link with data-method'
        expect(session.html).to include('The requested object was deleted')
      end

      it 'should not use data-method if option is false' do
        session.driver.options[:respect_data_method] = false
        session.visit '/with_html'
        session.click_link 'A link with data-method'
        expect(session.html).to include('Not deleted')
      end

      it "should use data-method if available even if it's capitalized" do
        session.driver.options[:respect_data_method] = true
        session.visit '/with_html'
        session.click_link 'A link with capitalized data-method'
        expect(session.html).to include('The requested object was deleted')
      end
    end

    describe '#fill_in' do
      it 'should warn that :fill_options are not supported' do
        session.visit '/with_html'
        expect { session.fill_in 'test_field', with: 'not_monkey', fill_options: { random: true } }.to \
          output(/^Options passed to Node#set but the RackTest driver doesn't support any - ignoring/).to_stderr
        expect(session).to have_field('test_field', with: 'not_monkey')
      end
    end

    describe '#attach_file' do
      context 'with multipart form' do
        it 'should submit an empty form-data section if no file is submitted' do
          session.visit('/form')
          session.click_button('Upload Empty')
          expect(session.html).to include('Successfully ignored empty file field.')
        end
      end

      it 'should not submit an obsolete mime type' do
        test_jpg_file_path = File.expand_path('fixtures/capybara.csv', File.dirname(__FILE__))
        session.visit('/form')
        session.attach_file 'form_document', test_jpg_file_path
        session.click_button('Upload Single')
        expect(session).to have_content('Content-type: text/csv')
      end
    end

    describe '#click' do
      context 'on a label' do
        it 'should toggle the associated checkbox' do
          session.visit('/form')
          expect(session).to have_unchecked_field('form_pets_cat')
          session.find(:label, 'Cat').click
          expect(session).to have_checked_field('form_pets_cat')
          session.find(:label, 'Cat').click
          expect(session).to have_unchecked_field('form_pets_cat')
          session.find(:label, 'McLaren').click
          expect(session).to have_checked_field('form_cars_mclaren', visible: :hidden)
        end

        it 'should toggle the associated radio' do
          session.visit('/form')
          expect(session).to have_unchecked_field('gender_male')
          session.find(:label, 'Male').click
          expect(session).to have_checked_field('gender_male')
          session.find(:label, 'Female').click
          expect(session).to have_unchecked_field('gender_male')
        end

        it 'should rewrite the forms action query for get submission' do
          session.visit('/form')
          session.click_button('mediocre')
          expect(session).not_to have_current_path(/foo|bar/)
        end

        it 'should rewrite the submit buttons formaction query for get submission' do
          session.visit('/form')
          session.click_button('mediocre2')
          expect(session).not_to have_current_path(/foo|bar/)
        end
      end
    end

    describe '#send_keys' do
      it 'raises an UnsupportedMethodError' do
        session.visit('/form')

        expect { session.send_keys(:tab) }.to raise_error(Capybara::NotSupportedByDriverError)
      end
    end

    describe '#active_element' do
      it 'raises an UnsupportedMethodError' do
        session.visit('/form')

        expect { session.active_element }.to raise_error(Capybara::NotSupportedByDriverError)
      end
    end

    describe '#text' do
      it 'should return original text content for textareas' do
        session.visit('/with_html')
        session.find_field('normal', type: 'textarea', with: 'banana').set('hello')
        normal = session.find(:css, '#normal')
        expect(normal.value).to eq 'hello'
        expect(normal.text).to eq 'banana'
      end
    end

    describe '#style' do
      it 'should raise an error' do
        session.visit('/with_html')
        el = session.find(:css, '#first')
        expect { el.style('display') }.to raise_error NotImplementedError, /not process CSS/
      end
    end
  end
end

RSpec.describe Capybara::RackTest::Driver do
  let(:driver) { TestSessions::RackTest.driver }

  describe ':headers option' do
    it 'should always set headers' do
      driver = described_class.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
      driver.visit('/get_header')
      expect(driver.html).to include('foobar')
    end

    it 'should keep headers on link clicks' do
      driver = described_class.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
      driver.visit('/header_links')
      driver.find_xpath('.//a').first.click
      expect(driver.html).to include('foobar')
    end

    it 'should keep headers on form submit' do
      driver = described_class.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
      driver.visit('/header_links')
      driver.find_xpath('.//input').first.click
      expect(driver.html).to include('foobar')
    end

    it 'should keep headers on redirects' do
      driver = described_class.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
      driver.visit('/get_header_via_redirect')
      expect(driver.html).to include('foobar')
    end
  end

  describe ':follow_redirects option' do
    it 'defaults to following redirects' do
      driver = described_class.new(TestApp)

      driver.visit('/redirect')
      expect(driver.response.header['Location']).to be_nil
      expect(driver.current_url).to match %r{/landed$}
    end

    it 'is possible to not follow redirects' do
      driver = described_class.new(TestApp, follow_redirects: false)

      driver.visit('/redirect')
      expect(driver.response.header['Location']).to match %r{/redirect_again$}
      expect(driver.current_url).to match %r{/redirect$}
    end
  end

  describe ':redirect_limit option' do
    context 'with default redirect limit' do
      let(:driver) { described_class.new(TestApp) }

      it 'should follow 5 redirects' do
        driver.visit('/redirect/5/times')
        expect(driver.html).to include('redirection complete')
      end

      it 'should not follow more than 6 redirects' do
        expect do
          driver.visit('/redirect/6/times')
        end.to raise_error(Capybara::InfiniteRedirectError)
      end
    end

    context 'with 21 redirect limit' do
      let(:driver) { described_class.new(TestApp, redirect_limit: 21) }

      it 'should follow 21 redirects' do
        driver.visit('/redirect/21/times')
        expect(driver.html).to include('redirection complete')
      end

      it 'should not follow more than 21 redirects' do
        expect do
          driver.visit('/redirect/22/times')
        end.to raise_error(Capybara::InfiniteRedirectError)
      end
    end
  end
end

RSpec.describe 'Capybara::String' do
  it 'should use HTML5 parsing' do
    skip 'Only valid if Nokogiri >= 1.12.0 or gumbo is included' unless defined? Nokogiri::HTML5
    Capybara.use_html5_parsing = true
    allow(Nokogiri::HTML5).to receive(:parse).and_call_original
    Capybara.string('<div id=test_div></div>')
    expect(Nokogiri::HTML5).to have_received(:parse)
  end
end

module CSSHandlerIncludeTester
  def dont_extend_css_handler
    raise 'should never be called'
  end
end

RSpec.describe Capybara::RackTest::CSSHandlers do
  include CSSHandlerIncludeTester

  it 'should not be extended by global includes' do
    expect(described_class.new).not_to respond_to(:dont_extend_css_handler)
  end
end