RSpec/Capybara - 测试 table 的页面
RSpec/Capybara - Testing a page for a table
有谁知道我可以使用什么匹配器来检查水豚中的页面以查看它是否包含 html table?然后也许是一个匹配器来检查 table 里面是否有特定的内容?
expect(page).to have_content(table)
有这样的吗? :s
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
如果页面上只有一个 table,@MilesStanfield 的答案就可以正常工作。如果有多个并且你想检查包含特定内容的table是否存在你可以做
expect(page).to have_css('table', text: 'content to check')
验证网页中任何 table 数据都可以使用水豚轻松完成,如下所示:
特征文件:
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 |
table 验证的步骤定义:
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
而如果你只想验证table是否存在,那么你可以使用
expect(page).to have_css 'table'
希望这个解决方案能帮到你。 (我在黄瓜中使用的 spreewald 库中移植了一个 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
和我的 support/helpers/table_helpers.rb
有谁知道我可以使用什么匹配器来检查水豚中的页面以查看它是否包含 html table?然后也许是一个匹配器来检查 table 里面是否有特定的内容?
expect(page).to have_content(table)
有这样的吗? :s
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
@MilesStanfield 的答案就可以正常工作。如果有多个并且你想检查包含特定内容的table是否存在你可以做
expect(page).to have_css('table', text: 'content to check')
验证网页中任何 table 数据都可以使用水豚轻松完成,如下所示:
特征文件:
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 |
table 验证的步骤定义:
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
而如果你只想验证table是否存在,那么你可以使用
expect(page).to have_css 'table'
希望这个解决方案能帮到你。 (我在黄瓜中使用的 spreewald 库中移植了一个 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
和我的 support/helpers/table_helpers.rb