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

Allow href: false option to :link selector to ignore href

This commit is contained in:
Thomas Walpole 2019-05-14 16:00:37 -07:00
parent b2d10bfa22
commit f0a699e8d0
5 changed files with 18 additions and 5 deletions

View file

@ -6,8 +6,9 @@ Release date: unreleased
* `Node#obscured?` to check viewport presence and element overlap
* `:obscured` system filter to check whether elements are obscured in finders, assertions, and expectations
* :label selector :for option can be a regexp
* Significanlty smaller `isDisplayed`/`getAttribute` atoms for selenium driver. If these produce issues you can disable their use
* Significantly smaller `isDisplayed`/`getAttribute` atoms for selenium driver. If these produce issues you can disable their use
by setting an environment variable named 'DISABLE_CAPYBARA_SELENIUM_OPTIMIZATIONS' (Please also report any issues).
* `href: false` option with `find_link`/`click_link`/:link selector ignores `href` presence/absence
### Fixed

View file

@ -142,7 +142,7 @@ module Capybara
#
# @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

View file

@ -48,7 +48,7 @@ require 'capybara/selector/definition'
# * :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 )

View file

@ -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
@ -35,7 +36,7 @@ Capybara.add_selector(:link, locator_type: [String, Symbol]) do
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

View file

@ -118,6 +118,17 @@ Capybara::SpecHelper.spec '#click_link' do
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