Reiss Johnson
Reiss Johnson

Reputation: 1509

RSpec/Capybara - Testing a page for a table

Does anyone know what matcher I can use to check a page in capybara to see if it contains a html table? And then maybe a matcher to check if that table has specific content inside?

expect(page).to have_content(table)

anything like this? :s

Upvotes: 2

Views: 5001

Answers (4)

Hai Nguyen
Hai Nguyen

Reputation: 458

I hope this solution can help you. (I port a table_helpers.rb in spreewald library that using in cucumber.)

Here is my scenario:

scenario "table's data after click to sort with '会場名称'", js: true do
  screening_rooms = (1..5).each do |n|
     create :screening_room, m_branch_id: "#{n}", name: "name-#{n}"
  end
  visit screening_rooms_path
  within("table#common_list thead") do
    click_link "会場名称"
  end
  expected_table = <<-EOF
    |拠点|会場名称|会場住所|登録日時|更新日時| |
    | * |name-5 |*      |*     |*      |*|
    | * |name-4 |*      |*     |*      |*|  
    | * |name-3 |*      |*     |*      |*|   
    | * |name-2 |*      |*     |*      |*|   
    | * |name-1 |*      |*     |*      |*|      
  EOF

  document = Nokogiri::HTML(page.body)
  tables = document.xpath('//table').collect {|table| table.xpath('.//tr').collect {|row| row.xpath('.//th|td')}}
  parsed_table = parse_table(expected_table)
  tables.should contain_table(parsed_table)
end  

and my support/helpers/table_helpers.rb

table_helpers.rb

Upvotes: 0

Sarabjit Singh
Sarabjit Singh

Reputation: 790

Verification of any table with data in a web page can be easily done with capybara as shown below:

Feature file:

Scenario: Verify content of html table
When Admin is on "www.abc.com" page
Then Admin verifies that following contents of html table:
| TableHeading1 | TableHeading2 | TableHeading3 | TableHeading4 |
| Value1        | Value2        | Value3        | Value4        |

Step Definition for table verification:

And(/^(\S*) Admin verifies that following contents of html table:$/) do | table|
  // to verify table header
  table.headers.each_with_index do |value, index|
    tableHeadingCss = "#{someTableId} > thead > tr > th:eq(#{index})"
    selectorText = page.evaluate_script("#{tableHeadingCss}').text().trim()")
    selectorText.should eq value
  end

  // to verify table cell contents
  table.raw[1...table.raw.length].each_with_index do |row, row_index|
    row.each_with_index do |value, index|
      tableCellContentCss = "#{someTableId} > tbody > tr:eq(#{row_index}) > td:eq(#{index})"
      selectorText = page.evaluate_script("#{tableCellContentCss}').text().trim()")
      selectorText.should eq value
    end
  end
end

And if you just want to verify that table exists, then you can use

expect(page).to have_css 'table'

Upvotes: -1

Thomas Walpole
Thomas Walpole

Reputation: 49910

@MilesStanfield answers will work fine if there's only one table on the page. If there are multiples and you want to check that a table containing specific content exists you can do

expect(page).to have_css('table', text: 'content to check')

Upvotes: 3

MilesStanfield
MilesStanfield

Reputation: 4639

describe 'table' do
  it 'exists' do
    expect(page).to have_css 'table'
  end

  it 'has something inside' do
    within 'table' do
      expect(page).to have_text 'foo bar'
    end
  end
end

Upvotes: 2

Related Questions