Add rubocop-spec - some disabled for now

This commit is contained in:
Thomas Walpole 2018-04-27 11:01:47 -07:00
parent c445acfeaf
commit a17e9c36fa
54 changed files with 394 additions and 361 deletions

View File

@ -1,3 +1,6 @@
require:
- rubocop-rspec
AllCops:
DisabledByDefault: false
TargetRubyVersion: 2.2.2
@ -76,9 +79,6 @@ Style/SingleLineMethods:
Style/StringLiterals:
Enabled: false
Style/StringLiteralsInInterpolation:
Enabled: false
Style/RegexpLiteral:
Enabled: false
@ -112,9 +112,6 @@ Style/RedundantReturn:
Style/ClassAndModuleChildren:
Enabled: false
Style/ModuleFunction:
Enabled: false
Style/NumericLiterals:
Exclude:
- 'lib/capybara/spec/**/*'
@ -141,3 +138,36 @@ Security/YAMLLoad:
Exclude:
- 'lib/capybara/spec/**/*'
- 'spec/**/*'
RSpec/ExampleWording:
Enabled: false
RSpec/InstanceVariable:
Enabled: false
RSpec/ExampleLength:
Enabled: false
RSpec/MultipleExpectations:
Enabled: false
RSpec/ContextWording:
Enabled: false
RSpec/DescribedClass:
Enabled: false
RSpec/NestedGroups:
Enabled: false
RSpec/DescribeClass:
Enabled: false
RSpec/AnyInstance:
Enabled: false
RSpec/FilePath:
Enabled: false
RSpec/PredicateMatcher:
Enabled: false

View File

@ -3,7 +3,7 @@
module Capybara
# @api private
module Helpers
extend self
module_function
##
# @deprecated

View File

@ -35,7 +35,7 @@ module Capybara
@description << "visible " if visible == :visible
@description << "non-visible " if visible == :hidden
@description << "#{label} #{locator.inspect}"
@description << " with#{" exact" if exact_text == true} text #{options[:text].inspect}" if options[:text]
@description << " with#{' exact' if exact_text == true} text #{options[:text].inspect}" if options[:text]
@description << " with exact text #{options[:exact_text]}" if options[:exact_text].is_a?(String)
@description << " with id #{options[:id]}" if options[:id]
@description << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]
@ -167,9 +167,8 @@ module Capybara
def assert_valid_keys
super
unless VALID_MATCH.include?(match)
raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(", ")}"
end
return if VALID_MATCH.include?(match)
raise ArgumentError, "invalid option #{match.inspect} for :match, should be one of #{VALID_MATCH.map(&:inspect).join(', ')}"
end
def filtered_xpath(expr)

View File

@ -39,7 +39,7 @@ module Capybara
if @expected_text.is_a?(Regexp)
"text matching #{@expected_text.inspect}"
else
"#{"exact " if exact?}text #{@expected_text.inspect}"
"#{'exact ' if exact?}text #{@expected_text.inspect}"
end
end
@ -68,16 +68,15 @@ module Capybara
def case_insensitive_message
insensitive_regexp = Capybara::Helpers.to_regexp(@expected_text, options: Regexp::IGNORECASE)
insensitive_count = @actual_text.scan(insensitive_regexp).size
if insensitive_count != @count
"it was found #{insensitive_count} #{Capybara::Helpers.declension("time", "times", insensitive_count)} using a case insensitive search"
end
return if insensitive_count == @count
"it was found #{insensitive_count} #{Capybara::Helpers.declension('time', 'times', insensitive_count)} using a case insensitive search"
end
def invisible_message
invisible_text = text(@node, :all)
invisible_count = invisible_text.scan(@search_regexp).size
if invisible_count != @count
"it was found #{invisible_count} #{Capybara::Helpers.declension("time", "times", invisible_count)} including non-visible text"
"it was found #{invisible_count} #{Capybara::Helpers.declension('time', 'times', invisible_count)} including non-visible text"
end
rescue # An error getting the non-visible text (if element goes out of scope) should not affect the response
end

View File

@ -210,9 +210,7 @@ private
find_xpath(".//input")
end.first
if labelled_control && (labelled_control.checkbox? || labelled_control.radio?)
labelled_control.set(!labelled_control.checked?)
end
labelled_control.set(!labelled_control.checked?) if checkbox_or_radio?(labelled_control)
end
def link?
@ -229,6 +227,10 @@ private
protected
def checkbox_or_radio?(field = self)
field && (field.checkbox? || field.radio?)
end
def checkbox?
input_field? && type == 'checkbox'
end

View File

@ -114,7 +114,7 @@ module Capybara
if count.zero?
message << " but there were no matches"
else
message << ", found #{count} #{Capybara::Helpers.declension("match", "matches", count)}: " << full_results.map(&:text).map(&:inspect).join(", ")
message << ", found #{count} #{Capybara::Helpers.declension('match', 'matches', count)}: " << full_results.map(&:text).map(&:inspect).join(", ")
end
unless rest.empty?
elements = rest.map { |el| el.text rescue "<<ERROR>>" }.map(&:inspect).join(", ")

View File

@ -23,11 +23,8 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base
::Selenium::WebDriver::Error.const_set('ElementClickInterceptedError', Class.new(::Selenium::WebDriver::Error::WebDriverError))
end
rescue LoadError => e
if e.message =~ /selenium-webdriver/
raise LoadError, "Capybara's selenium driver is unable to load `selenium-webdriver`, please install the gem and add `gem 'selenium-webdriver'` to your Gemfile if you are using bundler."
else
raise e
end
raise e if e.message !~ /selenium-webdriver/
raise LoadError, "Capybara's selenium driver is unable to load `selenium-webdriver`, please install the gem and add `gem 'selenium-webdriver'` to your Gemfile if you are using bundler."
end
def browser
@ -360,7 +357,7 @@ private
if response_text.nil?
"default_text"
else
"'#{response_text.gsub("\\", "\\\\\\").gsub("'", "\\\\'")}'"
"'#{response_text.gsub('\\', '\\\\\\').gsub("'", "\\\\'")}'"
end
else
'null'
@ -452,11 +449,8 @@ private
if called
execute_script('window.capybara && window.capybara.modal_handlers.shift()')
regexp = text.is_a?(Regexp) ? text : Regexp.escape(text.to_s)
if alert_text.match(regexp)
alert_text
else
raise Capybara::ModalNotFound, "Unable to find modal dialog#{" with #{text}" if text}"
end
raise Capybara::ModalNotFound, "Unable to find modal dialog#{" with #{text}" if text}" unless alert_text.match(regexp)
alert_text
elsif called.nil?
# page changed so modal_handler data has gone away
warn "Can't verify modal text when page change occurs - ignoring" if options[:text]

View File

@ -137,18 +137,17 @@ module Capybara
# Raise errors encountered in the server
#
def raise_server_error!
if @server and @server.error
# Force an explanation for the error being raised as the exception cause
begin
if config.raise_server_errors
raise CapybaraError, "Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true"
end
rescue CapybaraError
# needed to get the cause set correctly in JRuby -- otherwise we could just do raise @server.error
raise @server.error, @server.error.message, @server.error.backtrace
ensure
@server.reset_error!
return if @server.nil? || !@server.error
# Force an explanation for the error being raised as the exception cause
begin
if config.raise_server_errors
raise CapybaraError, "Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true"
end
rescue CapybaraError
# needed to get the cause set correctly in JRuby -- otherwise we could just do raise @server.error
raise @server.error, @server.error.message, @server.error.backtrace
ensure
@server.reset_error!
end
end

View File

