can use NamedTuple as *_with method's argument

master
Kanezoh 2021-08-11 07:17:38 +09:00
parent 979c5b4c3a
commit 9dd4273f2c
6 changed files with 44 additions and 4 deletions

View File

@ -26,8 +26,13 @@ describe "Form Fields CheckBox" do
form.buttons.size.should eq 2 form.buttons.size.should eq 2
end end
it "can be found by button_with method" do it "can be found by button_with method, argument type: Hash" do
button2 = form.button_with({"class" => "sndButton"}) button2 = form.button_with({"class" => "sndButton"})
button2.value.should eq "sndButtonValue" button2.value.should eq "sndButtonValue"
end end
it "can be found by button_with method, argument type: NamedTuple" do
button2 = form.button_with({class: "sndButton"})
button2.value.should eq "sndButtonValue"
end
end end

View File

@ -52,10 +52,17 @@ describe "Form Fields CheckBox" do
checkbox.name.should eq "remember_me" checkbox.name.should eq "remember_me"
end end
it "can be found by checkboxes_with method" do it "can be found by checkboxes_with method, argument type: Hash" do
checkboxes = form.checkboxes_with({"class" => "some_checkbox"}) checkboxes = form.checkboxes_with({"class" => "some_checkbox"})
checkboxes.size.should eq 2 checkboxes.size.should eq 2
checkboxes[0].name.should eq "remember_me" checkboxes[0].name.should eq "remember_me"
checkboxes[1].name.should eq "forget_me" checkboxes[1].name.should eq "forget_me"
end end
it "can be found by checkboxes_with method, argument type: NamedTuple" do
checkboxes = form.checkboxes_with({class: "some_checkbox"})
checkboxes.size.should eq 2
checkboxes[0].name.should eq "remember_me"
checkboxes[1].name.should eq "forget_me"
end
end end

View File

@ -47,9 +47,17 @@ describe "Form Fields" do
email_field.name.should eq "email" email_field.name.should eq "email"
end end
it "can be found by fields_with method" do it "can be found by fields_with method, argument type: Hash" do
name_fields = form.fields_with("name") name_fields = form.fields_with("name")
name_fields.size.should eq 2 name_fields.size.should eq 2
name_fields[0].name.should eq "name" name_fields[0].name.should eq "name"
end end
it "can be found by fields_with method, argument type: NamedTuple" do
name_field = form.field_with("name")
name_field.name.should eq "name"
email_field = form.field_with({id: "emailID"})
email_field.name.should eq "email"
end
end end

View File

@ -71,4 +71,14 @@ describe "Form Fields RadioButton" do
radiobuttons[1].value.should eq "phone" radiobuttons[1].value.should eq "phone"
radiobuttons[2].value.should eq "mail" radiobuttons[2].value.should eq "mail"
end end
it "can be found by radiobutton_with method, argument type: Hash" do
radiobutton = form.radiobutton_with({"id": "contactChoice1"})
radiobutton.value.should eq "email"
end
it "can be found by radiobutton_with method, argument type: NamedTuple" do
radiobutton = form.radiobutton_with({id: "contactChoice1"})
radiobutton.value.should eq "email"
end
end end

View File

@ -32,10 +32,17 @@ describe "Mechanize Page test" do
page.forms.first.name.should eq "sample_form" page.forms.first.name.should eq "sample_form"
end end
it "can detect form by using form_with method" do it "can detect form by using form_with method, argument type: Hash" do
agent = Mechanize.new agent = Mechanize.new
page = agent.get("http://example.com/form") page = agent.get("http://example.com/form")
form = page.form_with({"name" => "sample_form" }) form = page.form_with({"name" => "sample_form" })
form.name.should eq "sample_form" form.name.should eq "sample_form"
end end
it "can detect form by using form_with method, argument type: NamedTuple" do
agent = Mechanize.new
page = agent.get("http://example.com/form")
form = page.form_with({name: "sample_form" })
form.name.should eq "sample_form"
end
end end

View File

@ -6,6 +6,9 @@ module MechanzeCr::ElementMatcher
end end
def {{plural.id}}_with(criteria, &block) def {{plural.id}}_with(criteria, &block)
if criteria.is_a?(NamedTuple)
criteria = criteria.to_h
end
if criteria.is_a?(String) if criteria.is_a?(String)
criteria = {"name" => criteria} criteria = {"name" => criteria}
else else