Implement Capybara::Selenium::Node#path

`Capybara::Selenium::Node#path` returns a XPath which points to the node, instead of raising `NotSupportedByDriverError`.
This commit is contained in:
Soutaro Matsumoto 2015-08-10 01:55:56 +09:00
parent b41fcb0f35
commit 0e26d602b4
2 changed files with 44 additions and 0 deletions

View File

@ -133,6 +133,34 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
native == other.native
end
def path
path = parents
path.unshift self
result = []
while node = path.shift
parent = path.first
if parent
siblings = parent.find_xpath(node.tag_name)
if siblings.size == 1
result.unshift node.tag_name
else
index = siblings.index(node)
result.unshift "#{node.tag_name}[#{index+1}]"
end
else
result.unshift node.tag_name
end
end
'/' + result.join('/')
end
def parents
find_xpath('ancestor::*').reverse
end
private
# a reference to the select node if this is an option node

View File

@ -100,6 +100,22 @@ RSpec.describe Capybara::Session do
expect(@session).to have_selector(:css, '.change_event_triggered', :match => :one)
end
end
describe "#path" do
before :each do
@session.visit('/with_js')
end
it "returns xpath" do
element = @session.find(:css, '#with_focus_event')
expect(element.path).to eq('/html/body/p[5]/input')
end
it "returns xpath which points to itself" do
element = @session.find(:css, '#with_focus_event')
expect(@session.find(:xpath, element.path)).to eq(element)
end
end
end
end