2016-03-08 00:52:19 +00:00
|
|
|
# frozen_string_literal: true
|
2012-07-21 20:44:10 +00:00
|
|
|
Capybara::SpecHelper.spec '#has_selector?' do
|
|
|
|
before do
|
|
|
|
@session.visit('/with_html')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should be true if the given selector is on the page" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_selector(:xpath, "//p")
|
|
|
|
expect(@session).to have_selector(:css, "p a#foo")
|
|
|
|
expect(@session).to have_selector("//p[contains(.,'est')]")
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be false if the given selector is not on the page" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_selector(:xpath, "//abbr")
|
|
|
|
expect(@session).not_to have_selector(:css, "p a#doesnotexist")
|
|
|
|
expect(@session).not_to have_selector("//p[contains(.,'thisstringisnotonpage')]")
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should use default selector" do
|
|
|
|
Capybara.default_selector = :css
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_selector("p a#doesnotexist")
|
|
|
|
expect(@session).to have_selector("p a#foo")
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should respect scopes" do
|
|
|
|
@session.within "//p[@id='first']" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_selector(".//a[@id='foo']")
|
|
|
|
expect(@session).not_to have_selector(".//a[@id='red']")
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
context "with count" do
|
|
|
|
it "should be true if the content is on the page the given number of times" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_selector("//p", :count => 3)
|
|
|
|
expect(@session).to have_selector("//p//a[@id='foo']", :count => 1)
|
|
|
|
expect(@session).to have_selector("//p[contains(.,'est')]", :count => 1)
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should be false if the content is on the page the given number of times" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_selector("//p", :count => 6)
|
|
|
|
expect(@session).not_to have_selector("//p//a[@id='foo']", :count => 2)
|
|
|
|
expect(@session).not_to have_selector("//p[contains(.,'est')]", :count => 5)
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should be false if the content isn't on the page at all" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_selector("//abbr", :count => 2)
|
|
|
|
expect(@session).not_to have_selector("//p//a[@id='doesnotexist']", :count => 1)
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
context "with text" do
|
|
|
|
it "should discard all matches where the given string is not contained" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_selector("//p//a", :text => "Redirect", :count => 1)
|
|
|
|
expect(@session).not_to have_selector("//p", :text => "Doesnotexist")
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
|
|
|
|
2013-02-17 15:09:14 +00:00
|
|
|
it "should respect visibility setting" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_selector(:id, "hidden-text", :text => "Some of this text is hidden!", :visible => false)
|
|
|
|
expect(@session).not_to have_selector(:id, "hidden-text", :text => "Some of this text is hidden!", :visible => true)
|
2013-02-17 15:09:14 +00:00
|
|
|
Capybara.ignore_hidden_elements = false
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_selector(:id, "hidden-text", :text => "Some of this text is hidden!", :visible => false)
|
2013-02-17 15:09:14 +00:00
|
|
|
Capybara.visible_text_only = true
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_selector(:id, "hidden-text", :text => "Some of this text is hidden!", :visible => true)
|
2013-02-17 15:09:14 +00:00
|
|
|
end
|
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should discard all matches where the given regexp is not matched" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_selector("//p//a", :text => /re[dab]i/i, :count => 1)
|
|
|
|
expect(@session).not_to have_selector("//p//a", :text => /Red$/)
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
2016-04-04 22:36:43 +00:00
|
|
|
|
|
|
|
it "should warn when extra parameters passed" do
|
|
|
|
expect_any_instance_of(Kernel).to receive(:warn).with(/extra/)
|
|
|
|
expect(@session).to have_selector(:css, "p a#foo", 'extra')
|
|
|
|
end
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
Capybara::SpecHelper.spec '#has_no_selector?' do
|
|
|
|
before do
|
|
|
|
@session.visit('/with_html')
|
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should be false if the given selector is on the page" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_no_selector(:xpath, "//p")
|
|
|
|
expect(@session).not_to have_no_selector(:css, "p a#foo")
|
|
|
|
expect(@session).not_to have_no_selector("//p[contains(.,'est')]")
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should be true if the given selector is not on the page" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_no_selector(:xpath, "//abbr")
|
|
|
|
expect(@session).to have_no_selector(:css, "p a#doesnotexist")
|
|
|
|
expect(@session).to have_no_selector("//p[contains(.,'thisstringisnotonpage')]")
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should use default selector" do
|
|
|
|
Capybara.default_selector = :css
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_no_selector("p a#doesnotexist")
|
|
|
|
expect(@session).not_to have_no_selector("p a#foo")
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should respect scopes" do
|
|
|
|
@session.within "//p[@id='first']" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_no_selector(".//a[@id='foo']")
|
|
|
|
expect(@session).to have_no_selector(".//a[@id='red']")
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
context "with count" do
|
|
|
|
it "should be false if the content is on the page the given number of times" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_no_selector("//p", :count => 3)
|
|
|
|
expect(@session).not_to have_no_selector("//p//a[@id='foo']", :count => 1)
|
|
|
|
expect(@session).not_to have_no_selector("//p[contains(.,'est')]", :count => 1)
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should be true if the content is on the page the wrong number of times" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_no_selector("//p", :count => 6)
|
|
|
|
expect(@session).to have_no_selector("//p//a[@id='foo']", :count => 2)
|
|
|
|
expect(@session).to have_no_selector("//p[contains(.,'est')]", :count => 5)
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should be true if the content isn't on the page at all" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).to have_no_selector("//abbr", :count => 2)
|
|
|
|
expect(@session).to have_no_selector("//p//a[@id='doesnotexist']", :count => 1)
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
context "with text" do
|
|
|
|
it "should discard all matches where the given string is contained" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_no_selector("//p//a", :text => "Redirect", :count => 1)
|
|
|
|
expect(@session).to have_no_selector("//p", :text => "Doesnotexist")
|
2012-07-21 20:44:10 +00:00
|
|
|
end
|
2010-10-12 11:26:24 +00:00
|
|
|
|
2012-07-21 20:44:10 +00:00
|
|
|
it "should discard all matches where the given regexp is matched" do
|
2013-11-14 17:43:36 +00:00
|
|
|
expect(@session).not_to have_no_selector("//p//a", :text => /re[dab]i/i, :count => 1)
|
|
|
|
expect(@session).to have_no_selector("//p//a", :text => /Red$/)
|
2010-10-12 11:26:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|