From 5bec2626a823a2d8fc002d69838dcd740fdb39ec Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 1 Jul 2021 20:31:22 +0900 Subject: [PATCH] add multi select box test --- spec/form/multi_select_list_spec.cr | 45 +++++++++++++++++++++++++ src/mechanize/form.cr | 10 ++++++ src/mechanize/form/multi_select_list.cr | 4 +++ 3 files changed, 59 insertions(+) create mode 100644 spec/form/multi_select_list_spec.cr diff --git a/spec/form/multi_select_list_spec.cr b/spec/form/multi_select_list_spec.cr new file mode 100644 index 0000000..d672cd0 --- /dev/null +++ b/spec/form/multi_select_list_spec.cr @@ -0,0 +1,45 @@ +require "../spec_helper" + +WebMock.stub(:get, "example.com/form/multi_select_list").to_return(body: +<<-BODY + + + page_title + + +
+ +
+ + +BODY +) + +describe "Form Fields Multiple Select List" do + agent = Mechanize.new + page = agent.get("http://example.com/form/multi_select_list") + form = page.forms[0] + + it "returns selectboxes size" do + form.selectboxes.size.should eq 1 + end + + selectbox = form.selectboxes[0] + + 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.select_all + selectbox.values.size.should eq 3 + selectbox.values.should eq ["dog", "cat", "hamster"] + selectbox.select_none + selectbox.values.empty?.should eq true + end +end diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 3227625..2289d20 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -163,6 +163,16 @@ class MechanizeCr::Form next if value[0] == "" query.push(value) end + + @selectboxes.each do |s| + value = s.query_value + if value + value.each do |v| + query.push(v) + end + end + end + query end diff --git a/src/mechanize/form/multi_select_list.cr b/src/mechanize/form/multi_select_list.cr index 4f4cf33..de2d7fc 100644 --- a/src/mechanize/form/multi_select_list.cr +++ b/src/mechanize/form/multi_select_list.cr @@ -48,6 +48,10 @@ class MechanizeCr::FormContent::MultiSelectList @values + selected_options.map &.value end + def query_value + values ? values.map { |v| [name, v] } : nil + end + #def inspect # :nodoc: # "[%s:0x%x type: %s name: %s value: %s]" % [ # self.class.name.sub(/MechanizeCr::FormContent::/, "").downcase,