44 notes &
Testing element ordering with Capybara
I’m sure I did this with xpath once before, but for now it eludes me, so I went with another, simpler approach.
scenario "reordering links" do
m1, m2 = MenuItem.make, MenuItem.make
visit menu_items_path
page.all(:xpath, "//div[@id='nav']//a/text()").map(&:text).should == [m1.text, m2.text]
within :css, rails_dom_id_selector(m2) do
click '↑'
end
visit menu_items_path
page.all(:xpath, "//div[@id='nav']//a/text()").map(&:text).should == [m2.text, m1.text]
end
So in short:
- run an all query to get the nodes you’re interested in (xpath or css)
- map over them to grab the data you are interested in, e.g. text contents
- run an array comparison against the data you want to be there
For more on getting the data out of the capybara nodes see Capybara’s RDoc, or experiment. Nokogiri can be useful too if you want to go to a lower level / use its api. Last I looked the Capybara rack-test nodes were just wrapping nokogiri nodes.