diff --git a/lib/webcat/driver/culerity_driver.rb b/lib/webcat/driver/culerity_driver.rb index 05779b99..2114a2a3 100644 --- a/lib/webcat/driver/culerity_driver.rb +++ b/lib/webcat/driver/culerity_driver.rb @@ -15,11 +15,7 @@ class Webcat::Driver::Culerity return value if value and not value.empty? end - def value - node.value - end - - def value=(value) + def set(value) node.set(value.to_s) end diff --git a/lib/webcat/driver/rack_test_driver.rb b/lib/webcat/driver/rack_test_driver.rb index 4dd871e9..d8dc8026 100644 --- a/lib/webcat/driver/rack_test_driver.rb +++ b/lib/webcat/driver/rack_test_driver.rb @@ -12,12 +12,12 @@ class Webcat::Driver::RackTest return value.to_s if value end - def value - node['value'].to_s - end - - def value=(value) - node['value'] = value.to_s + def set(value) + if tag_name == 'input' + node['value'] = value.to_s + elsif tag_name == "textarea" + node.content = value.to_s + end end def click @@ -64,7 +64,7 @@ class Webcat::Driver::RackTest agg << param(select['name'].to_s, (option['value'] || option.text).to_s) if option agg end - params << param(button[:name], button.value) if button[:name] + params << param(button[:name], button[:value]) if button[:name] params.join('&') end diff --git a/lib/webcat/session.rb b/lib/webcat/session.rb index e07f8220..5c37a6fe 100644 --- a/lib/webcat/session.rb +++ b/lib/webcat/session.rb @@ -30,8 +30,7 @@ class Webcat::Session end def fill_in(locator, options={}) - element = find_field(locator) { |id| "//input[@type='text'][@id='#{id}']" } - element.value = options[:with] + find_field(locator, :text_field, :text_area).set(options[:with]) end def body @@ -40,22 +39,41 @@ class Webcat::Session private - def find_field(locator) - element = find_element(yield(locator), :loose => true) - element ||= begin - label = find_element("//label[text()='#{locator}']") - find_element(yield(label[:for])) + def find_field(locator, *kinds) + find_field_by_id(locator, *kinds) or find_field_by_label(locator, *kinds) + end + + FIELDS_PATHS = { + :text_field => proc { |id| "//input[@type='text'][@id='#{id}']" }, + :text_area => proc { |id| "//textarea[@id='#{id}']" } + } + + def find_field_by_id(locator, *kinds) + kinds.each do |kind| + path = FIELDS_PATHS[kind] + element = driver.find(path.call(locator)).first + return element if element end - element + return nil + end + + def find_field_by_label(locator, *kinds) + kinds.each do |kind| + label = driver.find("//label[text()='#{locator}']").first + if label + element = find_field_by_id(label[:for], kind) + return element if element + end + end + return nil end def find_element(*locators) - options = if locators.last.is_a?(Hash) then locators.pop else {} end locators.each do |locator| element = driver.find(locator).first return element if element end - raise Webcat::ElementNotFound, "element not found" unless options[:loose] + raise Webcat::ElementNotFound, "element not found" end end diff --git a/spec/drivers_spec.rb b/spec/drivers_spec.rb index 11749828..85ead06b 100644 --- a/spec/drivers_spec.rb +++ b/spec/drivers_spec.rb @@ -46,14 +46,10 @@ shared_examples_for 'driver' do @driver.find('//a')[1][:rel].should be_nil end - it "should extract field values" do - @driver.find('//input').first.value.should == 'monkey' - end - it "should allow assignment of field value" do - @driver.find('//input').first.value.should == 'monkey' - @driver.find('//input').first.value = 'gorilla' - @driver.find('//input').first.value.should == 'gorilla' + @driver.find('//input').first[:value].should == 'monkey' + @driver.find('//input').first.set('gorilla') + @driver.find('//input').first[:value].should == 'gorilla' end it "should extract node tag name" do diff --git a/spec/session_spec.rb b/spec/session_spec.rb index 0851fb36..8de42bc1 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -123,19 +123,33 @@ shared_examples_for "session" do end describe "#fill_in" do - it "should fill in a field by id" do + before do @session.visit('/form') + end + + it "should fill in a field by id" do @session.fill_in('form_first_name', :with => 'Harry') @session.click_button('awesome') YAML.load(@session.body)['first_name'].should == 'Harry' end it "should fill in a field by label" do - @session.visit('/form') @session.fill_in('First Name', :with => 'Harry') @session.click_button('awesome') YAML.load(@session.body)['first_name'].should == 'Harry' end + + it "should fill in a textarea by id" do + @session.fill_in('form_description', :with => 'Texty text') + @session.click_button('awesome') + YAML.load(@session.body)['description'].should == 'Texty text' + end + + it "should fill in a textarea by label" do + @session.fill_in('Description', :with => 'Texty text') + @session.click_button('awesome') + YAML.load(@session.body)['description'].should == 'Texty text' + end end end