@ -96,7 +96,7 @@ Capybara::SpecHelper.spec "#all" do
context 'with element count filters' do
context ':count' do
it 'should succeed when the number of elements founds matches the expectation' do
expect { @session.all(:css, 'h1, p', count: 4) }.to_not raise_error
expect { @session.all(:css, 'h1, p', count: 4) }.not_to raise_error
end
it 'should raise ExpectationNotMet when the number of elements founds does not match the expectation' do
expect { @session.all(:css, 'h1, p', count: 5) }.to raise_error(Capybara::ExpectationNotMet)
@ -104,7 +104,7 @@ Capybara::SpecHelper.spec "#all" do
end
context ':minimum' do
it 'should succeed when the number of elements founds matches the expectation' do
expect { @session.all(:css, 'h1, p', minimum: 0) }.to_not raise_error
expect { @session.all(:css, 'h1, p', minimum: 0) }.not_to raise_error
end
it 'should raise ExpectationNotMet when the number of elements founds does not match the expectation' do
expect { @session.all(:css, 'h1, p', minimum: 5) }.to raise_error(Capybara::ExpectationNotMet)
@ -112,7 +112,7 @@ Capybara::SpecHelper.spec "#all" do
end
context ':maximum' do
it 'should succeed when the number of elements founds matches the expectation' do
expect { @session.all(:css, 'h1, p', maximum: 4) }.to_not raise_error
expect { @session.all(:css, 'h1, p', maximum: 4) }.not_to raise_error
end
it 'should raise ExpectationNotMet when the number of elements founds does not match the expectation' do
expect { @session.all(:css, 'h1, p', maximum: 0) }.to raise_error(Capybara::ExpectationNotMet)
@ -120,7 +120,7 @@ Capybara::SpecHelper.spec "#all" do
end
context ':between' do
it 'should succeed when the number of elements founds matches the expectation' do
expect { @session.all(:css, 'h1, p', between: 2..7) }.to_not raise_error
expect { @session.all(:css, 'h1, p', between: 2..7) }.not_to raise_error
end
it 'should raise ExpectationNotMet when the number of elements founds does not match the expectation' do
expect { @session.all(:css, 'h1, p', between: 0..3) }.to raise_error(Capybara::ExpectationNotMet)
@ -133,7 +133,7 @@ Capybara::SpecHelper.spec "#all" do
minimum: 5,
maximum: 0,
between: 0..3 }
expect { @session.all(:css, 'h1, p', o) }.to_not raise_error
expect { @session.all(:css, 'h1, p', o) }.not_to raise_error
end
context 'with no :count expectation' do
it 'fails if :minimum is not met' do
@ -158,7 +158,7 @@ Capybara::SpecHelper.spec "#all" do
o = { minimum: 0,
maximum: 4,
between: 2..7 }
expect { @session.all(:css, 'h1, p', o) }.to_not raise_error
expect { @session.all(:css, 'h1, p', o) }.not_to raise_error
end
end
end

View File

@ -37,8 +37,7 @@ Capybara::SpecHelper.spec '#assert_current_path' do
end
it "should not cause an exception when current_url is nil" do
allow_any_instance_of(Capybara::Session).to receive(:current_url) { nil }
allow_any_instance_of(Capybara::Session).to receive(:current_url).and_return(nil)
expect { @session.assert_current_path(nil) }.not_to raise_error
end
end
@ -66,7 +65,7 @@ Capybara::SpecHelper.spec '#assert_no_current_path?' do
end
it "should not cause an exception when current_url is nil" do
allow_any_instance_of(Capybara::Session).to receive(:current_url) { nil }
allow_any_instance_of(Capybara::Session).to receive(:current_url).and_return(nil)
expect { @session.assert_no_current_path('/with_html') }.not_to raise_error
end

View File

@ -128,7 +128,7 @@ Capybara::SpecHelper.spec '#assert_text' do
end
context 'with multiple count filters' do
before(:each) do
before do
@session.visit('/with_html')
end

View File

@ -128,21 +128,21 @@ Capybara::SpecHelper.spec "#check" do
end
it "should check via clicking the label with :for attribute if possible" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be_truthy
@session.check('form_cars_tesla')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('tesla')
end
it "should check via clicking the wrapping label if possible" do
expect(@session.find(:checkbox, 'form_cars_mclaren', unchecked: true, visible: :hidden)).to be
expect(@session.find(:checkbox, 'form_cars_mclaren', unchecked: true, visible: :hidden)).to be_truthy
@session.check('form_cars_mclaren')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('mclaren')
end
it "should not click the label if unneeded" do
expect(@session.find(:checkbox, 'form_cars_jaguar', checked: true, visible: :hidden)).to be
expect(@session.find(:checkbox, 'form_cars_jaguar', checked: true, visible: :hidden)).to be_truthy
@session.check('form_cars_jaguar')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('jaguar')
@ -170,14 +170,14 @@ Capybara::SpecHelper.spec "#check" do
context "with allow_label_click == true" do
it "should check via the label if input is hidden" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be_truthy
@session.check('form_cars_tesla', allow_label_click: true)
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('tesla')
end
it "should not wait the full time if label can be clicked" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be_truthy
start_time = Time.now
@session.check('form_cars_tesla', allow_label_click: true, wait: 10)
end_time = Time.now
@ -185,7 +185,7 @@ Capybara::SpecHelper.spec "#check" do
end
it "should check via the label if input is moved off the left edge of the page" do
expect(@session.find(:checkbox, 'form_cars_pagani', unchecked: true, visible: :all)).to be
expect(@session.find(:checkbox, 'form_cars_pagani', unchecked: true, visible: :all)).to be_truthy
@session.check('form_cars_pagani', allow_label_click: true)
@session.click_button('awesome')
expect(extract_results(@session)['cars']).to include('pagani')

View File

@ -102,13 +102,6 @@ Capybara::SpecHelper.spec '#click_link_or_button' do
@session.click_link_or_button('Disabled link')
expect(@session).to have_content("Bar")
end
it "does nothing when button is disabled" do
@session.visit('/form')
expect do
@session.click_link_or_button('Disabled button', disabled: false)
end.to raise_error(Capybara::ElementNotFound)
end
end
it "should return the element clicked" do

View File

@ -3,11 +3,11 @@
require "capybara/spec/test_app"
Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do
before :all do
before :all do # rubocop:disable RSpec/BeforeAfterAll
@servers = Array.new(2) { Capybara::Server.new(TestApp.new).boot }
# sanity check
expect(@servers[0].port).not_to eq(@servers[1].port)
expect(@servers.map(&:port)).not_to include 80
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
end
def bases
@ -23,10 +23,8 @@ Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do
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)
if path == '/host'
# Server should agree with us
expect(@session).to have_content("Current host is #{scheme}://#{s.host}:#{s.port}")
end
# Server should agree with us
expect(@session).to have_content("Current host is #{scheme}://#{s.host}:#{s.port}") if path == '/host'
end
def visit_host_links
@ -100,7 +98,7 @@ Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do
it "doesn't raise exception on a nil current_url" do
skip "Only makes sense when there is a real driver" unless @session.respond_to?(:driver)
allow(@session.driver).to receive(:current_url) { nil }
allow(@session.driver).to receive(:current_url).and_return(nil)
@session.visit("/")
expect { @session.current_url }.not_to raise_exception
expect { @session.current_path }.not_to raise_exception

View File

@ -24,7 +24,7 @@ Capybara::SpecHelper.spec '#find_by_id' do
expect(@session.find_by_id("hidden_via_ancestor", visible: false).text(:all)).to match(/with hidden ancestor/)
end
it "finds invisible elements when `false`" do
it "doesn't find invisible elements when `true`" do
expect do
@session.find_by_id("hidden_via_ancestor", visible: true)
end.to raise_error(Capybara::ElementNotFound)

View File

