teamcapybara--capybara/spec/rack_test_spec.rb

242 lines
7.7 KiB
Ruby
Raw Normal View History

2016-03-08 00:52:19 +00:00
# frozen_string_literal: true
2018-03-01 00:11:41 +00:00
require 'spec_helper'
2012-10-30 13:32:47 +00:00
module TestSessions
RackTest = Capybara::Session.new(:rack_test, TestApp)
end
skipped_tests = %i[
2018-03-01 00:11:41 +00:00
js
modals
screenshot
frames
windows
send_keys
server
hover
about_scheme
2018-06-17 17:29:46 +00:00
download
css
]
2018-07-10 21:18:39 +00:00
Capybara::SpecHelper.run_specs TestSessions::RackTest, 'RackTest', capybara_skip: skipped_tests
RSpec.describe Capybara::Session do # rubocop:disable RSpec/MultipleDescribes
context 'with rack test driver' do
before do
@session = TestSessions::RackTest
end
describe '#driver' do
2018-07-10 21:18:39 +00:00
it 'should be a rack test driver' do
expect(@session.driver).to be_an_instance_of(Capybara::RackTest::Driver)
end
end
describe '#mode' do
2018-07-10 21:18:39 +00:00
it 'should remember the mode' do
expect(@session.mode).to eq(:rack_test)
end
end
describe '#click_link' do
2018-08-27 16:34:33 +00:00
after do
@session.driver.options[:respect_data_method] = false
end
2018-07-10 21:18:39 +00:00
it 'should use data-method if option is true' do
@session.driver.options[:respect_data_method] = true
2018-07-10 21:18:39 +00:00
@session.visit '/with_html'
@session.click_link 'A link with data-method'
expect(@session.html).to include('The requested object was deleted')
end
2018-07-10 21:18:39 +00:00
it 'should not use data-method if option is false' do
@session.driver.options[:respect_data_method] = false
2018-07-10 21:18:39 +00:00
@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
2018-07-10 21:18:39 +00:00
@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
2015-08-25 23:57:01 +00:00
2018-07-10 21:18:39 +00:00
describe '#fill_in' do
it 'should warn that :fill_options are not supported' do
allow_any_instance_of(Capybara::RackTest::Node).to receive(:warn)
2018-07-10 21:18:39 +00:00
@session.visit '/with_html'
field = @session.fill_in 'test_field', with: 'not_monkey', fill_options: { random: true }
expect(@session).to have_field('test_field', with: 'not_monkey')
expect(field.base).to have_received(:warn).with("Options passed to Node#set but the RackTest driver doesn't support any - ignoring")
end
end
2018-07-10 21:18:39 +00:00
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
2018-07-10 21:18:39 +00:00
it 'should not submit an obsolete mime type' do
@test_jpg_file_path = File.expand_path('fixtures/capybara.csv', File.dirname(__FILE__))
2018-07-10 21:18:39 +00:00
@session.visit('/form')
@session.attach_file 'form_document', @test_jpg_file_path
@session.click_button('Upload Single')
2018-07-10 21:18:39 +00:00
expect(@session).to have_content('Content-type: text/csv')
end
end
2018-07-10 21:18:39 +00:00
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
2018-07-10 21:18:39 +00:00
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
end
end
describe '#text' do
2018-07-10 21:18:39 +00:00
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
2018-07-10 21:18:39 +00:00
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
2012-07-13 11:15:44 +00:00
RSpec.describe Capybara::RackTest::Driver do
2012-07-13 11:15:44 +00:00
before do
@driver = TestSessions::RackTest.driver
end
describe ':headers option' do
it 'should always set headers' do
2018-03-01 00:11:41 +00:00
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
2012-07-13 11:15:44 +00:00
@driver.visit('/get_header')
expect(@driver.html).to include('foobar')
2012-07-13 11:15:44 +00:00
end
it 'should keep headers on link clicks' do
2018-03-01 00:11:41 +00:00
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
2012-07-13 11:15:44 +00:00
@driver.visit('/header_links')
@driver.find_xpath('.//a').first.click
expect(@driver.html).to include('foobar')
2012-07-13 11:15:44 +00:00
end
it 'should keep headers on form submit' do
2018-03-01 00:11:41 +00:00
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
2012-07-13 11:15:44 +00:00
@driver.visit('/header_links')
@driver.find_xpath('.//input').first.click
expect(@driver.html).to include('foobar')
2012-07-13 11:15:44 +00:00
end
it 'should keep headers on redirects' do
2018-03-01 00:11:41 +00:00
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
2012-07-13 11:15:44 +00:00
@driver.visit('/get_header_via_redirect')
expect(@driver.html).to include('foobar')
2012-07-13 11:15:44 +00:00
end
end
describe ':follow_redirects option' do
2018-07-10 21:18:39 +00:00
it 'defaults to following redirects' do
2012-07-13 11:15:44 +00:00
@driver = Capybara::RackTest::Driver.new(TestApp)
@driver.visit('/redirect')
expect(@driver.response.header['Location']).to be_nil
2015-08-25 23:57:01 +00:00
expect(@driver.current_url).to match %r{/landed$}
2012-07-13 11:15:44 +00:00
end
2018-07-10 21:18:39 +00:00
it 'is possible to not follow redirects' do
2016-10-04 18:10:29 +00:00
@driver = Capybara::RackTest::Driver.new(TestApp, follow_redirects: false)
2012-07-13 11:15:44 +00:00
@driver.visit('/redirect')
expect(@driver.response.header['Location']).to match %r{/redirect_again$}
2015-08-25 23:57:01 +00:00
expect(@driver.current_url).to match %r{/redirect$}
2012-07-13 11:15:44 +00:00
end
end
describe ':redirect_limit option' do
2018-07-10 21:18:39 +00:00
context 'with default redirect limit' do
2012-07-13 11:15:44 +00:00
before do
@driver = Capybara::RackTest::Driver.new(TestApp)
end
2018-07-10 21:18:39 +00:00
it 'should follow 5 redirects' do
@driver.visit('/redirect/5/times')
expect(@driver.html).to include('redirection complete')
2012-07-13 11:15:44 +00:00
end
2018-07-10 21:18:39 +00:00
it 'should not follow more than 6 redirects' do
expect do
2018-07-10 21:18:39 +00:00
@driver.visit('/redirect/6/times')
end.to raise_error(Capybara::InfiniteRedirectError)
2012-07-13 11:15:44 +00:00
end
end
2018-07-10 21:18:39 +00:00
context 'with 21 redirect limit' do
2012-07-13 11:15:44 +00:00
before do
2016-10-04 18:10:29 +00:00
@driver = Capybara::RackTest::Driver.new(TestApp, redirect_limit: 21)
2012-07-13 11:15:44 +00:00
end
2018-07-10 21:18:39 +00:00
it 'should follow 21 redirects' do
@driver.visit('/redirect/21/times')
expect(@driver.html).to include('redirection complete')
2012-07-13 11:15:44 +00:00
end
2018-07-10 21:18:39 +00:00
it 'should not follow more than 21 redirects' do
expect do
2018-07-10 21:18:39 +00:00
@driver.visit('/redirect/22/times')
end.to raise_error(Capybara::InfiniteRedirectError)
2012-07-13 11:15:44 +00:00
end
end
end
end
module CSSHandlerIncludeTester
def dont_extend_css_handler
raise 'should never be called'
end
end
2018-03-01 00:11:41 +00:00
RSpec.describe Capybara::RackTest::CSSHandlers do
include CSSHandlerIncludeTester
2018-07-10 21:18:39 +00:00
it 'should not be extended by global includes' do
expect(Capybara::RackTest::CSSHandlers.new).not_to respond_to(:dont_extend_css_handler)
end
end