Handle namespaces in Selenium::Node#path - Fix Issue #2048

This commit is contained in:
Thomas Walpole 2018-06-12 13:06:25 -07:00
parent 35adf2cb49
commit fdfa22d492
4 changed files with 34 additions and 1 deletions

View File

@ -157,9 +157,15 @@ class Capybara::Selenium::Node < Capybara::Driver::Node
path = find_xpath(XPath.ancestor_or_self).reverse
result = []
default_ns = path.last[:namespaceURI]
while (node = path.shift)
parent = path.first
selector = node.tag_name
if node[:namespaceURI] != default_ns
selector = XPath.child.where((XPath.local_name == selector) & (XPath.namespace_uri == node[:namespaceURI])).to_s
selector
end
if parent
siblings = parent.find_xpath(selector)
selector += "[#{siblings.index(node) + 1}]" unless siblings.size == 1

View File

@ -0,0 +1,20 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Namespace</title>
</head>
<body id="body">
<div>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice"
style="width:100%; height:100%; position:absolute; top:0; left:0; z-index:-1;">
<linearGradient id="gradient">
<stop class="begin" offset="0%"/>
<stop class="end" offset="100%"/>
</linearGradient>
<rect x="0" y="0" width="100" height="100" style="fill:url(#gradient)" />
<rect x="150" y="150" width="25" height="25" style="fill:url(#gradient)" />
<circle cx="50" cy="50" r="30" style="fill:url(#gradient)" />
</svg>
<div>
</body>
</html>

View File

@ -50,7 +50,7 @@ RSpec.describe "Capybara::Session with chrome" do
include_examples "Capybara::Session", TestSessions::Chrome, CHROME_REMOTE_DRIVER
include_examples Capybara::RSpecMatchers, TestSessions::Chrome, CHROME_REMOTE_DRIVER
it 'is considered to be chrome', :focus_ do
it 'is considered to be chrome' do
expect(session.driver.send(:chrome?)).to be_truthy
end
end

View File

@ -182,6 +182,13 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
element = session.find(:link, 'Second Link')
expect(element.path).to eq('/html/body/div[2]/a[1]')
end
it "handles namespaces" do
session.visit "/with_namespace"
rect = session.find(:css, "div svg rect")
expect(rect.path).to eq("/html/body/div/./*[((local-name(.) = 'svg') and (namespace-uri(.) = 'http://www.w3.org/2000/svg'))]/./*[((local-name(.) = 'rect') and (namespace-uri(.) = 'http://www.w3.org/2000/svg'))][1]")
expect(session.find(:xpath, rect.path)).to eq rect
end
end
describe "all with disappearing elements" do