@ -9,7 +9,7 @@ Capybara::SpecHelper.spec '#find_field' do
expect(@session.find_field('Dog').value).to eq('dog')
expect(@session.find_field('form_description').value).to eq('Descriptive text goes here')
expect(@session.find_field('Region')[:name]).to eq('form[region]')
expect(@session.find_field('With Asterisk*')).to be
expect(@session.find_field('With Asterisk*')).to be_truthy
end
context "aria_label attribute with Capybara.enable_aria_label" do

View File

@ -65,20 +65,20 @@ Capybara::SpecHelper.spec '#first' do
end
it "should find nodes regardless of whether they are invisible when false" do
expect(@session.first(:css, "a#invisible", visible: false)).to be
expect(@session.first(:css, "a#invisible", visible: false, text: 'hidden link')).to be
expect(@session.first(:css, "a#visible", visible: false)).to be
expect(@session.first(:css, "a#invisible", visible: false)).to be_truthy
expect(@session.first(:css, "a#invisible", visible: false, text: 'hidden link')).to be_truthy
expect(@session.first(:css, "a#visible", visible: false)).to be_truthy
end
it "should find nodes regardless of whether they are invisible when :all" do
expect(@session.first(:css, "a#invisible", visible: :all)).to be
expect(@session.first(:css, "a#invisible", visible: :all, text: 'hidden link')).to be
expect(@session.first(:css, "a#visible", visible: :all)).to be
expect(@session.first(:css, "a#invisible", visible: :all)).to be_truthy
expect(@session.first(:css, "a#invisible", visible: :all, text: 'hidden link')).to be_truthy
expect(@session.first(:css, "a#visible", visible: :all)).to be_truthy
end
it "should find only hidden nodes when :hidden" do
expect(@session.first(:css, "a#invisible", visible: :hidden)).to be
expect(@session.first(:css, "a#invisible", visible: :hidden, text: 'hidden link')).to be
expect(@session.first(:css, "a#invisible", visible: :hidden)).to be_truthy
expect(@session.first(:css, "a#invisible", visible: :hidden, text: 'hidden link')).to be_truthy
expect do
@session.first(:css, "a#invisible", visible: :hidden, text: 'not hidden link')
end.to raise_error Capybara::ElementNotFound
@ -94,7 +94,7 @@ Capybara::SpecHelper.spec '#first' do
expect do
@session.first(:css, "a#invisible", visible: :visible, text: 'hidden link')
end.to raise_error Capybara::ElementNotFound
expect(@session.first(:css, "a#visible", visible: :visible)).to be
expect(@session.first(:css, "a#visible", visible: :visible)).to be_truthy
end
it "should default to Capybara.ignore_hidden_elements" do
@ -103,8 +103,8 @@ Capybara::SpecHelper.spec '#first' do
@session.first(:css, "a#invisible")
end.to raise_error Capybara::ElementNotFound
Capybara.ignore_hidden_elements = false
expect(@session.first(:css, "a#invisible")).to be
expect(@session.first(:css, "a")).to be
expect(@session.first(:css, "a#invisible")).to be_truthy
expect(@session.first(:css, "a")).to be_truthy
end
end
@ -115,7 +115,7 @@ Capybara::SpecHelper.spec '#first' do
it "should find the first element using the given locator" do
@session.within(:xpath, "//div[@id='for_bar']") do
expect(@session.first('.//form')).to be
expect(@session.first('.//form')).to be_truthy
end
end
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#frame_title', requires: [:frames] do
before(:each) do
before do
@session.visit('/within_frames')
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#frame_url', requires: [:frames] do
before(:each) do
before do
@session.visit('/within_frames')
end

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#switch_to_frame', requires: [:frames] do
before(:each) do
before do
@session.visit('/within_frames')
end
after(:each) do
after do
# Ensure we clean up after the frame changes
@session.switch_to_frame(:top)
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#within_frame', requires: [:frames] do
before(:each) do
before do
@session.visit('/within_frames')
end

View File

@ -81,7 +81,7 @@ Capybara::SpecHelper.spec '#has_current_path?' do
end
it "should not raise an exception if the current_url is nil" do
allow_any_instance_of(Capybara::Session).to receive(:current_url) { nil }
allow_any_instance_of(Capybara::Session).to receive(:current_url).and_return(nil)
# Without ignore_query option
expect do
@ -121,7 +121,7 @@ Capybara::SpecHelper.spec '#has_no_current_path?' do
end
it "should not raise an exception if the current_url is nil" do
allow_any_instance_of(Capybara::Session).to receive(:current_url) { nil }
allow_any_instance_of(Capybara::Session).to receive(:current_url).and_return(nil)
# Without ignore_query option
expect do

View File

@ -269,7 +269,7 @@ Capybara::SpecHelper.spec '#has_unchecked_field?' do
end
it "should support locator-less usage" do
expect(@session.has_unchecked_field?(disabled: true, id: "form_disabled_unchecked_checkbox"))
expect(@session.has_unchecked_field?(disabled: true, id: "form_disabled_unchecked_checkbox")).to be true
expect(@session).to have_unchecked_field(disabled: true, id: "form_disabled_unchecked_checkbox")
end
end

View File

@ -98,7 +98,7 @@ Capybara::SpecHelper.spec "node" do
expect { @session.first('//input[@readonly]').set('changed') }.to raise_error(Capybara::ReadOnlyElementError)
end
it "should raise if the text field is readonly" do
it "should raise if the textarea is readonly" do
expect { @session.first('//textarea[@readonly]').set('changed') }.to raise_error(Capybara::ReadOnlyElementError)
end
@ -251,7 +251,7 @@ Capybara::SpecHelper.spec "node" do
describe "#path" do
# Testing for specific XPaths here doesn't make sense since there
# are many that can refer to the same element
before :each do
before do
@session.visit('/path')
end
@ -372,7 +372,7 @@ Capybara::SpecHelper.spec "node" do
it "should double click an element" do
@session.visit('/with_js')
@session.find(:css, '#click-test').double_click
expect(@session.find(:css, '#has-been-double-clicked')).to be
expect(@session.find(:css, '#has-been-double-clicked')).to be_truthy
end
it "should allow modifiers", requires: [:js] do
@ -397,7 +397,7 @@ Capybara::SpecHelper.spec "node" do
it "should right click an element" do
@session.visit('/with_js')
@session.find(:css, '#click-test').right_click
expect(@session.find(:css, '#has-been-right-clicked')).to be
expect(@session.find(:css, '#has-been-right-clicked')).to be_truthy
end
it "should allow modifiers", requires: [:js] do

View File

@ -18,8 +18,8 @@ Capybara::SpecHelper.spec '#reset_session!' do
expect(@session.current_path).to eq('/foo')
@session.reset_session!
expect([nil, '', 'about:blank']).to include(@session.current_url)
expect(['', nil]).to include(@session.current_path)
expect(@session.current_url).to satisfy('be a blank url') { |url| [nil, '', 'about:blank'].include? url }
expect(@session.current_path).to satisfy('be a blank path') { |path| ['', nil].include? path }
expect(@session.current_host).to be_nil
end

View File

@ -34,9 +34,10 @@ Capybara::SpecHelper.spec '#save_and_open_screenshot' do
context 'when launchy cannot be required' do
it 'prints out a correct warning message', requires: [:screenshot] do
file_path = File.join(Dir.tmpdir, 'test.png')
allow(@session).to receive(:warn)
allow(@session).to receive(:require).with('launchy').and_raise(LoadError)
expect(@session).to receive(:warn).with("File saved to #{file_path}.\nPlease install the launchy gem to open the file automatically.")
@session.save_and_open_screenshot(file_path)
expect(@session).to have_received(:warn).with("File saved to #{file_path}.\nPlease install the launchy gem to open the file automatically.")
end
end
end

