2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2010-12-10 08:54:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-04-27 14:01:47 -04:00
|
|
|
RSpec.describe 'capybara/rspec' do
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'Feature', type: :feature do
|
|
|
|
it 'should include Capybara in rspec' do
|
2018-04-27 14:01:47 -04:00
|
|
|
visit('/foo')
|
|
|
|
expect(page.body).to include('Another World')
|
2010-12-10 08:54:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should include RSpec matcher proxies' do
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(self.class.ancestors).to include Capybara::RSpecMatcherProxies
|
2010-12-10 08:54:46 -05:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'resetting session' do
|
|
|
|
it 'sets a cookie in one example...' do
|
2018-04-27 14:01:47 -04:00
|
|
|
visit('/set_cookie')
|
|
|
|
expect(page.body).to include('Cookie set to test_cookie')
|
|
|
|
end
|
2017-03-28 13:41:49 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it '...then it is not available in the next' do
|
2018-04-27 14:01:47 -04:00
|
|
|
visit('/get_cookie')
|
|
|
|
expect(page.body).not_to include('test_cookie')
|
|
|
|
end
|
2017-03-28 13:41:49 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'setting the current driver' do
|
|
|
|
it 'sets the current driver in one example...' do
|
2018-04-27 14:01:47 -04:00
|
|
|
Capybara.current_driver = :selenium
|
|
|
|
end
|
2017-03-28 13:41:49 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it '...then it has returned to the default in the next example' do
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(Capybara.current_driver).to eq(:rack_test)
|
|
|
|
end
|
2017-03-28 13:41:49 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'switches to the javascript driver when giving it as metadata', js: true do
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(Capybara.current_driver).to eq(Capybara.javascript_driver)
|
2017-03-28 13:41:49 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'switches to the given driver when giving it as metadata', driver: :culerity do
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(Capybara.current_driver).to eq(:culerity)
|
2017-03-28 13:41:49 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context '#all' do
|
|
|
|
it 'allows access to the Capybara finder' do
|
2018-04-27 14:01:47 -04:00
|
|
|
visit('/with_html')
|
|
|
|
found = all(:css, 'h2') { |element| element[:class] == 'head' }
|
|
|
|
expect(found.size).to eq(5)
|
2017-03-28 13:41:49 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows access to the RSpec matcher' do
|
2018-04-27 14:01:47 -04:00
|
|
|
visit('/with_html')
|
|
|
|
strings = %w[test1 test2]
|
|
|
|
expect(strings).to all(be_a(String))
|
2017-03-28 13:41:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context '#within' do
|
|
|
|
it 'allows access to the Capybara scoper' do
|
2018-04-27 14:01:47 -04:00
|
|
|
visit('/with_html')
|
2017-03-28 13:41:49 -04:00
|
|
|
expect do
|
2018-07-10 17:18:39 -04:00
|
|
|
within(:css, '#does_not_exist') { click_link 'Go to simple' }
|
2017-03-28 13:41:49 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows access to the RSpec matcher' do
|
2018-04-27 14:01:47 -04:00
|
|
|
visit('/with_html')
|
2017-03-28 13:41:49 -04:00
|
|
|
# This reads terribly, but must call #within
|
2018-04-27 14:01:47 -04:00
|
|
|
expect(find(:css, 'span.number').text.to_i).to within(1).of(41)
|
2017-03-28 13:41:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-01-09 07:57:41 -05:00
|
|
|
|
2018-04-27 14:01:47 -04:00
|
|
|
context 'Type: Other', type: :other do
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'when RSpec::Matchers is included after Capybara::DSL' do
|
2018-04-27 14:01:47 -04:00
|
|
|
before do
|
|
|
|
class DSLMatchersTest
|
|
|
|
include Capybara::DSL
|
|
|
|
include RSpec::Matchers
|
|
|
|
end
|
|
|
|
|
|
|
|
@test_class_instance = DSLMatchersTest.new
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context '#all' do
|
|
|
|
it 'allows access to the Capybara finder' do
|
2018-04-27 14:01:47 -04:00
|
|
|
@test_class_instance.visit('/with_html')
|
|
|
|
expect(@test_class_instance.all(:css, 'h2.head').size).to eq(5)
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows access to the RSpec matcher' do
|
2018-04-27 14:01:47 -04:00
|
|
|
@test_class_instance.visit('/with_html')
|
|
|
|
strings = %w[test1 test2]
|
|
|
|
expect(strings).to @test_class_instance.all(be_a(String))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context '#within' do
|
|
|
|
it 'allows access to the Capybara scoper' do
|
2018-04-27 14:01:47 -04:00
|
|
|
@test_class_instance.visit('/with_html')
|
|
|
|
expect do
|
2018-07-10 17:18:39 -04:00
|
|
|
@test_class_instance.within(:css, '#does_not_exist') { @test_class_instance.click_link 'Go to simple' }
|
2018-04-27 14:01:47 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'allows access to the RSpec matcher' do
|
2018-04-27 14:01:47 -04:00
|
|
|
@test_class_instance.visit('/with_html')
|
|
|
|
# This reads terribly, but must call #within
|
|
|
|
expect(@test_class_instance.find(:css, 'span.number').text.to_i).to @test_class_instance.within(1).of(41)
|
|
|
|
end
|
|
|
|
end
|
2018-10-17 13:14:19 -04:00
|
|
|
|
|
|
|
context 'when `match_when_negated` is not defined in a matcher' do
|
|
|
|
before do
|
|
|
|
RSpec::Matchers.define :only_match_matcher do |expected|
|
|
|
|
match do |actual|
|
|
|
|
!(actual ^ expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-19 17:44:59 -04:00
|
|
|
it 'can be called with `not_to`' do
|
2018-10-17 13:14:19 -04:00
|
|
|
# This test is for a bug in jruby where `super` isn't defined correctly - https://github.com/jruby/jruby/issues/4678
|
|
|
|
# Reported in https://github.com/teamcapybara/capybara/issues/2115
|
|
|
|
@test_class_instance.instance_eval do
|
|
|
|
expect do
|
2018-10-19 17:44:59 -04:00
|
|
|
expect(true).not_to only_match_matcher(false) # rubocop:disable RSpec/ExpectActual
|
2018-10-17 13:14:19 -04:00
|
|
|
end.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-04-27 14:01:47 -04:00
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should not include Capybara' do
|
2018-04-27 14:01:47 -04:00
|
|
|
expect { visit('/') }.to raise_error(NoMethodError)
|
|
|
|
end
|
2011-01-09 07:57:41 -05:00
|
|
|
end
|
|
|
|
end
|
2011-02-11 08:44:58 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
feature 'Feature DSL' do
|
|
|
|
scenario 'is pulled in' do
|
2011-02-11 08:44:58 -05:00
|
|
|
visit('/foo')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(page.body).to include('Another World')
|
2011-02-11 08:44:58 -05:00
|
|
|
end
|
|
|
|
end
|