Skip to content

Commit

Permalink
Added has_table?
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Feb 20, 2010
1 parent 65232a0 commit 823b2dd
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/capybara/session.rb
Expand Up @@ -222,6 +222,14 @@ def has_no_select?(locator, options={})
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)
Expand Down
17 changes: 15 additions & 2 deletions lib/capybara/xpath.rb
Expand Up @@ -55,8 +55,16 @@ def content(locator)
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)
Expand Down Expand Up @@ -123,6 +131,11 @@ def #{type}_field(locator)

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}")
Expand Down
96 changes: 96 additions & 0 deletions spec/dsl/has_table_spec.rb
@@ -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



1 change: 1 addition & 0 deletions spec/session_spec.rb
Expand Up @@ -56,6 +56,7 @@ def extract_results(session)
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"
Expand Down
62 changes: 61 additions & 1 deletion spec/views/tables.erb
Expand Up @@ -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>

0 comments on commit 823b2dd

Please sign in to comment.