add selectbox

master
Kanezoh 2021-07-02 09:59:33 +09:00
parent 76447684da
commit 8cfd44473d
4 changed files with 86 additions and 4 deletions

View File

@ -0,0 +1,51 @@
require "../spec_helper"
WebMock.stub(:get, "example.com/form/select_list").to_return(body:
<<-BODY
<html>
<head>
<title>page_title</title>
</head>
<body>
<form action="post_path" method="post" name="sample_form">
<select name="pet" id="pet-select">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="hamster">Hamster</option>
</select>
</form>
</body>
</html>
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

View File

@ -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

View File

@ -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)

View File

@ -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