# frozen_string_literal: true Capybara::SpecHelper.spec '#click_button' do before do @session.visit('/form') end it "should wait for asynchronous load", requires: [:js] do @session.visit('/with_js') @session.click_link('Click me') @session.click_button('New Here') end it "casts to string" do @session.click_button(:'Relative Action') expect(extract_results(@session)['relative']).to eq('Relative Action') expect(@session.current_path).to eq('/relative') end context "with multiple values with the same name" do it "should use the latest given value" do @session.check('Terms of Use') @session.click_button('awesome') expect(extract_results(@session)['terms_of_use']).to eq('1') end end context "with a form that has a relative url as an action" do it "should post to the correct url" do @session.click_button('Relative Action') expect(extract_results(@session)['relative']).to eq('Relative Action') expect(@session.current_path).to eq('/relative') end end context "with a form that has no action specified" do it "should post to the correct url" do @session.click_button('No Action') expect(extract_results(@session)['no_action']).to eq('No Action') expect(@session.current_path).to eq('/form') end end context "with value given on a submit button" do context "on a form with HTML5 fields" do before do @session.click_button('html5_submit') @results = extract_results(@session) end it "should serialise and submit search fields" do expect(@results['html5_search']).to eq('what are you looking for') end it "should serialise and submit email fields" do expect(@results['html5_email']).to eq('person@email.com') end it "should serialise and submit url fields" do expect(@results['html5_url']).to eq('http://www.example.com') end it "should serialise and submit tel fields" do expect(@results['html5_tel']).to eq('911') end it "should serialise and submit color fields" do expect(@results['html5_color'].upcase).to eq('#FFFFFF') end end context "on an HTML4 form" do before do @session.click_button('awesome') @results = extract_results(@session) end it "should serialize and submit text fields" do expect(@results['first_name']).to eq('John') end it "should escape fields when submitting" do expect(@results['phone']).to eq('+1 555 7021') end it "should serialize and submit password fields" do expect(@results['password']).to eq('seeekrit') end it "should serialize and submit hidden fields" do expect(@results['token']).to eq('12345') end it "should not serialize fields from other forms" do expect(@results['middle_name']).to be_nil end it "should submit the button that was clicked, but not other buttons" do expect(@results['awesome']).to eq('awesome') expect(@results['crappy']).to be_nil end it "should serialize radio buttons" do expect(@results['gender']).to eq('female') end it "should default radio value to 'on' if none specified" do expect(@results['valueless_radio']).to eq('on') end it "should serialize check boxes" do expect(@results['pets']).to include('dog', 'hamster') expect(@results['pets']).not_to include('cat') end it "should default checkbox value to 'on' if none specififed" do expect(@results['valueless_checkbox']).to eq('on') end it "should serialize text areas" do expect(@results['description']).to eq('Descriptive text goes here') end it "should serialize select tag with values" do expect(@results['locale']).to eq('en') end it "should serialize select tag without values" do expect(@results['region']).to eq('Norway') end it "should serialize first option for select tag with no selection" do expect(@results['city']).to eq('London') end it "should not serialize a select tag without options" do expect(@results['tendency']).to be_nil end it "should convert lf to cr/lf in submitted textareas" do expect(@results['newline']).to eq("\r\nNew line after and before textarea tag\r\n") end it "should not submit disabled fields" do expect(@results['disabled_text_field']).to be_nil expect(@results['disabled_textarea']).to be_nil expect(@results['disabled_checkbox']).to be_nil expect(@results['disabled_radio']).to be_nil expect(@results['disabled_select']).to be_nil expect(@results['disabled_file']).to be_nil end end end context "input type=submit button" do it "should submit by button id" do @session.click_button('awe123') expect(extract_results(@session)['first_name']).to eq('John') end it "should submit by button title" do @session.click_button('What an Awesome Button') expect(extract_results(@session)['first_name']).to eq('John') end it "should submit by partial title", :exact_false do @session.click_button('What an Awesome') expect(extract_results(@session)['first_name']).to eq('John') end end context "with fields associated with the form using the form attribute", requires: [:form_attribute] do before do @session.click_button('submit_form1') @results = extract_results(@session) end it "should serialize and submit text fields" do expect(@results['outside_input']).to eq('outside_input') end it "should serialize text areas" do expect(@results['outside_textarea']).to eq('Some text here') end it "should serialize select tags" do expect(@results['outside_select']).to eq('Ruby') end it "should not serliaze fields associated with a different form" do expect(@results['for_form2']).to be_nil end end context "with submit button outside the form defined by