From 6971ac6967e4ac280583b5ec9d8ad8d9fe414cbd Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 10 Sep 2018 12:47:14 -0700 Subject: [PATCH] Default to document when element has no parent in match selector query --- History.md | 7 +++++++ lib/capybara/node/matchers.rb | 2 +- lib/capybara/session.rb | 2 +- lib/capybara/spec/session/element/match_css_spec.rb | 6 ++++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 0a56f710..1ba56285 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,10 @@ +# Version 3.7.2 +Release date: unreleased + +### Fixed + +* Fix MatchQuery based matchers when used on a root element found using any type of parent/ancestor query - Issue #2097 + # Version 3.7.1 Release date: 2018-09-05 diff --git a/lib/capybara/node/matchers.rb b/lib/capybara/node/matchers.rb index b7db196a..59f57ac5 100644 --- a/lib/capybara/node/matchers.rb +++ b/lib/capybara/node/matchers.rb @@ -713,7 +713,7 @@ module Capybara query_args = _set_query_session_options(*query_args) query = Capybara::Queries::MatchQuery.new(*query_args, &optional_filter_block) synchronize(query.wait) do - yield query.resolve_for(first(:xpath, './parent::*', minimum: 0) || query_scope) + yield query.resolve_for(first(:xpath, './parent::*', minimum: 0) || session&.document || query_scope) end true end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 5c3105be..9beac763 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -364,7 +364,7 @@ module Capybara # @overload switch_to_frame(element) # @param [Capybara::Node::Element] iframe/frame element to switch to # @overload switch_to_frame(:parent) - # Switch to the parent element + # Switch to the parent frame # @overload switch_to_frame(:top) # Switch to the top level document # diff --git a/lib/capybara/spec/session/element/match_css_spec.rb b/lib/capybara/spec/session/element/match_css_spec.rb index 195971a9..d538c036 100644 --- a/lib/capybara/spec/session/element/match_css_spec.rb +++ b/lib/capybara/spec/session/element/match_css_spec.rb @@ -22,4 +22,10 @@ Capybara::SpecHelper.spec '#match_css?' do expect(@element).to match_css('span') { |el| el[:class] == 'number' } expect(@element).not_to match_css('span') { |el| el[:class] == 'not_number' } end + + it 'should work with root element found via ancestor' do + el = @session.find(:css, 'body').find(:xpath, '..') + expect(el).to match_css('html') + expect { expect(el).to not_match_css('html') }.to raise_exception(RSpec::Expectations::ExpectationNotMetError) + end end