Dan SimBed
Dan SimBed

Reputation: 170

Test for consecutive HTML elements in Rails view

I wish to run a test* to check for presence of something like this:

<tr>
    <td> name </td> <td> date </td>
</tr>

Code, such as below:

assert_select "tr" do
  assert_select "td",  name
  assert_select "td",  date
end

looks plausible, but is not correct, as the below for example (which is not the match required) would also pass:

<tr>
    <td> name </td>
</tr>
<tr>
    <td> date </td>
</tr>

I’m struggling to see how this should be approached from the documentation of assert_select.

Thank you

Daniel

Upvotes: 1

Views: 493

Answers (1)

Dan SimBed
Dan SimBed

Reputation: 170

I ended up using a regexs on the result of a css_select, which seems a bit inelegant, but worked for my purposes. If there is a better way I'd be interested to hear it. I used something like this:

pars = css_select "tr"
regexs = /<td>#{name}<\/td>.*<td>#{date}<\/td>/m
match = false
pars.each { |i| if i.to_s =~ regexs then match = true end}
assert match  

Upvotes: 1

Related Questions