1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Allow link selector to find links with no href attribute when href: nil option specified

This commit is contained in:
Thomas Walpole 2016-12-15 10:59:01 -08:00
parent 9fc3ad9735
commit 2c45b43988
3 changed files with 29 additions and 6 deletions

View file

@ -89,7 +89,7 @@ module Capybara
#
# @macro waiting_behavior
#
# @option options [String,Regexp] href Value to match against the links href
# @option options [String,Regexp,nil] href Value to match against the links href, if nil finds link placeholders (<a> elements with no href attribute)
# @option options [String] id Match links with the id provided
# @option options [String] title Match links with the title provided
# @option options [String] alt Match links with a contained img element whose alt matches

View file

@ -123,11 +123,16 @@ end
# @filter [String] :title Matches the title attribute
# @filter [String] :alt Matches the alt attribute of a contained img element
# @filter [String] :class Matches the class(es) provided
# @filter [String, Regexp] :href Matches the normalized href of the link
# @filter [String, Regexp,nil] :href Matches the normalized href of the link, if nil will find <a> elements with no href attribute
#
Capybara.add_selector(:link) do
xpath(:title, :alt) do |locator, options={}|
xpath = XPath.descendant(:a)[XPath.attr(:href)]
xpath = XPath.descendant(:a)
xpath = if options.fetch(:href, true).nil?
xpath[~XPath.attr(:href)]
else
xpath[XPath.attr(:href)]
end
unless locator.nil?
locator = locator.to_s
matchers = XPath.attr(:id).equals(locator) |
@ -143,14 +148,21 @@ Capybara.add_selector(:link) do
end
filter(:href) do |node, href|
if href.is_a? Regexp
case href
when nil
true
when Regexp
node[:href].match href
else
node.first(:xpath, XPath.axis(:self)[XPath.attr(:href).equals(href.to_s)], minimum: 0)
end
end
describe { |options| " with href #{options[:href].inspect}" if options[:href] }
describe do |options|
desc = String.new()
desc << " with href #{options[:href].inspect}" if options[:href]
desc << " with no href attribute" if options.fetch(:href, true).nil?
end
end
##

View file

@ -105,6 +105,17 @@ Capybara::SpecHelper.spec '#click_link' do
expect { @session.click_link('labore', href: /invalid_pattern/) }.to raise_error(Capybara::ElementNotFound)
expect { @session.click_link('labore', href: /.+d+/) }.to raise_error(Capybara::ElementNotFound)
end
context 'href: nil' do
it "should not raise an error on links with no href attribute" do
expect { @session.click_link('No Href', href: nil) }.not_to raise_error
end
it "should raise an error if href attribute exists" do
expect { @session.click_link('Blank Href', href: nil) }.to raise_error(Capybara::ElementNotFound)
expect { @session.click_link('Normal Anchor', href: nil) }.to raise_error(Capybara::ElementNotFound)
end
end
end
it "should follow relative links" do
@ -161,7 +172,7 @@ Capybara::SpecHelper.spec '#click_link' do
expect(@session).to have_content('Bar')
end
it "raise an error with links with no href" do
it "should raise an error with links with no href" do
expect do
@session.click_link('No Href')
end.to raise_error(Capybara::ElementNotFound)