require 'mechanize/test_case' class TestMechanizeForm < Mechanize::TestCase def setup super @uri = URI 'http://example' @page = page @uri @form = Mechanize::Form.new node('form', 'name' => @NAME), @mech, @page end def test_action form = Mechanize::Form.new node('form', 'action' => '?a=b&b=c') assert_equal '?a=b&b=c', form.action end def test_add_button_to_query button = Mechanize::Form::Button.new node('input', 'type' => 'submit') e = assert_raises ArgumentError do @form.add_button_to_query button end assert_equal "#{button.inspect} does not belong to the same page " \ "as the form \"#{@NAME}\" in #{@uri}", e.message end def test_aset assert_empty @form.keys @form['intarweb'] = 'Aaron' assert_equal 'Aaron', @form['intarweb'] end def test_aset_exists page = html_page <<-BODY Page Title
BODY form = page.form_with(:name => 'post_form') assert_equal %w[first first], form.keys form['first'] = 'Aaron' assert_equal 'Aaron', form['first'] assert_equal ['Aaron', ''], form.values end def test_build_query_blank_form page = @mech.get('http://localhost/tc_blank_form.html') form = page.forms.first query = form.build_query assert(query.length > 0) assert query.all? { |x| x[1] == '' } end def test_build_query_blank_input_name html = Nokogiri::HTML <<-HTML
HTML form = Mechanize::Form.new html.at('form'), @mech, @page assert_equal [], form.build_query end def test_build_query_radio_button_duplicate html = Nokogiri::HTML <<-HTML
HTML form = Mechanize::Form.new html.at('form'), @mech, @page query = form.build_query assert_equal [%w[name a]], query end def test_build_query_radio_button_multiple_checked html = Nokogiri::HTML <<-HTML
HTML form = Mechanize::Form.new html.at('form'), @mech, @page e = assert_raises Mechanize::Error do form.build_query end assert_equal 'radiobuttons "a, b" are checked in the "name" group, ' \ 'only one is allowed', e.message end def test_method_missing_get page = html_page <<-BODY
BODY form = page.forms.first assert_equal 'some value', form.not_a_method end def test_method_missing_set page = html_page <<-BODY
BODY form = page.forms.first form.not_a_method = 'some value' assert_equal [%w[not_a_method some\ value]], form.build_query end def test_parse_buttons page = html_page <<-BODY