Sibling and ancestor queries should support Simple::Node

This commit is contained in:
Thomas Walpole 2021-03-14 13:59:19 -07:00
parent 84acc29d5f
commit 3780800f33
4 changed files with 19 additions and 2 deletions

View File

@ -191,6 +191,9 @@ module Capybara
{}
end
def ==(other)
native == other.native
end
private
def option_value(option)

View File

@ -8,7 +8,8 @@ module Capybara
@child_node = node
node.synchronize do
match_results = super(node.session.current_scope, exact)
scope = node.respond_to?(:session) ? node.session.current_scope : node.find(:xpath, '/*')
match_results = super(scope, exact)
ancestors = node.find_xpath(XPath.ancestor.to_s)
.map(&method(:to_element))
.select { |el| match_results.include?(el) }

View File

@ -7,7 +7,8 @@ module Capybara
def resolve_for(node, exact = nil)
@sibling_node = node
node.synchronize do
match_results = super(node.session.current_scope, exact)
scope = node.respond_to?(:session) ? node.session.current_scope : node.find(:xpath, '/*')
match_results = super(scope, exact)
siblings = node.find_xpath((XPath.preceding_sibling + XPath.following_sibling).to_s)
.map(&method(:to_element))
.select { |el| match_results.include?(el) }

View File

@ -111,6 +111,18 @@ RSpec.describe Capybara do
expect(string.find('//form/input[@name="meh"]')).not_to be_disabled
end
it 'allows finding siblings' do
h1 = string.find(:css, 'h1')
expect(h1).to have_sibling(:css, 'p', text: 'Yes it is')
expect(h1).not_to have_sibling(:css, 'p', text: 'Jonas Nicklas')
end
it 'allows finding ancestor' do
h1 = string.find(:css, 'h1')
expect(h1).to have_ancestor(:css, '#content')
expect(h1).not_to have_ancestor(:css, '#footer')
end
it 'drops illegal fragments when using gumbo' do
skip 'libxml is less strict than Gumbo' unless Nokogiri.respond_to?(:HTML5)
expect(described_class.string('<td>1</td>')).not_to have_css('td')