Cucumber: how to check specific text in a column?

I am trying to use a cucumber to check the contents of a Ruby on Rails view in my application. The view displays information in the table. I want to be able to check the contents of only one column of this table. When I use the definition of the web step "I should see" , it checks the entire page. Is there an easy way to do this?

For example: column.should have_content("text")

+4
source share
2 answers

Capybara's built-in scope may be slightly easier to maintain than those with xpaths

 within "#id_for_your_tr" within('td', :class=> 'class_for_your_column') page.should have_content "foo" end end 
+6
source

Using capybara + cucumber, let's say your step

 Then I should see the following (#MYTABLE) | FOO | 94040 | "friendly" | | BAR | 94050 | "competition"| 

step definition

 Then /^I should see the following games:$/ do |expected_table| table_results = page.find('#DOM_ID') end 

My complex approach to defining

 When /^(.*) in the "([^\"]*)" column of the "([^\"]*)" row$/ do | action, column_title, row_title| col_number = 0 all(:xpath, "//*[(th|td)/descendant-or-self::*[contains(text(), '#{column_title}')]]/th").each do |element| col_number += 1 break if element.has_content?(column_title) end within :xpath, "//*[(th|td)/descendant-or-self::*[contains(text(), '#{row_title}')]]/td[#{col_number}]" do When action end end 

to check that the table structure is genetic, you can use check the number of rows of X page.should have_selector ('table tr' ,: count => X)

+1
source

All Articles