View File

@ -56,21 +56,21 @@ Capybara::SpecHelper.spec "#uncheck" do
end
it "should uncheck via clicking the label with :for attribute if possible" do
expect(@session.find(:checkbox, 'form_cars_jaguar', checked: true, visible: :hidden)).to be
expect(@session.find(:checkbox, 'form_cars_jaguar', checked: true, visible: :hidden)).to be_truthy
@session.uncheck('form_cars_jaguar')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('jaguar')
end
it "should uncheck via clicking the wrapping label if possible" do
expect(@session.find(:checkbox, 'form_cars_koenigsegg', checked: true, visible: :hidden)).to be
expect(@session.find(:checkbox, 'form_cars_koenigsegg', checked: true, visible: :hidden)).to be_truthy
@session.uncheck('form_cars_koenigsegg')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('koenigsegg')
end
it "should not click the label if unneeded" do
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be
expect(@session.find(:checkbox, 'form_cars_tesla', unchecked: true, visible: :hidden)).to be_truthy
@session.uncheck('form_cars_tesla')
@session.click_button('awesome')
expect(extract_results(@session)['cars']).not_to include('tesla')

View File

@ -54,11 +54,11 @@ Capybara::SpecHelper.spec '#visit' do
URI.parse(@session.current_url)
end
before(:each) do
before do
Capybara.always_include_port = true
end
after(:each) do
after do
Capybara.always_include_port = false
end
@ -73,52 +73,59 @@ Capybara::SpecHelper.spec '#visit' do
end
it "should add the server port to a visited url if no port specified", requires: [:server] do
expect(@session.driver).to receive(:visit).with("http://www.example.com:#{@session.server.port}")
allow(@session.driver).to receive(:visit)
@session.visit("http://www.example.com")
expect(@session.driver).to have_received(:visit).with("http://www.example.com:#{@session.server.port}")
end
it "should not override the visit specified port even if default for scheme", requires: [:server] do
expect(@session.driver).to receive(:visit).with("http://www.example.com:80")
allow(@session.driver).to receive(:visit)
@session.visit('http://www.example.com:80')
expect(@session.driver).to have_received(:visit).with("http://www.example.com:80")
end
it "should give preference to app_host port if specified", requires: [:server] do
allow(@session.driver).to receive(:visit)
Capybara.app_host = "http://www.example.com:6666"
expect(@session.driver).to receive(:visit).with("http://www.example.com:6666/random")
@session.visit('/random')
expect(@session.driver).to have_received(:visit).with("http://www.example.com:6666/random")
end
it "shouldn't override port if no server", requires: [:server] do
session = Capybara::Session.new(@session.mode, nil)
expect(session.driver).to receive(:visit).with("http://www.google.com")
allow(session.driver).to receive(:visit)
session.visit("http://www.google.com")
expect(session.driver).to have_received(:visit).with("http://www.google.com")
end
it "shouldn't override port if no server but app_host is set", requires: [:server] do
session = Capybara::Session.new(@session.mode, nil)
Capybara.app_host = "http://www.example.com:6666"
expect(session.driver).to receive(:visit).with("http://www.google.com")
allow(session.driver).to receive(:visit)
session.visit("http://www.google.com")
expect(session.driver).to have_received(:visit).with("http://www.google.com")
end
end
context "when Capybara.always_include_port is false" do
before(:each) do
before do
Capybara.always_include_port = false
end
it "shouldn't overwrite port if app_host is set", requires: [:server] do
session = Capybara::Session.new(@session.mode, nil)
Capybara.app_host = "http://www.example.com:6666"
expect(session.driver).to receive(:visit).with("http://www.google.com")
allow(session.driver).to receive(:visit)
session.visit("http://www.google.com")
expect(session.driver).to have_received(:visit).with("http://www.google.com")
end
it "shouldn't overwrite port if port specfified", requires: [:server] do
session = Capybara::Session.new(@session.mode, nil)
Capybara.app_host = "http://www.example.com:6666"
expect(session.driver).to receive(:visit).with("http://www.google.com:99")
allow(session.driver).to receive(:visit)
session.visit("http://www.google.com:99")
expect(session.driver).to have_received(:visit).with("http://www.google.com:99")
end
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#become_closed', requires: %i[windows js] do
before(:each) do
before do
@window = @session.current_window
@session.visit('/with_windows')
@other_window = @session.window_opened_by do
@ -9,7 +9,7 @@ Capybara::SpecHelper.spec '#become_closed', requires: %i[windows js] do
end
end
after(:each) do
after do
@session.document.synchronize(5, errors: [Capybara::CapybaraError]) do
raise Capybara::CapybaraError if @session.windows.size != 1
end
@ -68,7 +68,7 @@ Capybara::SpecHelper.spec '#become_closed', requires: %i[windows js] do
Capybara.using_wait_time 0.3 do
expect do
expect(@other_window).not_to become_closed
end
end.not_to raise_error
end
end

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#current_window', requires: [:windows] do
before(:each) do
before do
@window = @session.current_window
@session.visit('/with_windows')
end
after(:each) do
after do
(@session.windows - [@window]).each do |w|
@session.switch_to_window w
w.close

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#open_new_window', requires: [:windows] do
before(:each) do
before do
@window = @session.current_window
@session.visit('/with_windows')
end
after(:each) do
after do
(@session.windows - [@window]).each do |w|
@session.switch_to_window w
w.close
@ -16,7 +16,7 @@ Capybara::SpecHelper.spec '#open_new_window', requires: [:windows] do
it 'should open new window with blank url and title' do
window = @session.open_new_window
@session.switch_to_window(window)
expect(['', 'about:blank']).to include(@session.title)
expect(@session.title).to satisfy('be a blank title') { |title| ['', 'about:blank'].include? title }
expect(@session.current_url).to eq('about:blank')
end

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#switch_to_window', requires: [:windows] do
before(:each) do
before do
@window = @session.current_window
@session.visit('/with_windows')
expect(@session).to have_css('body.loaded')
@session.assert_selector(:css, 'body.loaded')
end
after(:each) do
after do
(@session.windows - [@window]).each do |w|
@session.switch_to_window w
w.close
@ -32,7 +32,7 @@ Capybara::SpecHelper.spec '#switch_to_window', requires: [:windows] do
window = @session.open_new_window
expect(@session.title).to eq('With Windows')
@session.switch_to_window(window)
expect(['', 'about:blank']).to include(@session.title)
expect(@session.title).to satisfy('be a blank title') { |title| ['', 'about:blank'].include? title }
end
it "should raise error when closed window is passed" do
@ -48,7 +48,7 @@ Capybara::SpecHelper.spec '#switch_to_window', requires: [:windows] do
end
context "with block" do
before(:each) do
before do
@session.find(:css, '#openTwoWindows').click
sleep(1) # wait for the windows to open
end

View File

@ -1,12 +1,12 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#window_opened_by', requires: [:windows] do
before(:each) do
before do
@window = @session.current_window
@session.visit('/with_windows')
expect(@session).to have_css('body.loaded')
@session.assert_selector(:css, 'body.loaded')
end
after(:each) do
after do
(@session.windows - [@window]).each do |w|
@session.switch_to_window w
w.close

View File

