From 8cfd44473d97e8fa5976eb30bf952d849266eb6c Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Fri, 2 Jul 2021 09:59:33 +0900 Subject: [PATCH] add selectbox --- spec/form/select_list_spec.cr | 51 +++++++++++++++++++++++++ src/mechanize/form.cr | 4 +- src/mechanize/form/multi_select_list.cr | 4 +- src/mechanize/form/select_list.cr | 31 +++++++++++++++ 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 spec/form/select_list_spec.cr create mode 100644 src/mechanize/form/select_list.cr diff --git a/spec/form/select_list_spec.cr b/spec/form/select_list_spec.cr new file mode 100644 index 0000000..0be6e89 --- /dev/null +++ b/spec/form/select_list_spec.cr @@ -0,0 +1,51 @@ +require "../spec_helper" + +WebMock.stub(:get, "example.com/form/select_list").to_return(body: +<<-BODY + + + page_title + + +
+ +
+ + +BODY +) + +describe "Form Fields Select List" do + agent = Mechanize.new + page = agent.get("http://example.com/form/select_list") + form = page.forms[0] + + it "returns selectboxes size" do + form.selectboxes.size.should eq 1 + end + + selectbox = form.selectboxes[0].as(MechanizeCr::FormContent::SelectList) + + it "returns selectbox options size" do + selectbox.options.size.should eq 3 + end + + it "returns selected values" do + selectbox.values.empty?.should eq true + selectbox.options[0].select + selectbox.value.should eq "dog" + end + + it "cannot select multiple values" do + selectbox.options[0].select + selectbox.value.should eq "dog" + form.request_data.should eq "pet=dog" + selectbox.options[1].select + selectbox.value.should eq "cat" + form.request_data.should eq "pet=cat" + end +end diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 2289d20..21a766c 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -5,7 +5,7 @@ require "./form/text" require "./form/textarea" require "./form/hidden" require "./form/button" -require "./form/multi_select_list" +require "./form/select_list" require "./utils/element_matcher" class MechanizeCr::Form @@ -110,7 +110,7 @@ class MechanizeCr::Form if node.has_key?("multiple") @selectboxes << FormContent::MultiSelectList.new(node) else - #@fields << SelectList.new(node) + @selectboxes << FormContent::SelectList.new(node) end end end diff --git a/src/mechanize/form/multi_select_list.cr b/src/mechanize/form/multi_select_list.cr index de2d7fc..528f2d8 100644 --- a/src/mechanize/form/multi_select_list.cr +++ b/src/mechanize/form/multi_select_list.cr @@ -32,9 +32,9 @@ class MechanizeCr::FormContent::MultiSelectList options.select &.selected? end - def values=(raw_values) + def values=(new_values) select_none - raw_values.each do |value| + new_values.each do |value| option = options.find { |o| o.value == value } if option.nil? @value.push(value) diff --git a/src/mechanize/form/select_list.cr b/src/mechanize/form/select_list.cr new file mode 100644 index 0000000..e00e5fb --- /dev/null +++ b/src/mechanize/form/select_list.cr @@ -0,0 +1,31 @@ +require "./multi_select_list" + +class MechanizeCr::FormContent::SelectList < MechanizeCr::FormContent::MultiSelectList + def initialize(node) + super node + # only one selected option is allowed + if selected_options.size > 1 + selected_options.reverse[1..selected_options.size].each do |o| + o.unselect + end + end + end + + def value + if values.size > 0 + values.last + elsif options.size > 0 + options.first.value + else + nil + end + end + + def value=(new_value) + values = new_value + end + + def query_value + value ? [[name, value.not_nil!]] : nil + end +end