Code to nowhere

Blog :: N. T. Rutherford

11 notes &

XPath to test 2 child node properties

This is actually quite easy, it’s just a little different to how we normally structure code. I was expecting to use a boolean and operator, but then thought about what XPath actually does, and came up with the following solution.

(Rails, RSpec, Capybara example):

  page.should have_xpath("//url/loc[.='http://www.example.com/']/../changefreq[.='weekly']")

The idea is to select some nodes, in this case url, then state that they have child nodes with particular properties. Once we descend into the first child node (loc) we can climb up to the url node again using .. then search a second time (and so on) for other nodes we are interested in, in this case changefreq. This is more precise than running 2 xpaths which only check for one property or the other, as this ensures the child nodes exist within the same url node.

Filed under capybara xpath rails

  1. nruth posted this