Added 'choose' for radio buttons

This commit is contained in:
Jonas Nicklas 2009-11-11 21:37:48 +01:00
parent c6a689bf6d
commit 147b81d6b3
4 changed files with 33 additions and 7 deletions

View File

@ -13,8 +13,11 @@ class Webcat::Driver::RackTest
end
def set(value)
if tag_name == 'input'
if tag_name == 'input' and %w(text password).include?(type)
node['value'] = value.to_s
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 == "textarea"
node.content = value.to_s
end
@ -23,7 +26,7 @@ class Webcat::Driver::RackTest
def click
if tag_name == 'a'
session.visit(self[:href])
elsif tag_name == 'input' and self[:type] == 'submit'
elsif tag_name == 'input' and type == 'submit'
Form.new(session, form).submit(self)
end
end
@ -33,6 +36,10 @@ class Webcat::Driver::RackTest
end
private
def type
self[:type]
end
def form
node.ancestors('form').first

View File

@ -32,6 +32,10 @@ class Webcat::Session
def fill_in(locator, options={})
find_field(locator, :text_field, :text_area, :password_field).set(options[:with])
end
def choose(locator)
find_field(locator, :radio).set(true)
end
def body
driver.body
@ -54,7 +58,8 @@ private
FIELDS_PATHS = {
:text_field => proc { |id| "//input[@type='text'][@id='#{id}']" },
:text_area => proc { |id| "//textarea[@id='#{id}']" },
:password_field => proc { |id| "//input[@type='password'][@id='#{id}']" }
:password_field => proc { |id| "//input[@type='password'][@id='#{id}']" },
:radio => proc { |id| "//input[@type='radio'][@id='#{id}']" }
}
def find_field_by_id(locator, *kinds)

View File

@ -171,7 +171,21 @@ shared_examples_for "session" do
end
describe "#choose" do
before do
@session.visit('/form')
end
it "should choose a radio button by id" do
@session.choose("gender_male")
@session.click_button('awesome')
YAML.load(@session.body)['gender'].should == 'male'
end
it "should choose a radio button by label" do
@session.choose("Both")
@session.click_button('awesome')
YAML.load(@session.body)['gender'].should == 'both'
end
end
describe "#set_hidden_field" do

View File

@ -55,11 +55,11 @@
<p>
<p>
<input type="radio" name=form[gender] value="male" id="gender_male"/>
<input type="radio" name="form[gender]" value="male" id="gender_male"/>
<label for="gender_male">Male</label>
<input type="radio" name=form[gender] value="female" id="gender_female" checked="checked"/>
<input type="radio" name="form[gender]" value="female" id="gender_female" checked="checked"/>
<label for="gender_female">Female</label>
<input type="radio" name=form[gender] value="both" id="gender_both"/>
<input type="radio" name="form[gender]" value="both" id="gender_both"/>
<label for="gender_both">Both</label>
</p>