From 334bca5d778b86870f93a5711456005dd5a6cc00 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Tue, 24 Nov 2009 21:45:52 +0100 Subject: [PATCH] Added within table method --- lib/capybara/dsl.rb | 3 +- lib/capybara/session.rb | 6 ++++ spec/session_spec.rb | 21 ++++++++++++++ spec/views/tables.erb | 62 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 spec/views/tables.erb diff --git a/lib/capybara/dsl.rb b/lib/capybara/dsl.rb index db666c46..9054b643 100644 --- a/lib/capybara/dsl.rb +++ b/lib/capybara/dsl.rb @@ -45,7 +45,8 @@ module Capybara SESSION_METHODS = [ :visit, :body, :click_link, :click_button, :fill_in, :choose, :has_xpath?, :has_css?, :check, :uncheck, :attach_file, :select, :has_content?, :within, :within_fieldset, - :save_and_open_page, :find_field, :find_link, :find_button, :field_labeled + :within_table, :save_and_open_page, :find_field, :find_link, :find_button, + :field_labeled ] SESSION_METHODS.each do |method| class_eval <<-RUBY, __FILE__, __LINE__+1 diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 9a6ac407..85ea4f76 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -106,6 +106,12 @@ class Capybara::Session end end + def within_table(locator) + within "//table[@id='#{locator}' or contains(caption,'#{locator}')]" do + yield + end + end + def save_and_open_page require 'capybara/save_and_open_page' Capybara::SaveAndOpenPage.save_and_open_page(body) diff --git a/spec/session_spec.rb b/spec/session_spec.rb index 6e531b70..5a3f0cd3 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -615,7 +615,28 @@ shared_examples_for "session" do end extract_results(@session)['villain_name'].should == 'Goldfinger' end + end + + describe '#within_table' do + before do + @session.visit('/tables') + end + it "should restrict scope to a fieldset given by id" do + @session.within_table("girl_table") do + @session.fill_in("Name", :with => 'Christmas') + @session.click_button("Create") + end + extract_results(@session)['girl_name'].should == 'Christmas' + end + + it "should restrict scope to a fieldset given by legend" do + @session.within_table("Villain") do + @session.fill_in("Name", :with => 'Quantum') + @session.click_button("Create") + end + extract_results(@session)['villain_name'].should == 'Quantum' + end end end diff --git a/spec/views/tables.erb b/spec/views/tables.erb new file mode 100644 index 00000000..115a4ba6 --- /dev/null +++ b/spec/views/tables.erb @@ -0,0 +1,62 @@ +
+ + + + + + + + + + + +
Agent
+ + + +
+ +
+
+ +
+ + + + + + + + + + + +
Girl
+ + + +
+ +
+
+ +
+ + + + + + + + + + + +
Villain
+ + + +
+ +
+
\ No newline at end of file