-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While other, more specific, selectors and matches exist, it's very common for test suites to fall back to CSS's support for attribute matching. For example, it's common for suites to find `<img>` elements by their `[src]` value through concatenating a CSS Selector string: ```ruby have_css("img[src=#{image_path}]") ``` Sometimes, including double quotes into the String isn't necessary, but when it is, it leads to escaping, combined `'` and `"` usage, alternative syntaxes like `%{...}`, or calls to `String#inspect`: ```ruby have_css("img[src=\"#{image_path}\"]") have_css('img[src="#{image_path}"]') have_css(%{img[src="#{image_path}"]}) have_css("img[src=#{image_path.inspect}]") ``` The `:element` selector thrives in circumstances like this: ```ruby have_selector :element, "img", src: image_path ``` In my experience, it's uncommon for teams to even _be aware_ that the `:element` selector exists, let alone make longer-form calls like `have_selector` that require both the `:element` and `"img"` argument. This commit proposes the `Node#has_element?` and `Node#has_no_element?` methods to power `have_element`, `assert_element`, etc. The hope is that the generated Ruby documentation will popularize the use of the `:element` selector.
- Loading branch information
1 parent
abf976e
commit cf6f6a3
Showing
8 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
Capybara::SpecHelper.spec '#has_element?' do | ||
before do | ||
@session.visit('/with_html') | ||
end | ||
|
||
it 'should be true if the given element is on the page' do | ||
expect(@session).to have_element('a', id: 'foo') | ||
expect(@session).to have_element('a', text: 'A link', href: '/with_simple_html') | ||
expect(@session).to have_element('a', text: :'A link', href: :'/with_simple_html') | ||
expect(@session).to have_element('a', text: 'A link', href: %r{/with_simple_html}) | ||
expect(@session).to have_element('a', text: 'labore', target: '_self') | ||
end | ||
|
||
it 'should be false if the given element is not on the page' do | ||
expect(@session).not_to have_element('a', text: 'monkey') | ||
expect(@session).not_to have_element('a', text: 'A link', href: '/nonexistent-href') | ||
expect(@session).not_to have_element('a', text: 'A link', href: /nonexistent/) | ||
expect(@session).not_to have_element('a', text: 'labore', target: '_blank') | ||
end | ||
|
||
it 'should notify if an invalid locator is specified' do | ||
allow(Capybara::Helpers).to receive(:warn).and_return(nil) | ||
@session.has_element?(@session) | ||
expect(Capybara::Helpers).to have_received(:warn).with(/Called from: .+/) | ||
end | ||
end | ||
|
||
Capybara::SpecHelper.spec '#has_no_element?' do | ||
before do | ||
@session.visit('/with_html') | ||
end | ||
|
||
it 'should be false if the given element is on the page' do | ||
expect(@session).not_to have_no_element('a', id: 'foo') | ||
expect(@session).not_to have_no_element('a', text: 'A link', href: '/with_simple_html') | ||
expect(@session).not_to have_no_element('a', text: 'labore', target: '_self') | ||
end | ||
|
||
it 'should be true if the given element is not on the page' do | ||
expect(@session).to have_no_element('a', text: 'monkey') | ||
expect(@session).to have_no_element('a', text: 'A link', href: '/nonexistent-href') | ||
expect(@session).to have_no_element('a', text: 'A link', href: %r{/nonexistent-href}) | ||
expect(@session).to have_no_element('a', text: 'labore', target: '_blank') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters