Added has_table?

This commit is contained in:
Jonas Nicklas 2010-02-20 22:41:53 +01:00
parent 65232a0dbc
commit 823b2dd28b
5 changed files with 181 additions and 3 deletions

View File

@ -222,6 +222,14 @@ module Capybara
has_no_xpath?(XPath.select(locator, options))
end
def has_table?(locator, options={})
has_xpath?(XPath.table(locator, options))
end
def has_no_table?(locator, options={})
has_no_xpath?(XPath.table(locator, options))
end
def save_and_open_page
require 'capybara/save_and_open_page'
Capybara::SaveAndOpenPage.save_and_open_page(body)

View File

@ -55,8 +55,16 @@ module Capybara
append("/descendant-or-self::*[contains(.,#{s(locator)})]")
end
def table(locator)
append("//table[@id=#{s(locator)} or contains(caption,#{s(locator)})]")
def table(locator, options={})
conditions = ""
if options[:rows]
row_conditions = options[:rows].map do |row|
row = row.map { |column| "*[self::td or self::th][text()=#{s(column)}]" }.join(sibling)
"tr[./#{row}]"
end.join(sibling)
conditions << "[.//#{row_conditions}]"
end
append("//table[@id=#{s(locator)} or contains(caption,#{s(locator)})]#{conditions}")
end
def fieldset(locator)
@ -123,6 +131,11 @@ module Capybara
protected
# place this between to nodes to indicate that they should be siblings
def sibling
'/following-sibling::*[1]/self::'
end
def add_field(locator, field, options={})
postfix = extract_postfix(options)
xpath = append("#{field}[@id=#{s(locator)}]#{postfix}")

View File

@ -0,0 +1,96 @@
shared_examples_for "has_table" do
describe '#has_table?' do
before do
@session.visit('/tables')
end
it "should be true if the field is on the page" do
@session.should have_table('Deaths')
@session.should have_table('villain_table')
end
it "should be false if the field is not on the page" do
@session.should_not have_table('Monkey')
end
context 'with rows' do
it "should be true if a table with the given rows is on the page" do
@session.should have_table('Ransom', :rows => [['2007', '$300', '$100']])
@session.should have_table('Deaths', :rows => [['2007', '66', '7'], ['2008', '123', '12']])
end
it "should be true if the given rows are incomplete" do
@session.should have_table('Ransom', :rows => [['$300', '$100']])
end
it "should be false if the given table is not on the page" do
@session.should_not have_table('Does not exist', :selected => 'John')
end
it "should be false if the given rows contain incorrect elements" do
@session.should_not have_table('Ransom', :rows => [['2007', '$1000000000', '$100']])
end
it "should be false if the given rows are incorrectly ordered" do
@session.should_not have_table('Ransom', :rows => [['2007', '$100', '$300']])
end
it "should be false if the only some of the given rows are correct" do
@session.should_not have_table('Deaths', :rows => [['2007', '66', '7'], ['2007', '99999999', '12']])
end
it "should be false if the given rows are out of order" do
@session.should_not have_table('Deaths', :rows => [['2007', '123', '12'], ['2007', '66', '7']])
end
end
end
describe '#has_no_table?' do
before do
@session.visit('/tables')
end
it "should be false if the field is on the page" do
@session.should_not have_no_table('Deaths')
@session.should_not have_no_table('villain_table')
end
it "should be true if the field is not on the page" do
@session.should have_no_table('Monkey')
end
context 'with rows' do
it "should be false if a table with the given rows is on the page" do
@session.should_not have_no_table('Ransom', :rows => [['2007', '$300', '$100']])
@session.should_not have_no_table('Deaths', :rows => [['2007', '66', '7'], ['2008', '123', '12']])
end
it "should be false if the given rows are incomplete" do
@session.should_not have_no_table('Ransom', :rows => [['$300', '$100']])
end
it "should be true if the given table is not on the page" do
@session.should have_no_table('Does not exist', :selected => 'John')
end
it "should be true if the given rows contain incorrect elements" do
@session.should have_no_table('Ransom', :rows => [['2007', '$1000000000', '$100']])
end
it "should be true if the given rows are incorrectly ordered" do
@session.should have_no_table('Ransom', :rows => [['2007', '$100', '$300']])
end
it "should be true if the only some of the given rows are correct" do
@session.should have_no_table('Deaths', :rows => [['2007', '66', '7'], ['2007', '99999999', '12']])
end
it "should be true if the given rows are out of order" do
@session.should have_no_table('Deaths', :rows => [['2007', '123', '12'], ['2007', '66', '7']])
end
end
end
end

View File

@ -56,6 +56,7 @@ shared_examples_for "session" do
it_should_behave_like "has_button"
it_should_behave_like "has_field"
it_should_behave_like "has_select"
it_should_behave_like "has_table"
it_should_behave_like "select"
it_should_behave_like "uncheck"
it_should_behave_like "unselect"

View File

@ -59,4 +59,64 @@
</td>
</tr>
</table>
</form>
</form>
<table>
<caption>Ransom</caption>
<thead>
<tr>
<th>Year</th>
<th>Governmental</th>
<th>Private</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">2007</th>
<td>$300</td>
<td>$100</td>
</tr>
<tr>
<th scope="row">2008</th>
<td>$123</td>
<td>$897</td>
</tr>
<tr>
<th scope="row">2009</th>
<td>$543</td>
<td>$99</td>
</tr>
</tbody>
</table>
<table>
<caption>Deaths</caption>
<thead>
<tr>
<th>Year</th>
<th>Sharks with lasers</th>
<th>Flaming volcano</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">2007</th>
<td>66</td>
<td>7</td>
</tr>
<tr>
<th scope="row">2008</th>
<td>123</td>
<td>12</td>
</tr>
<tr>
<th scope="row">2009</th>
<td>913</td>
<td>13</td>
</tr>
</tbody>
</table>