CSS 'contains' selector & upgrade of Capybara

五迷三道 提交于 2019-12-10 13:46:47

问题


Previously my specs had these lines:

within "h3:contains('FooBar text') + dl" do
  page.should have_content 'FizzBuzz'
end

(within definition list next from header that contains specified text)

I upgraded capybara-webkit and now 'contains' selector does not work
(which is kind a fine and understandable since it's deprecated in CSS3).

I can't figure out an easy way to rewrite this. Any ideas?


回答1:


I think you upgraded not only capybara-webkit but also capybara.

Capybara 2.1 now uses driver's implementation of CSS selectors.

Previously it worked because Capybara converted CSS selector to XPath using Nokogiri. Nokogiri seems to support :contains pseudo selector (as this code worked previously).

You can rewrite it using XPath like:

within(:xpath, "//dl[preceding-sibling::h3[contains(text(),'FooBar text')]]") do
  page.should have_content 'FizzBuzz'
end

However, I think it's not too readable so it may be better to choose a better selector that will be more short and readable.




回答2:


If you want to avoid having to figure out the xpath, you can use Nokogiri::CSS.xpath_for It returns an array so you need to do [0]

within :xpath, Nokogiri::CSS.xpath_for("<CSS SELECTOR>")[0] do


来源:https://stackoverflow.com/questions/16937009/css-contains-selector-upgrade-of-capybara

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!