Support XPath queries of non-valid XML attribute names

This commit is contained in:
Thomas Walpole 2018-09-20 15:15:32 -07:00
parent 6038c5e84d
commit a42054fafc
4 changed files with 26 additions and 0 deletions

View File

@ -393,6 +393,7 @@ module Capybara
module RackTest; end
module Selenium; end
require 'capybara/xpath_patches'
require 'capybara/helpers'
require 'capybara/session'
require 'capybara/window'

View File

@ -648,3 +648,7 @@ New line after and before textarea tag
<label for="asterisk_input">With Asterisk<abbr title="required">*</abbr></label>
<input id="asterisk_input" type="number"value="2016"/>
</p>
<p>
<input id="special" {custom}="abcdef" value="custom attribute"/>
</p>

View File

@ -0,0 +1,15 @@
# frozen_string_literal: true
module Capybara
module XPathPatches
module Renderer
def attribute(current, name)
return super if name =~ /^[a-zA-Z_:][a-zA-Z0-9_:\.\-]*$/
"#{current}/attribute::*[local-name(.) = #{string_literal(name)}]"
end
end
end
end
XPath::Renderer.prepend(Capybara::XPathPatches::Renderer)

View File

@ -445,6 +445,12 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
expect(session).to have_selector(:element, :circle)
expect(session).to have_selector(:element, :linearGradient, visible: :all)
end
it 'can query attributes with strange characters' do
session.visit('/form')
expect(session).to have_selector(:element, "{custom}": true)
expect(session).to have_selector(:element, "{custom}": 'abcdef')
end
end
end