2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2010-09-17 19:56:32 -04:00
|
|
|
require 'spec_helper'
|
2009-11-07 14:44:45 -05:00
|
|
|
|
2012-10-30 09:32:47 -04:00
|
|
|
module TestSessions
|
|
|
|
RackTest = Capybara::Session.new(:rack_test, TestApp)
|
|
|
|
end
|
|
|
|
|
2018-04-13 17:49:28 -04:00
|
|
|
skipped_tests = %i[
|
2018-02-28 19:11:41 -05:00
|
|
|
js
|
|
|
|
modals
|
|
|
|
screenshot
|
|
|
|
frames
|
|
|
|
windows
|
|
|
|
send_keys
|
|
|
|
server
|
|
|
|
hover
|
|
|
|
about_scheme
|
2012-07-21 16:44:10 -04:00
|
|
|
]
|
2018-04-13 17:49:28 -04:00
|
|
|
Capybara::SpecHelper.run_specs TestSessions::RackTest, "RackTest", capybara_skip: skipped_tests
|
2012-07-21 16:44:10 -04:00
|
|
|
|
2018-04-13 18:16:26 -04:00
|
|
|
RSpec.describe Capybara::Session do
|
2009-11-07 14:44:45 -05:00
|
|
|
context 'with rack test driver' do
|
|
|
|
before do
|
2010-09-07 12:16:19 -04:00
|
|
|
@session = TestSessions::RackTest
|
2009-11-07 14:44:45 -05:00
|
|
|
end
|
2009-12-12 07:33:00 -05:00
|
|
|
|
2009-11-07 14:44:45 -05:00
|
|
|
describe '#driver' do
|
|
|
|
it "should be a rack test driver" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.driver).to be_an_instance_of(Capybara::RackTest::Driver)
|
2009-11-07 14:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-12 07:33:00 -05:00
|
|
|
|
2009-11-07 14:44:45 -05:00
|
|
|
describe '#mode' do
|
|
|
|
it "should remember the mode" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.mode).to eq(:rack_test)
|
2009-11-07 14:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-12 07:33:00 -05:00
|
|
|
|
2010-04-09 08:01:17 -04:00
|
|
|
describe '#click_link' do
|
2012-07-12 08:39:18 -04:00
|
|
|
it "should use data-method if option is true" do
|
|
|
|
@session.driver.options[:respect_data_method] = true
|
2010-04-09 08:01:17 -04:00
|
|
|
@session.visit "/with_html"
|
|
|
|
@session.click_link "A link with data-method"
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.html).to include('The requested object was deleted')
|
2010-04-09 08:01:17 -04:00
|
|
|
end
|
2011-08-30 05:20:29 -04:00
|
|
|
|
|
|
|
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"
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.html).to include('Not deleted')
|
2011-08-30 05:20:29 -04:00
|
|
|
end
|
|
|
|
|
2011-10-18 08:41:44 -04:00
|
|
|
it "should use data-method if available even if it's capitalized" do
|
2012-07-12 08:39:18 -04:00
|
|
|
@session.driver.options[:respect_data_method] = true
|
2011-10-18 08:41:44 -04:00
|
|
|
@session.visit "/with_html"
|
2012-01-31 10:20:35 -05:00
|
|
|
@session.click_link "A link with capitalized data-method"
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.html).to include('The requested object was deleted')
|
2011-10-18 08:41:44 -04:00
|
|
|
end
|
|
|
|
|
2011-08-30 05:20:29 -04:00
|
|
|
after do
|
2012-07-12 08:39:18 -04:00
|
|
|
@session.driver.options[:respect_data_method] = false
|
2011-08-30 05:20:29 -04:00
|
|
|
end
|
2010-04-09 08:01:17 -04:00
|
|
|
end
|
2015-08-25 19:57:01 -04:00
|
|
|
|
2014-06-19 12:29:12 -04:00
|
|
|
describe "#fill_in" do
|
|
|
|
it "should warn that :fill_options are not supported" do
|
2018-01-10 19:20:07 -05:00
|
|
|
expect_any_instance_of(Capybara::RackTest::Node).to receive(:warn)
|
|
|
|
.with("Options passed to Node#set but the RackTest driver doesn't support any - ignoring")
|
2014-06-19 12:29:12 -04:00
|
|
|
@session.visit "/with_html"
|
2018-01-10 19:20:07 -05:00
|
|
|
@session.fill_in 'test_field', with: 'not_monkey', fill_options: { random: true }
|
|
|
|
expect(@session).to have_field('test_field', with: 'not_monkey')
|
2014-06-19 12:29:12 -04:00
|
|
|
end
|
|
|
|
end
|
2010-04-09 08:01:17 -04:00
|
|
|
|
2010-12-06 02:22:59 -05: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")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.html).to include('Successfully ignored empty file field.')
|
2010-12-06 02:22:59 -05:00
|
|
|
end
|
|
|
|
end
|
2016-09-12 21:55:17 -04:00
|
|
|
|
|
|
|
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
|
2010-12-06 02:22:59 -05:00
|
|
|
end
|
2016-04-21 17:25:14 -04: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
|
|
|
|
|
|
|
|
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
|
2017-01-16 15:39:44 -05:00
|
|
|
|
|
|
|
describe '#text' do
|
2017-01-18 11:47:48 -05:00
|
|
|
it "should return original text content for textareas" do
|
2017-01-16 15:39:44 -05:00
|
|
|
@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
|
2009-11-07 14:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
2012-07-13 07:15:44 -04:00
|
|
|
|
2014-04-03 13:25:03 -04:00
|
|
|
RSpec.describe Capybara::RackTest::Driver do
|
2012-07-13 07:15:44 -04:00
|
|
|
before do
|
|
|
|
@driver = TestSessions::RackTest.driver
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ':headers option' do
|
|
|
|
it 'should always set headers' do
|
2018-02-28 19:11:41 -05:00
|
|
|
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
|
2012-07-13 07:15:44 -04:00
|
|
|
@driver.visit('/get_header')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@driver.html).to include('foobar')
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should keep headers on link clicks' do
|
2018-02-28 19:11:41 -05:00
|
|
|
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
|
2012-07-13 07:15:44 -04:00
|
|
|
@driver.visit('/header_links')
|
2013-02-19 12:03:26 -05:00
|
|
|
@driver.find_xpath('.//a').first.click
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@driver.html).to include('foobar')
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should keep headers on form submit' do
|
2018-02-28 19:11:41 -05:00
|
|
|
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
|
2012-07-13 07:15:44 -04:00
|
|
|
@driver.visit('/header_links')
|
2013-02-19 12:03:26 -05:00
|
|
|
@driver.find_xpath('.//input').first.click
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@driver.html).to include('foobar')
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should keep headers on redirects' do
|
2018-02-28 19:11:41 -05:00
|
|
|
@driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
|
2012-07-13 07:15:44 -04:00
|
|
|
@driver.visit('/get_header_via_redirect')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@driver.html).to include('foobar')
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ':follow_redirects option' do
|
|
|
|
it "defaults to following redirects" do
|
|
|
|
@driver = Capybara::RackTest::Driver.new(TestApp)
|
|
|
|
|
|
|
|
@driver.visit('/redirect')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@driver.response.header['Location']).to be_nil
|
2015-08-25 19:57:01 -04:00
|
|
|
expect(@driver.current_url).to match %r{/landed$}
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is possible to not follow redirects" do
|
2016-10-04 14:10:29 -04:00
|
|
|
@driver = Capybara::RackTest::Driver.new(TestApp, follow_redirects: false)
|
2012-07-13 07:15:44 -04:00
|
|
|
|
|
|
|
@driver.visit('/redirect')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@driver.response.header['Location']).to match %r{/redirect_again$}
|
2015-08-25 19:57:01 -04:00
|
|
|
expect(@driver.current_url).to match %r{/redirect$}
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ':redirect_limit option' do
|
|
|
|
context "with default redirect limit" do
|
|
|
|
before do
|
|
|
|
@driver = Capybara::RackTest::Driver.new(TestApp)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should follow 5 redirects" do
|
|
|
|
@driver.visit("/redirect/5/times")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@driver.html).to include('redirection complete')
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not follow more than 6 redirects" do
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-07-13 07:15:44 -04:00
|
|
|
@driver.visit("/redirect/6/times")
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::InfiniteRedirectError)
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with 21 redirect limit" do
|
|
|
|
before do
|
2016-10-04 14:10:29 -04:00
|
|
|
@driver = Capybara::RackTest::Driver.new(TestApp, redirect_limit: 21)
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should follow 21 redirects" do
|
|
|
|
@driver.visit("/redirect/21/times")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@driver.html).to include('redirection complete')
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not follow more than 21 redirects" do
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-07-13 07:15:44 -04:00
|
|
|
@driver.visit("/redirect/22/times")
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::InfiniteRedirectError)
|
2012-07-13 07:15:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-03-31 12:52:00 -04:00
|
|
|
|
|
|
|
module CSSHandlerIncludeTester
|
|
|
|
def dont_extend_css_handler
|
|
|
|
raise 'should never be called'
|
|
|
|
end
|
|
|
|
end
|
2018-02-28 19:11:41 -05:00
|
|
|
include CSSHandlerIncludeTester # rubocop:disable Style/MixinUsage
|
2014-03-31 12:52:00 -04:00
|
|
|
|
2018-02-28 19:11:41 -05:00
|
|
|
RSpec.describe Capybara::RackTest::CSSHandlers do
|
2014-03-31 12:52:00 -04: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
|