# frozen_string_literal: trueCapybara::SpecHelper.spec"#check"dobeforedo@session.visit('/form')enddescribe"'checked' attribute"doit"should be true if checked"do@session.check("Terms of Use")expect(@session.find(:xpath,"//input[@id='form_terms_of_use']")['checked']).tobe_truthyendit"should be false if unchecked"doexpect(@session.find(:xpath,"//input[@id='form_terms_of_use']")['checked']).tobe_falseyendendit"should trigger associated events",requires: [:js]do@session.visit('/with_js')@session.check('checkbox_with_event')expect(@session).tohave_css('#checkbox_event_triggered')enddescribe"checking"doit"should not change an already checked checkbox"doexpect(@session.find(:xpath,"//input[@id='form_pets_dog']")).tobe_checked@session.check('form_pets_dog')expect(@session.find(:xpath,"//input[@id='form_pets_dog']")).tobe_checkedendit"should check an unchecked checkbox"doexpect(@session.find(:xpath,"//input[@id='form_pets_cat']")).not_tobe_checked@session.check('form_pets_cat')expect(@session.find(:xpath,"//input[@id='form_pets_cat']")).tobe_checkedendenddescribe"unchecking"doit"should not change an already unchecked checkbox"doexpect(@session.find(:xpath,"//input[@id='form_pets_cat']")).not_tobe_checked@session.uncheck('form_pets_cat')expect(@session.find(:xpath,"//input[@id='form_pets_cat']")).not_tobe_checkedendit"should uncheck a checked checkbox"doexpect(@session.find(:xpath,"//input[@id='form_pets_dog']")).tobe_checked@session.uncheck('form_pets_dog')expect(@session.find(:xpath,"//input[@id='form_pets_dog']")).not_tobe_checkedendendit"should check a checkbox by id"do@session.check("form_pets_cat")@session.click_button('awesome')expect(extract_results(@session)['pets']).toinclude('dog','cat','hamster')endit"should check a checkbox by label"do@session.check("Cat")@session.click_button('awesome')expect(extract_results(@session)['pets']).toinclude('dog','cat','hamster')endit"casts to string"do@session.check(:form_pets_cat)@session.click_button('awesome')expect(extract_results(@session)['pets']).toinclude('dog','cat','hamster')endcontext"with a locator that doesn't exist"doit"should raise an error"domsg="Unable to find visible checkbox \"does not exist\" that is not disabled"expectdo@session.check('does not exist')end.toraise_error(Capybara::ElementNotFound,msg)endendcontext"with a disabled checkbox"doit"should raise an error"doexpectdo@session.check('Disabled Checkbox')end.toraise_error(Capybara::ElementNotFound)endendcontext"with :exact option"doit"should accept partial matches when false"do@session.check('Ham',exact: false)@session.click_button('awesome')expect(extract_results(@session)['pets']).toinclude('hamster')endit"should not accept partial matches when true"doexpectdo@session.check('Ham',exact: true)end.toraise_error(Capybara::ElementNotFound)endendcontext"with `option` option"doit"can check boxes by their value"do@session.check('form[pets][]',option: "cat")@session.click_button('awesome')expect(extract_results(@session)['pets']).toinclude('cat')endit"should raise an error if option not found"doexpectdo@session.check('form[pets][]',option: "elephant")end.toraise_error(Capybara::ElementNotFound)endendcontext"when checkbox hidden"docontext"with Capybara.automatic_label_click == true"doarounddo|spec|old_click_label,Capybara.automatic_label_click=Capybara.automatic_label_click,truespec.runCapybara.automatic_label_click=old_click_labelendit"should check via clicking the label with :for attribute if possible"doexpect(@session.find(:checkbox,'form_cars_tesla',unchecked: true,visible: :hidden)).tobe@session.check('form_cars_tesla')@session.click_button('awesome')expect(extract_results(@session)['cars']).toinclude('tesla')endit"should check via clicking the wrapping label if possible"doexpect(@session.find(:checkbox,'form_cars_mclaren',unchecked: true,visible: :hidden)).tobe@session.check('form_cars_mclaren')@session.click_button('awesome')expect(extract_results(@session)['cars']).toinclude('mclaren')endit"should not click the label if unneeded"doexpect(@session.find(:checkbox,'form_cars_jaguar',checked: true,visible: :hidden)).tobe@session.check('form_cars_jaguar')@session.click_button('awesome')expect(extract_results(@session)['cars']).toinclude('jaguar')endit"should raise original error when no label available"doexpect{@session.check('form_cars_ariel')}.toraise_error(Capybara::ElementNotFound,'Unable to find visible checkbox "form_cars_ariel" that is not disabled')endit"should raise error if not allowed to click label"doexpect{@session.check('form_cars_mclaren',allow_label_click: false)}.toraise_error(Capybara::ElementNotFound,'Unable to find visible checkbox "form_cars_mclaren" that is not disabled')endendcontext"with Capybara.automatic_label_click == false"doarounddo|spec|old_label_click,Capybara.automatic_label_click=Capybara.automatic_label_click,falsespec.runCapybara.automatic_label_click=old_label_clickendit"should raise error if checkbox not visible"doexpect{@session.check('form_cars_mclaren')}.toraise_error(Capybara::ElementNotFound,'Unable to find visible checkbox "form_cars_mclaren" that is not disabled')endcontext"with allow_label_click == true"doit"should check via the label if input is hidden"doexpect(@session.find(:checkbox,'form_cars_tesla',unchecked: true,visible: :hidden)).tobe@session.check('form_cars_tesla',allow_label_click: true)@session.click_button('awesome')expect(extract_results(@session)['cars']).toinclude('tesla')endit"should not wait the full time if label can be clicked"doexpect(@session.find(:checkbox,'form_cars_tesla',unchecked: true,visible: :hidden)).tobestart_time=Time.now@session.check('form_cars_tesla',allow_label_click: true,wait: 10)end_time=Time.nowexpect(end_time-start_time).tobe<10endit"should check via the label if input is moved off the left edge of the page"doexpect(@session.find(:checkbox,'form_cars_pagani',unchecked: true,visible: :all)).tobe@session.check('form_cars_pagani',allow_label_click: true)@session.click_button('awesome')expect(extract_results(@session)['cars']).toinclude('pagani')endendendendend