Skip to content

Commit

Permalink
Implement Capybara::Selenium::Node#path
Browse files Browse the repository at this point in the history
`Capybara::Selenium::Node#path` returns a XPath which points to the node, instead of raising `NotSupportedByDriverError`.
  • Loading branch information
soutaro committed Aug 9, 2015
1 parent b41fcb0 commit 0e26d60
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/capybara/selenium/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ def ==(other)
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
Expand Down
16 changes: 16 additions & 0 deletions spec/selenium_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ module TestSessions
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

Expand Down

0 comments on commit 0e26d60

Please sign in to comment.