@ -1,11 +1,11 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
before(:each) do
before do
@window = @session.current_window
@session.visit('/with_windows')
end
after(:each) do
after do
(@session.windows - [@window]).each do |w|
@session.switch_to_window w
w.close
@ -14,7 +14,7 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end
describe '#exists?' do
before(:each) do
before do
@other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
@ -41,7 +41,7 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end
describe '#current?' do
before(:each) do
before do
@other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end
@ -61,7 +61,7 @@ Capybara::SpecHelper.spec Capybara::Window, requires: [:windows] do
end
describe '#close' do
before(:each) do
before do
@other_window = @session.window_opened_by do
@session.find(:css, '#openWindow').click
end

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#windows', requires: [:windows] do
before(:each) do
before do
@window = @session.current_window
@session.visit('/with_windows')
@session.find(:css, '#openTwoWindows').click
@ -10,7 +10,7 @@ Capybara::SpecHelper.spec '#windows', requires: [:windows] do
raise Capybara::CapybaraError if @session.windows.size != 3
end
end
after(:each) do
after do
(@session.windows - [@window]).each do |w|
@session.switch_to_window w
w.close

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
Capybara::SpecHelper.spec '#within_window', requires: [:windows] do
before(:each) do
before do
@window = @session.current_window
@session.visit('/with_windows')
@session.find(:css, '#openTwoWindows').click
@ -10,7 +10,7 @@ Capybara::SpecHelper.spec '#within_window', requires: [:windows] do
raise Capybara::CapybaraError if @session.windows.size != 3
end
end
after(:each) do
after do
(@session.windows - [@window]).each do |w|
@session.switch_to_window w
w.close

View File

@ -7,7 +7,7 @@ Capybara::SpecHelper.spec '#within' do
context "with CSS selector" do
it "should click links in the given scope" do
@session.within(:css, "#for_bar li", text: 'With Simple HTML') do
@session.within(:css, "#for_bar li:first-child") do
@session.click_link('Go')
end
expect(@session).to have_content('Bar')
@ -103,9 +103,9 @@ Capybara::SpecHelper.spec '#within' do
@session.within(".//div[@id='doesnotexist']") do
end
end.to raise_error(Capybara::ElementNotFound)
end.to_not change { @session.has_xpath?(".//div[@id='another_foo']") }.from(false)
end.not_to change { @session.has_xpath?(".//div[@id='another_foo']") }.from(false)
end
end.to_not change { @session.has_xpath?(".//div[@id='another_foo']") }.from(true)
end.not_to change { @session.has_xpath?(".//div[@id='another_foo']") }.from(true)
end
it "should fill in a field and click a button" do

View File

@ -49,31 +49,33 @@ module Capybara
def run_specs(session, name, **options)
specs = @specs
RSpec.describe Capybara::Session, name, options do
RSpec.describe Capybara::Session, name, options do # rubocop:disable RSpec/EmptyExampleGroup
include Capybara::SpecHelper
include Capybara::RSpecMatchers
# rubocop:disable RSpec/ScatteredSetup
before do
@session = session
end
after do
@session.reset_session!
session.reset_session!
end
before :each, psc: true do
SpecHelper.reset_threadsafe(true, @session)
SpecHelper.reset_threadsafe(true, session)
end
after psc: true do
SpecHelper.reset_threadsafe(false, @session)
SpecHelper.reset_threadsafe(false, session)
end
before :each, :exact_false do
Capybara.exact = false
end
# rubocop:enable RSpec/ScatteredSetup
specs.each do |spec_name, spec_options, block|
describe spec_name, *spec_options do
describe spec_name, *spec_options do # rubocop:disable RSpec/EmptyExampleGroup
class_eval(&block)
end
end

View File

@ -95,7 +95,7 @@ RSpec.describe Capybara do
expect(string.find('//h1').path).to eq('/html/body/div/div[1]/h1')
end
it "allows finding elements and extracting the path" do
it "allows finding elements and extracting the value" do
expect(string.find('//div/input').value).to eq('bar')
expect(string.find('//select').value).to eq('Capybara')
end

View File

@ -28,9 +28,8 @@ RSpec.describe Capybara do
describe '.register_server' do
it "should add a new server" do
handler = double("handler")
Capybara.register_server :blob do |_app, _port, _host|
handler.run
# do nothing
end
expect(Capybara.servers).to have_key(:blob)
@ -43,9 +42,10 @@ RSpec.describe Capybara do
end
it "should default to a proc that calls run_default_server" do
mock_app = double('app')
expect(Capybara).to receive(:run_default_server).with(mock_app, 8000)
mock_app = Object.new
allow(Capybara).to receive(:run_default_server)
Capybara.server.call(mock_app, 8000)
expect(Capybara).to have_received(:run_default_server).with(mock_app, 8000)
end
it "should return a custom server proc" do
@ -110,13 +110,3 @@ RSpec.describe Capybara do
end
end
end
RSpec.describe Capybara::Session do
context 'with nonexistent driver' do
it "should raise an error" do
expect do
Capybara::Session.new(:quox, TestApp).driver
end.to raise_error(Capybara::DriverNotFoundError)
end
end
end

View File

@ -65,7 +65,7 @@ RSpec.describe Capybara::DSL do
describe '#using_driver' do
before do
expect(Capybara.current_driver).not_to eq(:selenium)
expect(Capybara.current_driver).not_to eq(:selenium) # rubocop:disable RSpec/ExpectInHook
end
it 'should set the driver using Capybara.current_driver=' do
@ -250,13 +250,15 @@ RSpec.describe Capybara::DSL do
end
it "should provide an 'using_session' shortcut" do
expect(Capybara).to receive(:using_session).with(:name)
allow(Capybara).to receive(:using_session)
@session.using_session(:name)
expect(Capybara).to have_received(:using_session).with(:name)
end
it "should provide a 'using_wait_time' shortcut" do
expect(Capybara).to receive(:using_wait_time).with(6)
allow(Capybara).to receive(:using_wait_time)
@session.using_wait_time(6)
expect(Capybara).to have_received(:using_wait_time).with(6)
end
end
end

View File

@ -7,7 +7,7 @@ RSpec.describe Capybara::Selenium::Driver do
it "should exit with a non-zero exit status" do
options = { browser: (ENV['SELENIUM_BROWSER'] || :firefox).to_sym }
browser = Capybara::Selenium::Driver.new(TestApp, options).browser
expect(browser).to be
expect(true).to eq(false)
expect(browser).to be_truthy
expect(true).to eq(false) # rubocop:disable RSpec/ExpectActual
end
end

View File

@ -7,7 +7,7 @@ RSpec.describe Capybara::Selenium::Driver do
it "should exit with a zero exit status" do
options = { browser: (ENV['SELENIUM_BROWSER'] || :firefox).to_sym }
browser = Capybara::Selenium::Driver.new(TestApp, options).browser
expect(browser).to be
expect(true).to eq(true)
expect(browser).to be_truthy
expect(true).to eq(true) # rubocop:disable RSpec/ExpectActual
end
end

View File

@ -19,7 +19,7 @@ skipped_tests = %i[
]
Capybara::SpecHelper.run_specs TestSessions::RackTest, "RackTest", capybara_skip: skipped_tests
RSpec.describe Capybara::Session do
RSpec.describe Capybara::Session do # rubocop:disable RSpec/MultipleDescribes
context 'with rack test driver' do
before do
@session = TestSessions::RackTest
@ -66,11 +66,11 @@ RSpec.describe Capybara::Session do
describe "#fill_in" do
it "should warn that :fill_options are not supported" do
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")
allow_any_instance_of(Capybara::RackTest::Node).to receive(:warn)
@session.visit "/with_html"
@session.fill_in 'test_field', with: 'not_monkey', fill_options: { random: true }
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
@ -221,9 +221,10 @@ module CSSHandlerIncludeTester
raise 'should never be called'
end
end
include CSSHandlerIncludeTester # rubocop:disable Style/MixinUsage
RSpec.describe Capybara::RackTest::CSSHandlers do
include CSSHandlerIncludeTester
it "should not be extended by global includes" do
expect(Capybara::RackTest::CSSHandlers.new).not_to respond_to(:dont_extend_css_handler)
end

