How to Get a HTML Table Row with Capybara

How to get a HTML table row with Capybara

I think you are looking to do:

page.all('#blah tr').each do |tr|
next unless tr.has_selector?('a[href*="HONDA"]')

# Do stuff with trs that meet the href requirement
puts tr.text
end
#=> link 29 33 485 45.2934,00 EUR
#=> link 22 93 485 38.336.934,123 EUR

This basically says to:

  1. Find all trs in the element with id 'blah'
  2. Iterate through each of the trs
  3. If the tr does not have a link that has a href containing HONDA, ignore it
  4. Otherwise, output the text of the row (that matches the criteria). You could do whatever you need with the tr here.

You could also use xpath to collapse the above into a single statement. However, I do not think it is as readable:

page.all(:xpath, '//div[@id="blah"]//tr[.//a[contains(@href, "HONDA")]]').each do |tr|
# Do stuff with trs that meet the href requirement
puts tr.text
end
#=> link 29 33 485 45.2934,00 EUR
#=> link 22 93 485 38.336.934,123 EUR

Here is an example of how to inspect each matching row's link url and column values:

page.all('#blah tr').each do |tr|
next unless tr.has_selector?('a[href*="HONDA"]')

# Do stuff with trs that meet the href requirement
href = tr.find('a')['href']
column_value_1 = tr.all('td')[1].text
column_value_2 = tr.all('td')[2].text

puts href, column_value_1, column_value_2
end
#=> file:///C:/Scripts/Misc/Programming/Capybara/afile?key=HONDA
#=> 29 33 485
#=> 45.2934,00 EUR
#=> file:///C:/Scripts/Misc/Programming/Capybara/afile?key=HONDA
#=> 22 93 485
#=> 38.336.934,123 EUR

Finding a specific row in a table using capybara

A nice clean approach would be to add a "data-user-id" attribute to each tr element and then find the row you want with tr = find('tr[data-user-id="22"]'), but if that's not an option there are a number of ways to do this. Either of

td = page.find(:css, 'td.id', text: /^22$/) # find the id td with text of exactly 22
tr = td.find(:xpath, './parent::tr') # get the parent tr of the td
expect(tr).to have_css('td.name', text: 'John Smith')

or finding the row using just an xpath like

tr = page.find(:xpath, ".//tr[./td[@class='id'][text()='22']]")
expect(tr).to have_css('td.name', text: 'John Smith')

should do what you want. If you want to stick with the looping approach (not recommended because it will be slow - and the way your loop is structured it could just not verify anything) it would be

page.all('tr').each do |tr|
next unless tr.has_css?('td.id', text: /^22$/)
expect(tr).to have_css('td.name', text: "John Smith")
#other expects here...
end

How to assert on number of html table rows in ruby using capybara + cucumber

I went with this in the end:

Then /^I should see "(.*)" once$/ do |text|
within_table('myTable') do
should have_xpath("//tr", :text => text, :count => 1)
end
end

which seemed suitably elegant.

I realise the other answers work but this seems to read well.

Any comments?

how to click on a table row using capybara & rspec

May be you should first get to know the testing in rails. Check this out! http://railscasts.com/episodes/275-how-i-test it is really helpful. You can give your tr a class (say) .tr and do

page.find(:css, ".tr").click()

I hope this works, worked in my case!

Can someone help me select row from a table with capybara

You can't use expect(page).to have_css('tr.tvRow .tvCell:nth-child(6) img[tooltip]', exact_text: "Invisível"). The text you are looking for isn't text content of an element (which the text and exact_text options would match against) it's an attribute value - https://www.w3schools.com/html/html_attributes.asp. If you go through the learning experience I sent in my previous answer you would learn about CSS attribute selectors - https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors - which would lead you to something like

expect(page).to have_css('tr.tvRow .tvCell:nth-child(6) img[tooltip="Invisível"]')

if you also need to validate it is in a specific row then you could use the CSS nth-child selector like

expect(page).to have_css('tr.tvRow:nth-child(1) .tvCell:nth-child(6) img[tooltip="Invisível"]')

I would really recommend you go through the learning experience I linked you to - https://flukeout.github.io/ - and learn the CSS if that's what you want to be using to select elements.

RSpec/Capybara - Testing a page for a table

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

How to get a particular table cell text with row column values in Capybara without looping?

Capybara also supports XPath for querying:

page.has_selector?(:xpath, '//table/tr[2]/td[3]')

There is this nice cheat sheet in case you want deepen your knowledge on how to traverse documents with XPath.

HTH.

Cucumber - Capybara search for text within table row

For anyone else who stumbles on this question, I think a better way to do this is:

find('tr', text: 'My title').should have_content(goal)

This way you're not adding a title attribute when it's not really needed

Capybara/HTML - Check text in row below

CSS selectors are not good for the structure you're trying to evaluate, but XPath will work well for it. Something like the following should work

page.find(:xpath, ".//td[contains(., 'Label1:')]/following-sibling::td[1]/span").should have_text('Value1') 

How to get row count from a table using ruby capybara?

You can find all the elements matching a selector using #all so

page.all(:css, 'table tr').size

will count all the table rows in the page. If there are multiple tables and you want for a specific table you can increase the specificity of the CSS selector. You can also find the specific table and then call all on that

table = page.find(:css, 'table#my_table_id')
row_count = table.all(:css, 'tr').size


Related Topics



Leave a reply



Submit