I have a users table that list out the users' ID, name, email and username. What I am attempting to do is verify that a specific entry is in that table.
So essentially what I would like to do is find the row that has ID = 22, then verify that name = John Smith, email = [email protected] and username = jsmith. The able is setup as show below. What I don't understand is a couple of things...
I thought I could do a sort of for loop but can't figure out how to set the has_selector? condition. (below is pseudocode)
page.all('tr').each do |tr|
  next unless tr.has_selector?('td.id = 22') #psuedocode
  expect(tr.get('td.name').to eq("John Smith")
  #other expects here...
end 
Table code
<table class="table table-striped table-condensed">
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Email</th>
      <th>Username</th>
    </tr>
  </thead>
  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td class="id"><%= user.id %></td>
        <td class="name"><%= link_to user.name, user %></td>
        <td class="email"><%= user.base_name %></td>
        <td class="username"><%= user.username %></td>
      </tr>
    <% end %>
  </tbody>
</table>
                A table in HTML is a collection of <tr> and <td> elements for rows and cells, respectively. In the sample test, the table can be located as a WebElement using its ID as follows: To get all the rows from a table, the findElements () method is called on simpleTable and the tagName strategy is used to get all <tr> elements. These are rows of a table.
In the Pandas DataFrame we can find the specified row value with the using function iloc (). In this function we pass the row number as parameter. Index Position : Index position of rows in integer or list of integer.
The test iterates through the row and columns to print the data in the following way: This method comes in handy when you have a test that needs to verify data in a table. There's more… We can also use CSS selectors or XPath for locating table rows and cells using index matching.
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 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With