Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS 'contains' selector & upgrade of Capybara

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?

like image 823
Arnis Lapsa Avatar asked Jun 05 '13 10:06

Arnis Lapsa


People also ask

Is there a CSS selector for elements containing certain text?

The [attribute~=value] selector is used to select elements with an attribute value containing a specified word.

How do I find the CSS selector of an element?

Simply right click and click Inspect Element. This will bring up the CSS selectors for that element.


2 Answers

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
like image 189
Karthik T Avatar answered Sep 19 '22 03:09

Karthik T


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.

like image 40
Andrei Botalov Avatar answered Sep 18 '22 03:09

Andrei Botalov