Warm tip: This article is reproduced from serverfault.com, please click

Is it possible to find out if an element comes before the second element in Capybara?

发布于 2020-09-22 04:06:39

I was doing tests with Caplybara, Cucumber and WebDriver, so I looked at two elements on top of each other, so I thought.

  • In a simpler way, is it possible to know if an element comes before the second?
  • In a more complex way, is it possible to know if one element has a position on top of the other?

The structure is simple

<div id="father-parent">
    <p id="children-0">Firt element</p>
    <p id="children-1">Second element</p>
</div>
Questioner
WEB Last Wolf
Viewed
0
Thomas Walpole 2020-09-22 12:26:11

If what you're trying to verify is that children-0 comes right before children-1 you can use the CSS adjacent sibling selector

expect(page).to have_css('#children-0 + #children-1')

If you just want to verify that children-0 is a prior sibling of children-1 (not necessarily adjacent) you can use the general sibling selector

expect(page).to have_css('#children-0 ~ #children-1')