View File

@ -79,14 +79,18 @@ RSpec.describe Capybara::Result do
it 'should catch invalid element errors during filtering' do
allow_any_instance_of(Capybara::Node::Simple).to receive(:text).and_raise(StandardError)
allow_any_instance_of(Capybara::Node::Simple).to receive(:session).and_return(double("session", driver: double("driver", invalid_element_errors: [StandardError])))
allow_any_instance_of(Capybara::Node::Simple).to receive(:session).and_return(
instance_double("Capybara::Session", driver: instance_double("Capybara::Driver::Base", invalid_element_errors: [StandardError]))
)
result = string.all('//li', text: 'Alpha')
expect(result.size).to eq 0
end
it 'should return non-invalid element errors during filtering' do
allow_any_instance_of(Capybara::Node::Simple).to receive(:text).and_raise(StandardError)
allow_any_instance_of(Capybara::Node::Simple).to receive(:session).and_return(double("session", driver: double("driver", invalid_element_errors: [ArgumentError])))
allow_any_instance_of(Capybara::Node::Simple).to receive(:session).and_return(
instance_double("Capybara::Session", driver: instance_double("Capybara::Driver::Base", invalid_element_errors: [ArgumentError]))
)
expect do
string.all('//li', text: 'Alpha').to_a
end.to raise_error(StandardError)

View File

@ -39,7 +39,7 @@ feature "Capybara's feature DSL" do
end
scenario "doesn't pollute the Object namespace" do
expect(Object.new.respond_to?(:feature, true)).to be_falsey
expect(Object.new).not_to respond_to(:feature)
end
feature 'nested features' do
@ -78,6 +78,7 @@ feature "Capybara's feature DSL with driver", driver: :culerity do
end
end
# rubocop:disable RSpec/RepeatedExample
xfeature "if xfeature aliases to pending then" do
scenario "this should be 'temporarily disabled with xfeature'" do
# dummy
@ -87,7 +88,7 @@ xfeature "if xfeature aliases to pending then" do
end
end
ffeature "if ffeature aliases focused tag then" do
ffeature "if ffeature aliases focused tag then" do # rubocop:disable RSpec/Focus
scenario "scenario inside this feature has metatag focus tag" do |example|
expect(example.metadata[:focus]).to eq true
end
@ -96,3 +97,4 @@ ffeature "if ffeature aliases focused tag then" do
expect(example.metadata[:focus]).to eq true
end
end
# rubocop:enable RSpec/RepeatedExample

View File

@ -8,7 +8,7 @@ RSpec.configuration.before(:each, file_path: "./spec/rspec/scenarios_spec.rb") d
end
feature "if fscenario aliases focused tag then" do
fscenario "scenario should have focused meta tag" do |example|
fscenario "scenario should have focused meta tag" do |example| # rubocop:disable RSpec/Focus
expect(example.metadata[:focus]).to eq true
end
end

View File

@ -5,6 +5,8 @@ require 'capybara/dsl'
require 'capybara/rspec/matchers'
require 'benchmark'
# rubocop:disable RSpec/ExpectActual
RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
include Capybara::DSL
include Capybara::RSpecMatchers
@ -548,7 +550,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
end
context 'with wait' do
before(:each) do
before do
@session = session
@session.visit('/with_js')
end
@ -593,7 +595,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
end
context 'with wait' do
before(:each) do
before do
@session = session
@session.visit('/with_js')
end
@ -841,7 +843,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
end
context "compounding timing" do
before(:each) do
before do
@session = session
@session.visit('/with_js')
@el = @session.find(:css, '#reload-me')
@ -934,3 +936,4 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
end
end
end
# rubocop:enable RSpec/ExpectActual

View File

@ -4,7 +4,8 @@ require 'spec_helper'
RSpec.describe "capybara/rspec", type: :view do
it "allows matchers to be used on strings" do
expect(%(<h1>Test header</h1>)).to have_css("h1", text: "Test header")
html = %(<h1>Test header</h1>)
expect(html).to have_css("h1", text: "Test header")
end
it "doesn't include RSpecMatcherProxies" do

View File

@ -2,118 +2,120 @@
require 'spec_helper'
RSpec.describe 'capybara/rspec', type: :feature do
it "should include Capybara in rspec" do
visit('/foo')
expect(page.body).to include('Another World')
end
it "should include RSpec matcher proxies" do
expect(self.class.ancestors).to include Capybara::RSpecMatcherProxies
end
context "resetting session" do
it "sets a cookie in one example..." do
visit('/set_cookie')
expect(page.body).to include('Cookie set to test_cookie')
RSpec.describe 'capybara/rspec' do
context "Feature", type: :feature do
it "should include Capybara in rspec" do
visit('/foo')
expect(page.body).to include('Another World')
end
it "...then it is not available in the next" do
visit('/get_cookie')
expect(page.body).not_to include('test_cookie')
end
end
context "setting the current driver" do
it "sets the current driver in one example..." do
Capybara.current_driver = :selenium
it "should include RSpec matcher proxies" do
expect(self.class.ancestors).to include Capybara::RSpecMatcherProxies
end
it "...then it has returned to the default in the next example" do
expect(Capybara.current_driver).to eq(:rack_test)
end
end
it "switches to the javascript driver when giving it as metadata", js: true do
expect(Capybara.current_driver).to eq(Capybara.javascript_driver)
end
it "switches to the given driver when giving it as metadata", driver: :culerity do
expect(Capybara.current_driver).to eq(:culerity)
end
context "#all" do
it "allows access to the Capybara finder" do
visit('/with_html')
found = all(:css, 'h2') { |element| element[:class] == 'head' }
expect(found.size).to eq(5)
end
it "allows access to the RSpec matcher" do
visit('/with_html')
expect(%w[test1 test2]).to all(be_a(String))
end
end
context "#within" do
it "allows access to the Capybara scoper" do
visit('/with_html')
expect do
within(:css, "#does_not_exist") { click_link "Go to simple" }
end.to raise_error(Capybara::ElementNotFound)
end
it "allows access to the RSpec matcher" do
visit('/with_html')
# This reads terribly, but must call #within
expect(find(:css, 'span.number').text.to_i).to within(1).of(41)
end
end
end
RSpec.describe 'capybara/rspec', type: :other do
context "when RSpec::Matchers is included after Capybara::DSL" do
before do
class DSLMatchersTest
include Capybara::DSL
include RSpec::Matchers
context "resetting session" do
it "sets a cookie in one example..." do
visit('/set_cookie')
expect(page.body).to include('Cookie set to test_cookie')
end
@test_class_instance = DSLMatchersTest.new
it "...then it is not available in the next" do
visit('/get_cookie')
expect(page.body).not_to include('test_cookie')
end
end
context "setting the current driver" do
it "sets the current driver in one example..." do
Capybara.current_driver = :selenium
end
it "...then it has returned to the default in the next example" do
expect(Capybara.current_driver).to eq(:rack_test)
end
end
it "switches to the javascript driver when giving it as metadata", js: true do
expect(Capybara.current_driver).to eq(Capybara.javascript_driver)
end
it "switches to the given driver when giving it as metadata", driver: :culerity do
expect(Capybara.current_driver).to eq(:culerity)
end
context "#all" do
it "allows access to the Capybara finder" do
@test_class_instance.visit('/with_html')
expect(@test_class_instance.all(:css, 'h2.head').size).to eq(5)
visit('/with_html')
found = all(:css, 'h2') { |element| element[:class] == 'head' }
expect(found.size).to eq(5)
end
it "allows access to the RSpec matcher" do
@test_class_instance.visit('/with_html')
expect(%w[test1 test2]).to @test_class_instance.all(be_a(String))
visit('/with_html')
strings = %w[test1 test2]
expect(strings).to all(be_a(String))
end
end
context "#within" do
it "allows access to the Capybara scoper" do
@test_class_instance.visit('/with_html')
visit('/with_html')
expect do
@test_class_instance.within(:css, "#does_not_exist") { @test_class_instance.click_link "Go to simple" }
within(:css, "#does_not_exist") { click_link "Go to simple" }
end.to raise_error(Capybara::ElementNotFound)
end
it "allows access to the RSpec matcher" do
@test_class_instance.visit('/with_html')
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)
expect(find(:css, 'span.number').text.to_i).to within(1).of(41)
end
end
end
end
RSpec.describe 'capybara/rspec', type: :other do
it "should not include Capybara" do
expect { visit('/') }.to raise_error(NoMethodError)
context 'Type: Other', type: :other do
context "when RSpec::Matchers is included after Capybara::DSL" do
before do
class DSLMatchersTest
include Capybara::DSL
include RSpec::Matchers
end
@test_class_instance = DSLMatchersTest.new
end
context "#all" do
it "allows access to the Capybara finder" do
@test_class_instance.visit('/with_html')
expect(@test_class_instance.all(:css, 'h2.head').size).to eq(5)
end
it "allows access to the RSpec matcher" do
@test_class_instance.visit('/with_html')
strings = %w[test1 test2]
expect(strings).to @test_class_instance.all(be_a(String))
end
end
context "#within" do
it "allows access to the Capybara scoper" do
@test_class_instance.visit('/with_html')
expect do
@test_class_instance.within(:css, "#does_not_exist") { @test_class_instance.click_link "Go to simple" }
end.to raise_error(Capybara::ElementNotFound)
end
it "allows access to the RSpec matcher" do
@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
end
it "should not include Capybara" do
expect { visit('/') }.to raise_error(NoMethodError)
end
end
end

