2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
require 'capybara/spec/test_app'
|
2015-07-22 02:41:23 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do
|
2018-04-27 14:01:47 -04:00
|
|
|
before :all do # rubocop:disable RSpec/BeforeAfterAll
|
2018-02-28 19:11:41 -05:00
|
|
|
@servers = Array.new(2) { Capybara::Server.new(TestApp.new).boot }
|
2012-01-04 10:33:41 -05:00
|
|
|
# sanity check
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(@servers[0].port).not_to eq(@servers[1].port) # rubocop:disable RSpec/ExpectInHook
|
|
|
|
expect(@servers.map(&:port)).not_to include 80 # rubocop:disable RSpec/ExpectInHook
|
2012-01-04 10:33:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def bases
|
|
|
|
@servers.map { |s| "http://#{s.host}:#{s.port}" }
|
2009-12-17 12:41:05 -05:00
|
|
|
end
|
2010-07-09 21:11:54 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
def should_be_on(server_index, path = '/host', scheme = 'http')
|
2012-07-21 16:44:10 -04:00
|
|
|
# Check that we are on /host on the given server
|
|
|
|
s = @servers[server_index]
|
2016-08-17 23:16:32 -04:00
|
|
|
|
|
|
|
expect(@session).to have_current_path("#{scheme}://#{s.host}:#{s.port}#{path}", url: true)
|
|
|
|
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.current_url.chomp('?')).to eq("#{scheme}://#{s.host}:#{s.port}#{path}")
|
|
|
|
expect(@session.current_host).to eq("#{scheme}://#{s.host}") # no port
|
|
|
|
expect(@session.current_path).to eq(path)
|
2018-04-27 14:01:47 -04:00
|
|
|
# Server should agree with us
|
|
|
|
expect(@session).to have_content("Current host is #{scheme}://#{s.host}:#{s.port}") if path == '/host'
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
def visit_host_links
|
|
|
|
@session.visit("#{bases[0]}/host_links?absolute_host=#{bases[1]}")
|
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is affected by visiting a page directly' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.visit("#{bases[0]}/host")
|
|
|
|
should_be_on 0
|
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'returns to the app host when visiting a relative url' do
|
2012-07-21 16:44:10 -04:00
|
|
|
Capybara.app_host = bases[1]
|
|
|
|
@session.visit("#{bases[0]}/host")
|
|
|
|
should_be_on 0
|
|
|
|
@session.visit('/host')
|
|
|
|
should_be_on 1
|
|
|
|
Capybara.app_host = nil
|
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is affected by setting Capybara.app_host' do
|
2012-07-21 16:44:10 -04:00
|
|
|
Capybara.app_host = bases[0]
|
2018-07-10 17:18:39 -04:00
|
|
|
@session.visit('/host')
|
2012-07-21 16:44:10 -04:00
|
|
|
should_be_on 0
|
|
|
|
Capybara.app_host = bases[1]
|
2018-07-10 17:18:39 -04:00
|
|
|
@session.visit('/host')
|
2012-07-21 16:44:10 -04:00
|
|
|
should_be_on 1
|
|
|
|
Capybara.app_host = nil
|
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is unaffected by following a relative link' do
|
2012-07-21 16:44:10 -04:00
|
|
|
visit_host_links
|
2018-07-10 17:18:39 -04:00
|
|
|
@session.click_link('Relative Host')
|
2012-07-21 16:44:10 -04:00
|
|
|
should_be_on 0
|
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is affected by following an absolute link' do
|
2012-07-21 16:44:10 -04:00
|
|
|
visit_host_links
|
2018-07-10 17:18:39 -04:00
|
|
|
@session.click_link('Absolute Host')
|
2012-07-21 16:44:10 -04:00
|
|
|
should_be_on 1
|
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is unaffected by posting through a relative form' do
|
2012-07-21 16:44:10 -04:00
|
|
|
visit_host_links
|
2018-07-10 17:18:39 -04:00
|
|
|
@session.click_button('Relative Host')
|
2012-07-21 16:44:10 -04:00
|
|
|
should_be_on 0
|
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is affected by posting through an absolute form' do
|
2012-07-21 16:44:10 -04:00
|
|
|
visit_host_links
|
2018-07-10 17:18:39 -04:00
|
|
|
@session.click_button('Absolute Host')
|
2012-07-21 16:44:10 -04:00
|
|
|
should_be_on 1
|
|
|
|
end
|
2012-01-04 10:33:41 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is affected by following a redirect' do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.visit("#{bases[0]}/redirect")
|
2018-07-10 17:18:39 -04:00
|
|
|
should_be_on 0, '/landed'
|
2010-07-09 21:11:54 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is affected by pushState', requires: [:js] do
|
|
|
|
@session.visit('/with_js')
|
2012-12-30 09:05:41 -05:00
|
|
|
@session.execute_script("window.history.pushState({}, '', '/pushed')")
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(@session.current_path).to eq('/pushed')
|
2012-12-30 09:05:41 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is affected by replaceState', requires: [:js] do
|
|
|
|
@session.visit('/with_js')
|
2012-12-30 09:05:41 -05:00
|
|
|
@session.execute_script("window.history.replaceState({}, '', '/replaced')")
|
2018-07-10 17:18:39 -04:00
|
|
|
expect(@session.current_path).to eq('/replaced')
|
2012-12-30 09:05:41 -05:00
|
|
|
end
|
2017-01-12 20:55:26 -05:00
|
|
|
|
2018-09-09 17:13:41 -04:00
|
|
|
it "doesn't raise exception on a nil current_url", requires: [:driver] do
|
2018-04-27 14:01:47 -04:00
|
|
|
allow(@session.driver).to receive(:current_url).and_return(nil)
|
2018-07-10 17:18:39 -04:00
|
|
|
@session.visit('/')
|
2017-01-12 20:55:26 -05:00
|
|
|
expect { @session.current_url }.not_to raise_exception
|
|
|
|
expect { @session.current_path }.not_to raise_exception
|
|
|
|
end
|
2018-03-16 17:33:52 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'within iframe', requires: [:frames] do
|
|
|
|
it 'should get the url of the top level browsing context' do
|
2018-03-16 17:33:52 -04:00
|
|
|
@session.visit('/within_frames')
|
|
|
|
expect(@session.current_url).to match(/within_frames\z/)
|
|
|
|
@session.within_frame('frameOne') do
|
|
|
|
expect(@session.current_url).to match(/within_frames\z/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|