2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2012-07-21 16:44:10 -04:00
|
|
|
Capybara::SpecHelper.spec '#find' do
|
|
|
|
before do
|
|
|
|
@session.visit('/with_html')
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Capybara::Selector.remove(:monkey)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should find the first element using the given locator" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//h1').text).to eq('This is a test')
|
2016-08-04 15:06:31 -04:00
|
|
|
expect(@session.find("//input[@id='test_field']").value).to eq('monkey')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should find the first element using the given locator and options" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//a', :text => 'Redirect')[:id]).to eq('red')
|
|
|
|
expect(@session.find(:css, 'a', :text => 'A link came first')[:title]).to eq('twas a fine link')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error if there are multiple matches" do
|
|
|
|
expect { @session.find('//a') }.to raise_error(Capybara::Ambiguous)
|
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should wait for asynchronous load", :requires => [:js] do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.click_link('Click me')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, "a#has-been-clicked").text).to include('Has been clicked')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
2013-03-26 11:52:26 -04:00
|
|
|
context "with :text option" do
|
|
|
|
it "casts text's argument to string" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, '.number', text: 42)).to have_content("42")
|
2013-03-26 11:52:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-25 14:34:58 -05:00
|
|
|
context "with :wait option", :requires => [:js] do
|
|
|
|
it "should not wait for asynchronous load when `false` given" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.click_link('Click me')
|
|
|
|
expect do
|
|
|
|
@session.find(:css, "a#has-been-clicked", :wait => false)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not find element if it appears after given wait duration" do
|
|
|
|
@session.visit('/with_js')
|
2013-03-08 07:01:03 -05:00
|
|
|
@session.click_link('Slowly')
|
2013-02-25 14:34:58 -05:00
|
|
|
expect do
|
2013-03-08 07:01:03 -05:00
|
|
|
@session.find(:css, "a#slow-clicked", :wait => 0.2)
|
2013-02-25 14:34:58 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should find element if it appears before given wait duration" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.click_link('Click me')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, "a#has-been-clicked", :wait => 0.9).text).to include('Has been clicked')
|
2013-02-25 14:34:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with frozen time", :requires => [:js] do
|
2015-04-14 17:41:01 -04:00
|
|
|
if defined?(Process::CLOCK_MONOTONIC)
|
|
|
|
it "will time out even if time is frozen" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
now = Time.now
|
|
|
|
allow(Time).to receive(:now).and_return(now)
|
|
|
|
expect { @session.find('//isnotthere') }.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
it "raises an error suggesting that Capybara is stuck in time" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
now = Time.now
|
|
|
|
allow(Time).to receive(:now).and_return(now)
|
|
|
|
expect { @session.find('//isnotthere') }.to raise_error(Capybara::FrozenInTime)
|
|
|
|
end
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-08-27 14:37:39 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with css selectors" do
|
2010-01-18 14:33:22 -05:00
|
|
|
it "should find the first element using the given locator" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, 'h1').text).to eq('This is a test')
|
2016-08-04 15:06:31 -04:00
|
|
|
expect(@session.find(:css, "input[id='test_field']").value).to eq('monkey')
|
2010-07-19 14:18:16 -04:00
|
|
|
end
|
2013-02-24 12:04:53 -05:00
|
|
|
|
2013-02-19 12:39:22 -05:00
|
|
|
it "should support pseudo selectors" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, 'input:disabled').value).to eq('This is disabled')
|
2013-02-16 16:26:01 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-07-19 14:18:16 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with xpath selectors" do
|
|
|
|
it "should find the first element using the given locator" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, '//h1').text).to eq('This is a test')
|
2016-08-04 15:06:31 -04:00
|
|
|
expect(@session.find(:xpath, "//input[@id='test_field']").value).to eq('monkey')
|
2010-07-19 14:18:16 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-07-19 14:18:16 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with custom selector" do
|
|
|
|
it "should use the custom selector" do
|
2013-03-08 08:57:01 -05:00
|
|
|
Capybara.add_selector(:beatle) do
|
|
|
|
xpath { |name| ".//*[@id='#{name}']" }
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:beatle, 'john').text).to eq('John')
|
|
|
|
expect(@session.find(:beatle, 'paul').text).to eq('Paul')
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with custom selector with :for option" do
|
|
|
|
it "should use the selector when it matches the :for option" do
|
2013-03-08 08:57:01 -05:00
|
|
|
Capybara.add_selector(:beatle) do
|
|
|
|
xpath { |num| ".//*[contains(@class, 'beatle')][#{num}]" }
|
2012-07-21 16:44:10 -04:00
|
|
|
match { |value| value.is_a?(Fixnum) }
|
2010-08-20 08:46:56 -04:00
|
|
|
end
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:beatle, '2').text).to eq('Paul')
|
|
|
|
expect(@session.find(1).text).to eq('John')
|
|
|
|
expect(@session.find(2).text).to eq('Paul')
|
|
|
|
expect(@session.find('//h1').text).to eq('This is a test')
|
2010-08-20 08:46:56 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-08-20 08:46:56 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with custom selector with custom filter" do
|
|
|
|
before do
|
2013-03-08 08:57:01 -05:00
|
|
|
Capybara.add_selector(:beatle) do
|
|
|
|
xpath { |name| ".//li[contains(@class, 'beatle')][contains(text(), '#{name}')]" }
|
|
|
|
filter(:type) { |node, type| node[:class].split(/\s+/).include?(type) }
|
2010-01-17 12:41:38 -05:00
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2010-01-17 12:41:38 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should find elements that match the filter" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:beatle, 'Paul', :type => 'drummer').text).to eq('Paul')
|
|
|
|
expect(@session.find(:beatle, 'Ringo', :type => 'drummer').text).to eq('Ringo')
|
2013-03-08 08:57:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores filter when it is not given" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:beatle, 'Paul').text).to eq('Paul')
|
|
|
|
expect(@session.find(:beatle, 'John').text).to eq('John')
|
2013-03-08 08:57:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not find elements that don't match the filter" do
|
|
|
|
expect { @session.find(:beatle, 'John', :type => 'drummer') }.to raise_error(Capybara::ElementNotFound)
|
|
|
|
expect { @session.find(:beatle, 'George', :type => 'drummer') }.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with custom selector with custom filter and default" do
|
|
|
|
before do
|
|
|
|
Capybara.add_selector(:beatle) do
|
|
|
|
xpath { |name| ".//li[contains(@class, 'beatle')][contains(text(), '#{name}')]" }
|
|
|
|
filter(:type, :default => "drummer") { |node, type| node[:class].split(/\s+/).include?(type) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should find elements that match the filter" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:beatle, 'Paul', :type => 'drummer').text).to eq('Paul')
|
|
|
|
expect(@session.find(:beatle, 'Ringo', :type => 'drummer').text).to eq('Ringo')
|
2013-03-08 08:57:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should use default value when filter is not given" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:beatle, 'Paul').text).to eq('Paul')
|
2013-03-08 08:57:01 -05:00
|
|
|
expect { @session.find(:beatle, 'John') }.to raise_error(Capybara::ElementNotFound)
|
2010-08-17 21:22:54 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should not find elements that don't match the filter" do
|
2013-03-08 08:57:01 -05:00
|
|
|
expect { @session.find(:beatle, 'John', :type => 'drummer') }.to raise_error(Capybara::ElementNotFound)
|
|
|
|
expect { @session.find(:beatle, 'George', :type => 'drummer') }.to raise_error(Capybara::ElementNotFound)
|
2010-08-27 14:37:39 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-08-27 14:37:39 -04:00
|
|
|
|
2015-01-26 17:50:59 -05:00
|
|
|
context "with alternate filter set" do
|
|
|
|
before do
|
|
|
|
Capybara::Selector::FilterSet.add(:value) do
|
|
|
|
filter(:with) { |node, with| node.value == with.to_s }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:id_with_field_filters) do
|
|
|
|
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
|
|
|
filter_set(:field)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow use of filters from custom filter set" do
|
|
|
|
expect(@session.find(:id, 'test_field', filter_set: :value, with: 'monkey').value).to eq('monkey')
|
|
|
|
expect{ @session.find(:id, 'test_field', filter_set: :value, with: 'not_monkey') }.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow use of filter set from a different selector" do
|
|
|
|
expect(@session.find(:id, 'test_field', filter_set: :field, with: 'monkey').value).to eq('monkey')
|
|
|
|
expect{ @session.find(:id, 'test_field', filter_set: :field, with: 'not_monkey') }.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow importing of filter set into selector" do
|
|
|
|
expect(@session.find(:id_with_field_filters, 'test_field', with: 'monkey').value).to eq('monkey')
|
|
|
|
expect{ @session.find(:id_with_field_filters, 'test_field', with: 'not_monkey') }.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with css as default selector" do
|
|
|
|
before { Capybara.default_selector = :css }
|
|
|
|
it "should find the first element using the given locator" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('h1').text).to eq('This is a test')
|
2016-08-04 15:06:31 -04:00
|
|
|
expect(@session.find("input[id='test_field']").value).to eq('monkey')
|
2012-01-02 06:46:04 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
after { Capybara.default_selector = :xpath }
|
|
|
|
end
|
2012-01-02 06:46:04 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should raise ElementNotFound with a useful default message if nothing was found" do
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
|
|
|
@session.find(:xpath, '//div[@id="nosuchthing"]').to be_nil
|
|
|
|
end.to raise_error(Capybara::ElementNotFound, "Unable to find xpath \"//div[@id=\\\"nosuchthing\\\"]\"")
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-01-17 12:41:38 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should accept an XPath instance" do
|
|
|
|
@session.visit('/form')
|
|
|
|
@xpath = XPath::HTML.fillable_field('First Name')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(@xpath).value).to eq('John')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-01-17 12:41:38 -05:00
|
|
|
|
2013-02-15 13:59:51 -05:00
|
|
|
context "with :exact option" do
|
|
|
|
it "matches exactly when true" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("test_field")], :exact => true).value).to eq("monkey")
|
2013-02-15 13:59:51 -05:00
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("est_fiel")], :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "matches loosely when false" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("test_field")], :exact => false).value).to eq("monkey")
|
|
|
|
expect(@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("est_fiel")], :exact => false).value).to eq("monkey")
|
2013-02-15 13:59:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults to `Capybara.exact`" do
|
2013-02-15 14:02:15 -05:00
|
|
|
Capybara.exact = true
|
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("est_fiel")])
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
Capybara.exact = false
|
|
|
|
@session.find(:xpath, XPath.descendant(:input)[XPath.attr(:id).is("est_fiel")])
|
2013-02-15 13:59:51 -05:00
|
|
|
end
|
2016-08-24 17:07:05 -04:00
|
|
|
|
|
|
|
it "warns when the option has no effect" do
|
|
|
|
expect_any_instance_of(Kernel).to receive(:warn).
|
|
|
|
with('The :exact option only has an effect on queries using the XPath#is method. Using it with the query "#test_field" has no effect.')
|
|
|
|
@session.find(:css, '#test_field', exact: true)
|
|
|
|
end
|
2013-02-15 13:59:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-15 14:32:05 -05:00
|
|
|
context "with :match option" do
|
|
|
|
context "when set to `one`" do
|
|
|
|
it "raises an error when multiple matches exist" do
|
|
|
|
expect do
|
|
|
|
@session.find(:css, ".multiple", :match => :one)
|
|
|
|
end.to raise_error(Capybara::Ambiguous)
|
|
|
|
end
|
|
|
|
it "raises an error even if there the match is exact and the others are inexact" do
|
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :exact => false, :match => :one)
|
|
|
|
end.to raise_error(Capybara::Ambiguous)
|
|
|
|
end
|
|
|
|
it "returns the element if there is only one" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, ".singular", :match => :one).text).to eq("singular")
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
it "raises an error if there is no match" do
|
|
|
|
expect do
|
2013-02-24 12:04:53 -05:00
|
|
|
@session.find(:css, ".does-not-exist", :match => :one)
|
2013-02-15 14:32:05 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-24 12:04:53 -05:00
|
|
|
context "when set to `first`" do
|
2013-02-15 14:32:05 -05:00
|
|
|
it "returns the first matched element" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, ".multiple", :match => :first).text).to eq("multiple one")
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
it "raises an error if there is no match" do
|
|
|
|
expect do
|
2013-02-24 12:04:53 -05:00
|
|
|
@session.find(:css, ".does-not-exist", :match => :first)
|
2013-02-15 14:32:05 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when set to `smart`" do
|
|
|
|
context "and `exact` set to `false`" do
|
|
|
|
it "raises an error when there are multiple exact matches" do
|
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("multiple")], :match => :smart, :exact => false)
|
|
|
|
end.to raise_error(Capybara::Ambiguous)
|
|
|
|
end
|
|
|
|
it "finds a single exact match when there also are inexact matches" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :match => :smart, :exact => false)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("almost singular")
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
it "raises an error when there are multiple inexact matches" do
|
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singul")], :match => :smart, :exact => false)
|
|
|
|
end.to raise_error(Capybara::Ambiguous)
|
|
|
|
end
|
|
|
|
it "finds a single inexact match" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular but")], :match => :smart, :exact => false)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("almost singular but not quite")
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
2013-02-15 15:43:30 -05:00
|
|
|
it "raises an error if there is no match" do
|
|
|
|
expect do
|
2016-08-24 17:07:05 -04:00
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("does-not-exist")], :match => :smart, :exact => false)
|
2013-02-15 15:43:30 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with `exact` set to `true`" do
|
|
|
|
it "raises an error when there are multiple exact matches" do
|
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("multiple")], :match => :smart, :exact => true)
|
|
|
|
end.to raise_error(Capybara::Ambiguous)
|
|
|
|
end
|
|
|
|
it "finds a single exact match when there also are inexact matches" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :match => :smart, :exact => true)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("almost singular")
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
it "raises an error when there are multiple inexact matches" do
|
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singul")], :match => :smart, :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
it "raises an error when there is a single inexact matches" do
|
|
|
|
expect do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular but")], :match => :smart, :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
2013-02-15 15:43:30 -05:00
|
|
|
it "raises an error if there is no match" do
|
|
|
|
expect do
|
2016-08-24 17:07:05 -04:00
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("does-not-exist")], :match => :smart, :exact => true)
|
2013-02-15 15:43:30 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when set to `prefer_exact`" do
|
|
|
|
context "and `exact` set to `false`" do
|
|
|
|
it "picks the first one when there are multiple exact matches" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("multiple")], :match => :prefer_exact, :exact => false)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("multiple one")
|
2013-02-15 15:43:30 -05:00
|
|
|
end
|
|
|
|
it "finds a single exact match when there also are inexact matches" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :match => :prefer_exact, :exact => false)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("almost singular")
|
2013-02-15 15:43:30 -05:00
|
|
|
end
|
|
|
|
it "picks the first one when there are multiple inexact matches" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singul")], :match => :prefer_exact, :exact => false)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("almost singular but not quite")
|
2013-02-15 15:43:30 -05:00
|
|
|
end
|
|
|
|
it "finds a single inexact match" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular but")], :match => :prefer_exact, :exact => false)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("almost singular but not quite")
|
2013-02-15 15:43:30 -05:00
|
|
|
end
|
|
|
|
it "raises an error if there is no match" do
|
|
|
|
expect do
|
2016-08-24 17:07:05 -04:00
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("does-not-exist")], :match => :prefer_exact, :exact => false)
|
2013-02-15 15:43:30 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with `exact` set to `true`" do
|
|
|
|
it "picks the first one when there are multiple exact matches" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("multiple")], :match => :prefer_exact, :exact => true)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("multiple one")
|
2013-02-15 15:43:30 -05:00
|
|
|
end
|
|
|
|
it "finds a single exact match when there also are inexact matches" do
|
|
|
|
result = @session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular")], :match => :prefer_exact, :exact => true)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(result.text).to eq("almost singular")
|
2013-02-15 15:43:30 -05:00
|
|
|
end
|
|
|
|
it "raises an error if there are multiple inexact matches" do
|
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singul")], :match => :prefer_exact, :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
it "raises an error if there is a single inexact match" do
|
|
|
|
expect do
|
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("almost_singular but")], :match => :prefer_exact, :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
it "raises an error if there is no match" do
|
|
|
|
expect do
|
2016-08-24 17:07:05 -04:00
|
|
|
@session.find(:xpath, XPath.descendant[XPath.attr(:class).is("does-not-exist")], :match => :prefer_exact, :exact => true)
|
2013-02-15 15:43:30 -05:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults to `Capybara.match`" do
|
|
|
|
Capybara.match = :one
|
|
|
|
expect do
|
|
|
|
@session.find(:css, ".multiple")
|
|
|
|
end.to raise_error(Capybara::Ambiguous)
|
2013-02-24 12:04:53 -05:00
|
|
|
Capybara.match = :first
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, ".multiple").text).to eq("multiple one")
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
2013-02-24 12:04:53 -05:00
|
|
|
|
|
|
|
it "raises an error when unknown option given" do
|
|
|
|
expect do
|
|
|
|
@session.find(:css, ".singular", :match => :schmoo)
|
|
|
|
end.to raise_error(ArgumentError)
|
|
|
|
end
|
2013-02-15 14:32:05 -05:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "within a scope" do
|
|
|
|
before do
|
|
|
|
@session.visit('/with_scope')
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should find the an element using the given locator" do
|
|
|
|
@session.within(:xpath, "//div[@id='for_bar']") do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('.//li[1]').text).to match(/With Simple HTML/)
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-24 12:04:53 -05:00
|
|
|
|
2013-02-19 12:39:22 -05:00
|
|
|
it "should support pseudo selectors" do
|
|
|
|
@session.within(:xpath, "//div[@id='for_bar']") do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, 'input:disabled').value).to eq('James')
|
2013-02-18 15:35:24 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|