View File

@ -44,7 +44,7 @@ $stdout.puts `#{Selenium::WebDriver::Firefox.driver_path} --version` if ENV['CI'
Capybara::SpecHelper.run_specs TestSessions::SeleniumMarionette, "selenium", capybara_skip: skipped_tests
RSpec.describe "Capybara::Session with firefox" do
RSpec.describe "Capybara::Session with firefox" do # rubocop:disable RSpec/MultipleDescribes
include Capybara::SpecHelper
include_examples "Capybara::Session", TestSessions::SeleniumMarionette, :selenium_marionette
include_examples Capybara::RSpecMatchers, TestSessions::SeleniumMarionette, :selenium_marionette
@ -57,7 +57,7 @@ RSpec.describe Capybara::Selenium::Driver do
describe '#quit' do
it "should reset browser when quit" do
expect(@driver.browser).to be
expect(@driver.browser).to be_truthy
@driver.quit
# access instance variable directly so we don't create a new browser instance
expect(@driver.instance_variable_get(:@browser)).to be_nil
@ -74,7 +74,7 @@ RSpec.describe Capybara::Selenium::Driver do
end
it "warns UnknownError returned during quit because the browser is probably already gone" do
expect_any_instance_of(Capybara::Selenium::Driver).to receive(:warn).with(/random message/)
allow(@driver).to receive(:warn)
allow(@driver.browser).to(
receive(:quit)
.and_raise(Selenium::WebDriver::Error::UnknownError, "random message")
@ -82,10 +82,11 @@ RSpec.describe Capybara::Selenium::Driver do
expect { @driver.quit }.not_to raise_error
expect(@driver.instance_variable_get(:@browser)).to be_nil
expect(@driver).to have_received(:warn).with(/random message/)
end
it "ignores silenced UnknownError returned during quit because the browser is almost definitely already gone" do
expect_any_instance_of(Capybara::Selenium::Driver).not_to receive(:warn)
allow(@driver).to receive(:warn)
allow(@driver.browser).to(
receive(:quit)
.and_raise(Selenium::WebDriver::Error::UnknownError, "Error communicating with the remote browser")
@ -93,6 +94,7 @@ RSpec.describe Capybara::Selenium::Driver do
expect { @driver.quit }.not_to raise_error
expect(@driver.instance_variable_get(:@browser)).to be_nil
expect(@driver).not_to have_received(:warn)
end
end
end

View File

@ -189,7 +189,7 @@ RSpec.describe Capybara::Server do
it "is not #responsive? when Net::HTTP raises a SystemCallError" do
app = -> { [200, {}, ['Hello, world']] }
server = Capybara::Server.new(app)
expect(Net::HTTP).to receive(:start).and_raise(SystemCallError.allocate)
allow(Net::HTTP).to receive(:start).and_raise(SystemCallError.allocate)
expect(server.responsive?).to eq false
end

View File

@ -3,10 +3,18 @@
require 'spec_helper'
RSpec.describe Capybara::Session do
it "verifies a passed app is a rack app" do
expect do
Capybara::Session.new(:unknown, random: "hash")
end.to raise_error TypeError, "The second parameter to Session::new should be a rack app if passed."
context "#new" do
it "should raise an error if passed non-existent driver" do
expect do
Capybara::Session.new(:quox, TestApp).driver
end.to raise_error(Capybara::DriverNotFoundError)
end
it "verifies a passed app is a rack app" do
expect do
Capybara::Session.new(:unknown, random: "hash")
end.to raise_error TypeError, "The second parameter to Session::new should be a rack app if passed."
end
end
context "current_driver" do

View File

@ -7,27 +7,23 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
let(:session) { session }
context 'with selenium driver' do
before do
@session = session
end
describe '#driver' do
it "should be a selenium driver" do
expect(@session.driver).to be_an_instance_of(Capybara::Selenium::Driver)
expect(session.driver).to be_an_instance_of(Capybara::Selenium::Driver)
end
end
describe '#mode' do
it "should remember the mode" do
expect(@session.mode).to eq(mode)
expect(session.mode).to eq(mode)
end
end
describe "#reset!" do
it "freshly reset session should not be touched" do
@session.instance_variable_set(:@touched, true)
@session.reset!
expect(@session.instance_variable_get(:@touched)).to eq false
session.instance_variable_set(:@touched, true)
session.reset!
expect(session.instance_variable_get(:@touched)).to eq false
end
end
@ -35,7 +31,7 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
before do
@current_dir = Dir.getwd
Dir.chdir(File.join(File.dirname(__FILE__), '..'))
@env = { 'SELENIUM_BROWSER' => @session.driver.options[:browser].to_s }
@env = { 'SELENIUM_BROWSER' => session.driver.options[:browser].to_s }
end
after do
@ -59,142 +55,140 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
describe "#accept_alert", requires: [:modals] do
it "supports a blockless mode" do
@session.visit('/with_js')
@session.click_link('Open alert')
@session.accept_alert
expect { @session.driver.browser.switch_to.alert }.to raise_error(@session.driver.send(:modal_error))
session.visit('/with_js')
session.click_link('Open alert')
session.accept_alert
expect { session.driver.browser.switch_to.alert }.to raise_error(session.driver.send(:modal_error))
end
it "can be called before visiting" do
@session.accept_alert "Initial alert" do
@session.visit('/initial_alert')
session.accept_alert "Initial alert" do
session.visit('/initial_alert')
end
expect(@session).to have_text('Initial alert page')
expect(session).to have_text('Initial alert page')
end
end
context '#fill_in_with empty string and no options' do
it 'should trigger change when clearing a field' do
@session.visit('/with_js')
@session.fill_in('with_change_event', with: '')
session.visit('/with_js')
session.fill_in('with_change_event', with: '')
# click outside the field to trigger the change event
@session.find(:css, 'body').click
expect(@session).to have_selector(:css, '.change_event_triggered', match: :one)
session.find(:css, 'body').click
expect(session).to have_selector(:css, '.change_event_triggered', match: :one)
end
end
context "#fill_in with { :clear => :backspace } fill_option", requires: [:js] do
it 'should fill in a field, replacing an existing value' do
@session.visit('/form')
@session.fill_in('form_first_name',
with: 'Harry',
fill_options: { clear: :backspace })
expect(@session.find(:fillable_field, 'form_first_name').value).to eq('Harry')
session.visit('/form')
session.fill_in('form_first_name',
with: 'Harry',
fill_options: { clear: :backspace })
expect(session.find(:fillable_field, 'form_first_name').value).to eq('Harry')
end
it 'should only trigger onchange once' do
@session.visit('/with_js')
@session.fill_in('with_change_event',
with: 'some value',
fill_options: { clear: :backspace })
session.visit('/with_js')
session.fill_in('with_change_event',
with: 'some value',
fill_options: { clear: :backspace })
# click outside the field to trigger the change event
@session.find(:css, 'body').click
expect(@session.find(:css, '.change_event_triggered', match: :one)).to have_text 'some value'
session.find(:css, 'body').click
expect(session.find(:css, '.change_event_triggered', match: :one)).to have_text 'some value'
end
it 'should trigger change when clearing field' do
@session.visit('/with_js')
@session.fill_in('with_change_event',
with: '',
fill_options: { clear: :backspace })
session.visit('/with_js')
session.fill_in('with_change_event',
with: '',
fill_options: { clear: :backspace })
# click outside the field to trigger the change event
@session.find(:css, 'body').click
expect(@session).to have_selector(:css, '.change_event_triggered', match: :one)
session.find(:css, 'body').click
expect(session).to have_selector(:css, '.change_event_triggered', match: :one)
end
it 'should trigger input event field_value.length times' do
@session.visit('/with_js')
@session.fill_in('with_change_event',
with: '',
fill_options: { clear: :backspace })
session.visit('/with_js')
session.fill_in('with_change_event',
with: '',
fill_options: { clear: :backspace })
# click outside the field to trigger the change event
@session.find(:css, 'body').click
expect(@session).to have_xpath('//p[@class="input_event_triggered"]', count: 13)
session.find(:css, 'body').click
expect(session).to have_xpath('//p[@class="input_event_triggered"]', count: 13)
end
end
context "#fill_in with { clear: :none } fill_options" do
it 'should append to content in a field' do
@session.visit('/form')
@session.fill_in('form_first_name',
with: 'Harry',
fill_options: { clear: :none })
expect(@session.find(:fillable_field, 'form_first_name').value).to eq('JohnHarry')
session.visit('/form')
session.fill_in('form_first_name',
with: 'Harry',
fill_options: { clear: :none })
expect(session.find(:fillable_field, 'form_first_name').value).to eq('JohnHarry')
end
end
context "#fill_in with { clear: Array } fill_options" do
it 'should pass the array through to the element' do
pending "selenium-webdriver/geckodriver doesn't support complex sets of characters" if marionette?(@session)
pending "selenium-webdriver/geckodriver doesn't support complex sets of characters" if marionette?(session)
# this is mainly for use with [[:control, 'a'], :backspace] - however since that is platform dependant I'm testing with something less useful
@session.visit('/form')
@session.fill_in('form_first_name',
with: 'Harry',
fill_options: { clear: [[:shift, 'abc'], :backspace] })
expect(@session.find(:fillable_field, 'form_first_name').value).to eq('JohnABHarry')
session.visit('/form')
session.fill_in('form_first_name',
with: 'Harry',
fill_options: { clear: [[:shift, 'abc'], :backspace] })
expect(session.find(:fillable_field, 'form_first_name').value).to eq('JohnABHarry')
end
end
describe "#path" do
it "returns xpath" do
# this is here because it is testing for an XPath that is specific to the algorithm used in the selenium driver
@session.visit('/path')
element = @session.find(:link, 'Second Link')
session.visit('/path')
element = session.find(:link, 'Second Link')
expect(element.path).to eq('/html/body/div[2]/a[1]')
end
end
describe "all with disappearing elements" do
it "ignores stale elements in results" do
@session.visit('/path')
elements = @session.all(:link) { |_node| raise Selenium::WebDriver::Error::StaleElementReferenceError }
session.visit('/path')
elements = session.all(:link) { |_node| raise Selenium::WebDriver::Error::StaleElementReferenceError }
expect(elements.size).to eq 0
end
end
describe "#evaluate_script" do
it "can return an element" do
@session.visit('/form')
element = @session.evaluate_script("document.getElementById('form_title')")
expect(element).to eq @session.find(:id, 'form_title')
session.visit('/form')
element = session.evaluate_script("document.getElementById('form_title')")
expect(element).to eq session.find(:id, 'form_title')
end
it "can return arrays of nested elements" do
@session.visit('/form')
elements = @session.evaluate_script('document.querySelectorAll("#form_city option")')
elements.each do |el|
expect(el).to be_instance_of Capybara::Node::Element
end
expect(elements).to eq @session.find(:css, '#form_city').all(:css, 'option').to_a
session.visit('/form')
elements = session.evaluate_script('document.querySelectorAll("#form_city option")')
expect(elements).to all(be_instance_of Capybara::Node::Element)
expect(elements).to eq session.find(:css, '#form_city').all(:css, 'option').to_a
end
it "can return hashes with elements" do
@session.visit('/form')
result = @session.evaluate_script("{ a: document.getElementById('form_title'), b: {c: document.querySelectorAll('#form_city option')}}")
session.visit('/form')
result = session.evaluate_script("{ a: document.getElementById('form_title'), b: {c: document.querySelectorAll('#form_city option')}}")
expect(result).to eq(
'a' => @session.find(:id, 'form_title'),
'a' => session.find(:id, 'form_title'),
'b' => {
'c' => @session.find(:css, '#form_city').all(:css, 'option').to_a
'c' => session.find(:css, '#form_city').all(:css, 'option').to_a
}
)
end
describe "#evaluate_async_script" do
it "will timeout if the script takes too long" do
@session.visit('/with_js')
session.visit('/with_js')
expect do
@session.using_wait_time(1) do
@session.evaluate_async_script("var cb = arguments[0]; setTimeout(function(){ cb(null) }, 3000)")
session.using_wait_time(1) do
session.evaluate_async_script("var cb = arguments[0]; setTimeout(function(){ cb(null) }, 3000)")
end
end.to raise_error Selenium::WebDriver::Error::ScriptTimeoutError
end
@ -203,27 +197,28 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
describe "Element#inspect" do
it "outputs obsolete elements" do
@session.visit('/form')
el = @session.find(:button, 'Click me!').click
expect(@session).to have_no_button('Click me!')
expect(el).not_to receive(:synchronize)
session.visit('/form')
el = session.find(:button, 'Click me!').click
expect(session).to have_no_button('Click me!')
allow(el).to receive(:synchronize)
expect(el.inspect).to eq "Obsolete #<Capybara::Node::Element>"
expect(el).not_to have_received(:synchronize)
end
end
describe "Element#click" do
it "should handle fixed headers/footers" do
@session.visit('/with_fixed_header_footer')
# @session.click_link('Go to root')
@session.find(:link, 'Go to root').click
expect(@session).to have_current_path('/')
session.visit('/with_fixed_header_footer')
# session.click_link('Go to root')
session.find(:link, 'Go to root').click
expect(session).to have_current_path('/')
end
end
context "Windows" do
it "can't close the primary window" do
expect do
@session.current_window.close
session.current_window.close
end.to raise_error(ArgumentError, 'Not allowed to close the primary window')
end
end