2012-07-21 16:44:10 -04:00
|
|
|
Capybara::SpecHelper.spec "#check" do
|
|
|
|
before do
|
|
|
|
@session.visit('/form')
|
|
|
|
end
|
2010-08-27 14:40:25 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "'checked' attribute" do
|
|
|
|
it "should be true if checked" do
|
|
|
|
@session.check("Terms of Use")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_terms_of_use']")['checked']).to be_truthy
|
2010-08-27 14:40:25 -04:00
|
|
|
end
|
2012-06-08 09:53:25 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should be false if unchecked" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_terms_of_use']")['checked']).to be_falsey
|
2010-08-27 14:40:25 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should trigger associated events", :requires => [:js] do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.check('checkbox_with_event')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_css('#checkbox_event_triggered');
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-04-22 10:17:31 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "checking" do
|
|
|
|
it "should not change an already checked checkbox" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_pets_dog']")['checked']).to be_truthy
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.check('form_pets_dog')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_pets_dog']")['checked']).to be_truthy
|
2010-08-27 14:40:25 -04:00
|
|
|
end
|
2010-04-22 10:17:31 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should check an unchecked checkbox" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_pets_cat']")['checked']).to be_falsey
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.check('form_pets_cat')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_pets_cat']")['checked']).to be_truthy
|
2010-08-27 14:40:25 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2010-08-27 14:40:25 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "unchecking" do
|
|
|
|
it "should not change an already unchecked checkbox" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_pets_cat']")['checked']).to be_falsey
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.uncheck('form_pets_cat')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_pets_cat']")['checked']).to be_falsey
|
2010-08-27 14:40:25 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should uncheck a checked checkbox" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_pets_dog']")['checked']).to be_truthy
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.uncheck('form_pets_dog')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:xpath, "//input[@id='form_pets_dog']")['checked']).to be_falsey
|
2010-08-27 14:40:25 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should check a checkbox by id" do
|
|
|
|
@session.check("form_pets_cat")
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['pets']).to include('dog', 'cat', 'hamster')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should check a checkbox by label" do
|
|
|
|
@session.check("Cat")
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['pets']).to include('dog', 'cat', 'hamster')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2012-09-06 03:33:43 -04:00
|
|
|
it "casts to string" do
|
|
|
|
@session.check(:"form_pets_cat")
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['pets']).to include('dog', 'cat', 'hamster')
|
2012-09-06 03:33:43 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with a locator that doesn't exist" do
|
|
|
|
it "should raise an error" do
|
|
|
|
msg = "Unable to find checkbox \"does not exist\""
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.check('does not exist')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound, msg)
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
|
|
|
end
|
2012-09-09 11:18:03 -04:00
|
|
|
|
|
|
|
context "with a disabled checkbox" do
|
|
|
|
it "should raise an error" do
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2012-09-09 11:18:03 -04:00
|
|
|
@session.check('Disabled Checkbox')
|
2012-10-30 09:26:19 -04:00
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
2012-09-09 11:18:03 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-24 10:48:25 -05:00
|
|
|
|
|
|
|
context "with :exact option" do
|
|
|
|
it "should accept partial matches when false" do
|
|
|
|
@session.check('Ham', :exact => false)
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['pets']).to include('hamster')
|
2013-02-24 10:48:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not accept partial matches when true" do
|
|
|
|
expect do
|
|
|
|
@session.check('Ham', :exact => true)
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
2013-10-20 13:59:07 -04:00
|
|
|
|
|
|
|
context "with `option` option" do
|
|
|
|
it "can check boxes by their value" do
|
|
|
|
@session.check('form[pets][]', :option => "cat")
|
|
|
|
@session.click_button('awesome')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['pets']).to include('cat')
|
2013-10-20 13:59:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise an error if option not found" do
|
|
|
|
expect do
|
|
|
|
@session.check('form[pets][]', :option => "elephant")
|
|
|
|
end.to raise_error(Capybara::ElementNotFound)
|
|
|
|
end
|
|
|
|
end
|
2010-08-27 14:40:25 -04:00
|
|
|
end
|