mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Add assert_all_of_selectors and assert_none_of_selectors
This commit is contained in:
parent
2ed9aad29f
commit
abde354338
3 changed files with 147 additions and 0 deletions
|
@ -97,6 +97,58 @@ module Capybara
|
|||
end
|
||||
end
|
||||
|
||||
# Asserts that all of the provided selectors are present on the given page
|
||||
# or descendants of the current node. If options are provided, the assertion
|
||||
# will check that each locator is present with those options as well (other than :wait).
|
||||
#
|
||||
# page.assert_all_of_selectors(:custom, 'Tom', 'Joe', visible: all)
|
||||
# page.assert_all_of_selectors(:css, '#my_div', 'a.not_clicked')
|
||||
#
|
||||
# It accepts all options that {Capybara::Node::Finders#all} accepts,
|
||||
# such as :text and :visible.
|
||||
#
|
||||
# The :wait option applies to all of the selectors as a group, so all of the locators must be present
|
||||
# within :wait (Defaults to Capybara.default_max_wait_time) seconds.
|
||||
#
|
||||
# @overload assert_all_of_selectors([kind = Capybara.default_selector], *locators, options = {})
|
||||
#
|
||||
def assert_all_of_selectors(*args, &optional_filter_block)
|
||||
options = if args.last.is_a?(Hash) then args.pop.dup else {} end
|
||||
selector = if args.first.is_a?(Symbol) then args.shift else Capybara.default_selector end
|
||||
wait = options.fetch(:wait, Capybara.default_max_wait_time)
|
||||
synchronize(wait) do
|
||||
args.each do |locator|
|
||||
assert_selector(selector, locator, options, &optional_filter_block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Asserts that none of the provided selectors are present on the given page
|
||||
# or descendants of the current node. If options are provided, the assertion
|
||||
# will check that each locator is present with those options as well (other than :wait).
|
||||
#
|
||||
# page.assert_none_of_selectors(:custom, 'Tom', 'Joe', visible: all)
|
||||
# page.assert_none_of_selectors(:css, '#my_div', 'a.not_clicked')
|
||||
#
|
||||
# It accepts all options that {Capybara::Node::Finders#all} accepts,
|
||||
# such as :text and :visible.
|
||||
#
|
||||
# The :wait option applies to all of the selectors as a group, so none of the locators must be present
|
||||
# within :wait (Defaults to Capybara.default_max_wait_time) seconds.
|
||||
#
|
||||
# @overload assert_none_of_selectors([kind = Capybara.default_selector], *locators, options = {})
|
||||
#
|
||||
def assert_none_of_selectors(*args, &optional_filter_block)
|
||||
options = if args.last.is_a?(Hash) then args.pop.dup else {} end
|
||||
selector = if args.first.is_a?(Symbol) then args.shift else Capybara.default_selector end
|
||||
wait = options.fetch(:wait, Capybara.default_max_wait_time)
|
||||
synchronize(wait) do
|
||||
args.each do |locator|
|
||||
assert_no_selector(selector, locator, options, &optional_filter_block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
#
|
||||
# Asserts that a given selector is not on the page or a descendant of the current node.
|
||||
|
|
|
@ -41,6 +41,7 @@ module Capybara
|
|||
:has_no_table?, :has_table?, :unselect, :has_select?, :has_no_select?,
|
||||
:has_selector?, :has_no_selector?, :click_on, :has_no_checked_field?,
|
||||
:has_no_unchecked_field?, :query, :assert_selector, :assert_no_selector,
|
||||
:assert_all_of_selectors, :assert_none_of_selectors,
|
||||
:refute_selector, :assert_text, :assert_no_text
|
||||
]
|
||||
# @api private
|
||||
|
|
94
lib/capybara/spec/session/assert_all_of_selectors_spec.rb
Normal file
94
lib/capybara/spec/session/assert_all_of_selectors_spec.rb
Normal file
|
@ -0,0 +1,94 @@
|
|||
# frozen_string_literal: true
|
||||
Capybara::SpecHelper.spec '#assert_all_of_selectors' do
|
||||
before do
|
||||
@session.visit('/with_html')
|
||||
end
|
||||
|
||||
it "should be true if the given selectors are on the page" do
|
||||
@session.assert_all_of_selectors(:css, "p a#foo", "h2#h2one", "h2#h2two" )
|
||||
end
|
||||
|
||||
it "should be false if any of the given selectors are not on the page" do
|
||||
expect { @session.assert_all_of_selectors(:css, "p a#foo", "h2#h2three", "h2#h2one")}.to raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
|
||||
it "should use default selector" do
|
||||
Capybara.default_selector = :css
|
||||
expect { @session.assert_all_of_selectors("p a#foo", "h2#h2three", "h2#h2one")}.to raise_error(Capybara::ElementNotFound)
|
||||
@session.assert_all_of_selectors("p a#foo", "h2#h2two", "h2#h2one" )
|
||||
end
|
||||
|
||||
it "should respect scopes" do
|
||||
@session.within "//p[@id='first']" do
|
||||
@session.assert_all_of_selectors(".//a[@id='foo']")
|
||||
expect { @session.assert_all_of_selectors(".//a[@id='red']") }.to raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
|
||||
context "with options" do
|
||||
it "should apply options to all locators" do
|
||||
@session.assert_all_of_selectors(:field, 'normal', 'additional_newline', type: :textarea)
|
||||
expect { @session.assert_all_of_selectors(:field, 'normal', 'test_field', 'additional_newline', type: :textarea) }.to raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
end
|
||||
|
||||
context "with wait", requires: [:js] do
|
||||
it "should not raise error if all the elements appear before given wait duration" do
|
||||
Capybara.using_wait_time(0.1) do
|
||||
@session.visit('/with_js')
|
||||
@session.click_link('Click me')
|
||||
@session.assert_all_of_selectors(:css, "a#clickable", "a#has-been-clicked", '#drag', wait: 0.9)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Capybara::SpecHelper.spec '#assert_none_of_selectors' do
|
||||
before do
|
||||
@session.visit('/with_html')
|
||||
end
|
||||
|
||||
it "should be false if any of the given locators are on the page" do
|
||||
expect { @session.assert_none_of_selectors(:xpath, "//p", "//a") }.to raise_error(Capybara::ElementNotFound)
|
||||
expect { @session.assert_none_of_selectors(:xpath, "//abbr", "//a") }.to raise_error(Capybara::ElementNotFound)
|
||||
expect { @session.assert_none_of_selectors(:css, "p a#foo") }.to raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
|
||||
it "should be true if none of the given locators are on the page" do
|
||||
@session.assert_none_of_selectors(:xpath, "//abbr", "//td" )
|
||||
@session.assert_none_of_selectors(:css, "p a#doesnotexist", "abbr")
|
||||
end
|
||||
|
||||
it "should use default selector" do
|
||||
Capybara.default_selector = :css
|
||||
@session.assert_none_of_selectors("p a#doesnotexist", "abbr")
|
||||
expect { @session.assert_none_of_selectors("abbr", "p a#foo") }.to raise_error(Capybara::ElementNotFound)
|
||||
end
|
||||
|
||||
it "should respect scopes" do
|
||||
@session.within "//p[@id='first']" do
|
||||
expect { @session.assert_none_of_selectors(".//a[@id='foo']") }.to raise_error(Capybara::ElementNotFound)
|
||||
@session.assert_none_of_selectors(".//a[@id='red']")
|
||||
end
|
||||
end
|
||||
|
||||
context "with options" do
|
||||
it "should apply the options to all locators" do
|
||||
expect { @session.assert_none_of_selectors("//p//a", text: "Redirect") }.to raise_error(Capybara::ElementNotFound)
|
||||
@session.assert_none_of_selectors("//p", text: "Doesnotexist")
|
||||
end
|
||||
|
||||
it "should discard all matches where the given regexp is matched" do
|
||||
expect { @session.assert_none_of_selectors("//p//a", text: /re[dab]i/i, count: 1) }.to raise_error(Capybara::ElementNotFound)
|
||||
@session.assert_none_of_selectors("//p//a", text: /Red$/)
|
||||
end
|
||||
end
|
||||
|
||||
context "with wait", requires: [:js] do
|
||||
it "should not find elements if they appear after given wait duration" do
|
||||
@session.visit('/with_js')
|
||||
@session.click_link('Click me')
|
||||
@session.assert_none_of_selectors(:css, "#new_field", "a#has-been-clicked", wait: 0.1)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue