Skip to content

Commit

Permalink
Allow href: false option to :link selector to ignore href
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed May 14, 2019
1 parent b2d10bf commit 329d2cf
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/capybara/node/finders.rb
Expand Up @@ -142,7 +142,7 @@ def find_field(locator = nil, **options, &optional_filter_block)
#
# @macro waiting_behavior
#
# @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,Regexp,nil] href Value to match against the links href, if nil finds link placeholders (<a> elements with no href attribute), if false ignores the href
# @option options [String, Regexp] 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
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/selector.rb
Expand Up @@ -48,7 +48,7 @@
# * :title (String) — Matches the title attribute
# * :alt (String) — Matches the alt attribute of a contained img element
# * :class (String, Array<String>, Regexp, XPath::Expression) — Matches the class(es) provided
# * :href (String, Regexp, nil) — Matches the normalized href of the link, if nil will find <a> elements with no href attribute
# * :href (String, Regexp, nil, false) — Matches the normalized href of the link, if nil will find <a> elements with no href attribute, if false ignores href
# * :style (String, Regexp, Hash)
#
# * **:button** - Find buttons ( input [of type submit, reset, image, button] or button elements )
Expand Down
5 changes: 3 additions & 2 deletions lib/capybara/selector/definition/link.rb
Expand Up @@ -2,7 +2,8 @@

Capybara.add_selector(:link, locator_type: [String, Symbol]) do
xpath do |locator, href: true, alt: nil, title: nil, **|
xpath = builder(XPath.descendant(:a)).add_attribute_conditions(href: href)
xpath = XPath.descendant(:a)
xpath = builder(xpath).add_attribute_conditions(href: href) unless href == false

unless locator.nil?
locator = locator.to_s
Expand Down Expand Up @@ -35,7 +36,7 @@
desc = +''
if (href = options[:href])
desc << " with href #{'matching ' if href.is_a? Regexp}#{href.inspect}"
elsif options.key?(:href) # is nil/false specified?
elsif options.key?(:href) && href != false # is nil specified?
desc << ' with no href attribute'
end
desc << " with download attribute#{" #{download}" if download.is_a? String}" if download
Expand Down
11 changes: 11 additions & 0 deletions lib/capybara/spec/session/click_link_spec.rb
Expand Up @@ -118,6 +118,17 @@
expect { @session.click_link('Normal Anchor', href: nil) }.to raise_error(Capybara::ElementNotFound, /with no href attribute/)
end
end

context 'href: false' do
it 'should not raise an error on links with no href attribute' do
expect { @session.click_link('No Href', href: false) }.not_to raise_error
end

it 'should not raise an error if href attribute exists' do
expect { @session.click_link('Blank Href', href: false) }.not_to raise_error
expect { @session.click_link('Normal Anchor', href: false) }.not_to raise_error
end
end
end

it 'should follow relative links' do
Expand Down

0 comments on commit 329d2cf

Please sign in to comment.