2016-02-25 61 views
0

有谁知道我可以用什么匹配器检查水豚的页面,看看它是否包含一个HTML表?然后可能是匹配器来检查表中是否包含特定内容?RSpec /水豚 - 测试表的页面

expect(page).to have_content(table)

像这样的东西? :s

回答

1
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 
0

@MilesStanfield答案将工作正常,如果只有一个表在页面上。如果有倍数要检查包含具体内容的表存在,你可以做

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

验证任何表与网页中的数据可以与水豚可以轻松完成,如下图所示:

特性文件:

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  | 

步骤定义为表验证:

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 

如果你只是想确认表存在,那么你可以使用

expect(page).to have_css 'table' 
+0

这是黄瓜而不是RSpec – PhilT

0

我希望这个解决方案可以帮助你。 (I端口在使用黄瓜施普利瓦尔德库table_helpers.rb)

这里是我的情况:

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 

和我的支持/助理/ table_helpers.rb

table_helpers.rb