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-16 16:02:16 -05:00
|
|
|
require 'capybara/dsl'
|
2009-11-12 11:51:31 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
class TestClass
|
|
|
|
include Capybara::DSL
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
Capybara::SpecHelper.run_specs TestClass.new, 'DSL', capybara_skip: %i[
|
2018-06-19 16:34:54 -04:00
|
|
|
js modals screenshot frames windows send_keys server hover about_scheme psc download css
|
2018-07-05 14:50:43 -04:00
|
|
|
] do |example|
|
|
|
|
case example.metadata[:full_description]
|
2018-07-23 14:17:07 -04:00
|
|
|
when /doesn't raise exception on a nil current_url$/,
|
|
|
|
/#refute_selector should warn not to use$/
|
2018-07-10 17:18:39 -04:00
|
|
|
skip 'Only makes sense when there is a real driver'
|
2018-07-05 14:50:43 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
|
2014-04-03 13:25:03 -04:00
|
|
|
RSpec.describe Capybara::DSL do
|
2009-11-12 11:51:31 -05:00
|
|
|
after do
|
2011-01-22 15:19:42 -05:00
|
|
|
Capybara.session_name = nil
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.default_driver = nil
|
2012-08-20 18:23:48 -04:00
|
|
|
Capybara.javascript_driver = nil
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.use_default_driver
|
2012-01-04 13:40:27 -05:00
|
|
|
Capybara.app = TestApp
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#default_driver' do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should default to rack_test' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.default_driver).to eq(:rack_test)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should be changeable' do
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.default_driver = :culerity
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.default_driver).to eq(:culerity)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#current_driver' do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should default to the default driver' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(:rack_test)
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.default_driver = :culerity
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(:culerity)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should be changeable' do
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.current_driver = :culerity
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(:culerity)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-12 07:33:00 -05:00
|
|
|
|
2009-11-16 14:11:42 -05:00
|
|
|
describe '#javascript_driver' do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should default to selenium' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.javascript_driver).to eq(:selenium)
|
2009-11-16 14:11:42 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should be changeable' do
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.javascript_driver = :culerity
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.javascript_driver).to eq(:culerity)
|
2009-11-16 14:11:42 -05:00
|
|
|
end
|
|
|
|
end
|
2009-11-12 11:51:31 -05:00
|
|
|
|
|
|
|
describe '#use_default_driver' do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should restore the default driver' do
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.current_driver = :culerity
|
|
|
|
Capybara.use_default_driver
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(:rack_test)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-07 09:37:33 -05:00
|
|
|
describe '#using_driver' do
|
|
|
|
before do
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(Capybara.current_driver).not_to eq(:selenium) # rubocop:disable RSpec/ExpectInHook
|
2010-11-07 09:37:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should set the driver using Capybara.current_driver=' do
|
|
|
|
driver = nil
|
|
|
|
Capybara.using_driver(:selenium) { driver = Capybara.current_driver }
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(driver).to eq(:selenium)
|
2010-11-07 09:37:33 -05:00
|
|
|
end
|
|
|
|
|
2011-08-19 17:28:32 -04:00
|
|
|
it 'should return the driver to default if it has not been changed' do
|
|
|
|
Capybara.using_driver(:selenium) do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(:selenium)
|
2011-08-19 17:28:32 -04:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(Capybara.default_driver)
|
2011-08-19 17:28:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should reset the driver even if an exception occurs' do
|
|
|
|
driver_before_block = Capybara.current_driver
|
2010-11-07 09:37:33 -05:00
|
|
|
begin
|
2018-07-10 17:18:39 -04:00
|
|
|
Capybara.using_driver(:selenium) { raise 'ohnoes!' }
|
|
|
|
rescue Exception # rubocop:disable Lint/RescueException,Lint/HandleExceptions
|
2010-11-07 09:37:33 -05:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(driver_before_block)
|
2010-11-07 09:37:33 -05:00
|
|
|
end
|
|
|
|
|
2011-08-18 21:41:07 -04:00
|
|
|
it 'should return the driver to what it was previously' do
|
|
|
|
Capybara.current_driver = :selenium
|
|
|
|
Capybara.using_driver(:culerity) do
|
|
|
|
Capybara.using_driver(:rack_test) do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(:rack_test)
|
2011-08-18 21:41:07 -04:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(:culerity)
|
2011-08-18 21:41:07 -04:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_driver).to eq(:selenium)
|
2011-08-18 21:41:07 -04:00
|
|
|
end
|
|
|
|
|
2010-11-07 09:37:33 -05:00
|
|
|
it 'should yield the passed block' do
|
|
|
|
called = false
|
|
|
|
Capybara.using_driver(:selenium) { called = true }
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(called).to eq(true)
|
2010-11-07 09:37:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-30 05:07:58 -04:00
|
|
|
describe '#using_wait_time' do
|
2012-09-09 18:36:04 -04:00
|
|
|
before do
|
2015-04-14 16:56:30 -04:00
|
|
|
@previous_wait_time = Capybara.default_max_wait_time
|
2012-09-09 18:36:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2015-04-14 16:56:30 -04:00
|
|
|
Capybara.default_max_wait_time = @previous_wait_time
|
2012-09-09 18:36:04 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should switch the wait time and switch it back' do
|
2011-08-30 05:07:58 -04:00
|
|
|
in_block = nil
|
|
|
|
Capybara.using_wait_time 6 do
|
2015-04-14 16:56:30 -04:00
|
|
|
in_block = Capybara.default_max_wait_time
|
2011-08-30 05:07:58 -04:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(in_block).to eq(6)
|
2015-04-14 16:56:30 -04:00
|
|
|
expect(Capybara.default_max_wait_time).to eq(@previous_wait_time)
|
2011-08-30 05:07:58 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should ensure wait time is reset' do
|
2011-08-30 05:07:58 -04:00
|
|
|
expect do
|
|
|
|
Capybara.using_wait_time 6 do
|
2018-07-10 17:18:39 -04:00
|
|
|
raise 'hell'
|
2011-08-30 05:07:58 -04:00
|
|
|
end
|
2018-07-10 17:18:39 -04:00
|
|
|
end.to raise_error(RuntimeError, 'hell')
|
2015-04-14 16:56:30 -04:00
|
|
|
expect(Capybara.default_max_wait_time).to eq(@previous_wait_time)
|
2011-08-30 05:07:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-12 11:51:31 -05:00
|
|
|
describe '#app' do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should be changeable' do
|
|
|
|
Capybara.app = 'foobar'
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.app).to eq('foobar')
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#current_session' do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should choose a session object of the current driver type' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session).to be_a(Capybara::Session)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should use #app as the application' do
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.app = proc {}
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session.app).to eq(Capybara.app)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should change with the current driver' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session.mode).to eq(:rack_test)
|
2010-09-07 12:34:47 -04:00
|
|
|
Capybara.current_driver = :selenium
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session.mode).to eq(:selenium)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should be persistent even across driver changes' do
|
2009-11-16 16:02:16 -05:00
|
|
|
object_id = Capybara.current_session.object_id
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session.object_id).to eq(object_id)
|
2010-09-07 12:34:47 -04:00
|
|
|
Capybara.current_driver = :selenium
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session.mode).to eq(:selenium)
|
|
|
|
expect(Capybara.current_session.object_id).not_to eq(object_id)
|
2009-11-12 11:51:31 -05:00
|
|
|
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.current_driver = :rack_test
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session.object_id).to eq(object_id)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should change when changing application' do
|
2009-11-16 16:02:16 -05:00
|
|
|
object_id = Capybara.current_session.object_id
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session.object_id).to eq(object_id)
|
2009-11-16 16:02:16 -05:00
|
|
|
Capybara.app = proc {}
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.current_session.object_id).not_to eq(object_id)
|
|
|
|
expect(Capybara.current_session.app).to eq(Capybara.app)
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
2011-01-09 17:47:28 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should change when the session name changes' do
|
2011-01-22 15:19:42 -05:00
|
|
|
object_id = Capybara.current_session.object_id
|
|
|
|
Capybara.session_name = :administrator
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.session_name).to eq(:administrator)
|
|
|
|
expect(Capybara.current_session.object_id).not_to eq(object_id)
|
2011-01-22 15:19:42 -05:00
|
|
|
Capybara.session_name = :default
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.session_name).to eq(:default)
|
|
|
|
expect(Capybara.current_session.object_id).to eq(object_id)
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
2011-01-22 15:19:42 -05:00
|
|
|
end
|
2011-01-09 17:47:28 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
describe '#using_session' do
|
|
|
|
it 'should change the session name for the duration of the block' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.session_name).to eq(:default)
|
2011-01-22 15:30:37 -05:00
|
|
|
Capybara.using_session(:administrator) do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.session_name).to eq(:administrator)
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.session_name).to eq(:default)
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should reset the session to the default, even if an exception occurs' do
|
2011-01-09 17:47:28 -05:00
|
|
|
begin
|
2011-01-22 15:30:37 -05:00
|
|
|
Capybara.using_session(:raise) do
|
2011-01-22 15:19:42 -05:00
|
|
|
raise
|
|
|
|
end
|
2018-07-10 17:18:39 -04:00
|
|
|
rescue Exception # rubocop:disable Lint/RescueException,Lint/HandleExceptions
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.session_name).to eq(:default)
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should yield the passed block' do
|
2011-01-09 17:47:28 -05:00
|
|
|
called = false
|
2011-01-22 15:30:37 -05:00
|
|
|
Capybara.using_session(:administrator) { called = true }
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(called).to eq(true)
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
2015-08-13 13:44:59 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should be nestable' do
|
2015-08-13 13:44:59 -04:00
|
|
|
Capybara.using_session(:outer) do
|
|
|
|
expect(Capybara.session_name).to eq(:outer)
|
|
|
|
Capybara.using_session(:inner) do
|
|
|
|
expect(Capybara.session_name).to eq(:inner)
|
|
|
|
end
|
|
|
|
expect(Capybara.session_name).to eq(:outer)
|
|
|
|
end
|
|
|
|
expect(Capybara.session_name).to eq(:default)
|
|
|
|
end
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
describe '#session_name' do
|
|
|
|
it 'should default to :default' do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(Capybara.session_name).to eq(:default)
|
2011-01-22 15:19:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-12 11:51:31 -05:00
|
|
|
describe 'the DSL' do
|
|
|
|
before do
|
2011-04-11 01:58:42 -04:00
|
|
|
@session = Class.new { include Capybara::DSL }.new
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should be possible to include it in another class' do
|
2012-12-18 11:29:56 -05:00
|
|
|
@session.visit('/with_html')
|
|
|
|
@session.click_link('ullamco')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.body).to include('Another World')
|
2009-11-12 12:09:29 -05:00
|
|
|
end
|
2009-12-12 07:33:00 -05:00
|
|
|
|
2009-11-14 07:51:52 -05:00
|
|
|
it "should provide a 'page' shortcut for more expressive tests" do
|
2012-12-18 11:29:56 -05:00
|
|
|
@session.page.visit('/with_html')
|
|
|
|
@session.page.click_link('ullamco')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.page.body).to include('Another World')
|
2009-11-14 07:51:52 -05:00
|
|
|
end
|
2011-01-09 17:47:28 -05:00
|
|
|
|
2011-01-22 15:30:37 -05:00
|
|
|
it "should provide an 'using_session' shortcut" do
|
2018-04-27 14:01:47 -04:00
|
|
|
allow(Capybara).to receive(:using_session)
|
2012-12-18 11:29:56 -05:00
|
|
|
@session.using_session(:name)
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(Capybara).to have_received(:using_session).with(:name)
|
2011-01-09 17:47:28 -05:00
|
|
|
end
|
2011-08-30 05:07:58 -04:00
|
|
|
|
|
|
|
it "should provide a 'using_wait_time' shortcut" do
|
2018-04-27 14:01:47 -04:00
|
|
|
allow(Capybara).to receive(:using_wait_time)
|
2012-12-18 11:29:56 -05:00
|
|
|
@session.using_wait_time(6)
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(Capybara).to have_received(:using_wait_time).with(6)
|
2011-08-30 05:07:58 -04:00
|
|
|
end
|
2009-11-12 11:51:31 -05:00
|
|
|
end
|
|
|
|
end
|