From 91caa4032687ca257af1bdf6cf91d5b16a00b2b6 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Wed, 11 Nov 2009 21:55:20 +0100 Subject: [PATCH] Check checkboxes --- lib/webcat/driver/rack_test_driver.rb | 2 ++ lib/webcat/session.rb | 7 ++++++- spec/session_spec.rb | 16 +++++++++++++++- spec/views/form.erb | 12 ++++++------ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/webcat/driver/rack_test_driver.rb b/lib/webcat/driver/rack_test_driver.rb index 23e053dc..75dfe319 100644 --- a/lib/webcat/driver/rack_test_driver.rb +++ b/lib/webcat/driver/rack_test_driver.rb @@ -18,6 +18,8 @@ class Webcat::Driver::RackTest elsif tag_name == 'input' and type == 'radio' session.html.xpath("//input[@name='#{self[:name]}']").each { |node| node.remove_attribute("checked") } node['checked'] = 'checked' + elsif tag_name == 'input' and type == 'checkbox' + node['checked'] = 'checked' elsif tag_name == "textarea" node.content = value.to_s end diff --git a/lib/webcat/session.rb b/lib/webcat/session.rb index 8dfc6537..a239ee80 100644 --- a/lib/webcat/session.rb +++ b/lib/webcat/session.rb @@ -40,6 +40,10 @@ class Webcat::Session def set_hidden_field(locator, options={}) find_field(locator, :hidden_field).set(options[:to]) end + + def check(locator) + find_field(locator, :checkbox).set(true) + end def body driver.body @@ -64,7 +68,8 @@ private :text_area => proc { |id| "//textarea[@id='#{id}']" }, :password_field => proc { |id| "//input[@type='password'][@id='#{id}']" }, :radio => proc { |id| "//input[@type='radio'][@id='#{id}']" }, - :hidden_field => proc { |id| "//input[@type='hidden'][@id='#{id}']" } + :hidden_field => proc { |id| "//input[@type='hidden'][@id='#{id}']" }, + :checkbox => proc { |id| "//input[@type='checkbox'][@id='#{id}']" } } def find_field_by_id(locator, *kinds) diff --git a/spec/session_spec.rb b/spec/session_spec.rb index 5b2bb661..2e0ba8e4 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -206,7 +206,21 @@ shared_examples_for "session" do end describe "#check" do - + before do + @session.visit('/form') + end + + it "should check a checkbox by id" do + @session.check("form_pets_cat") + @session.click_button('awesome') + YAML.load(@session.body)['pets'].should include('dog', 'cat', 'hamster') + end + + it "should check a checkbox by label" do + @session.check("Cat") + @session.click_button('awesome') + YAML.load(@session.body)['pets'].should include('dog', 'cat', 'hamster') + end end describe "#uncheck" do diff --git a/spec/views/form.erb b/spec/views/form.erb index abafd740..780ca24b 100644 --- a/spec/views/form.erb +++ b/spec/views/form.erb @@ -68,12 +68,12 @@

- - - - - - + + + + + +