CSS 'contains' Capybara selector and update

My specifications used to have the following lines:

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

(in the definition list next to the heading containing the specified text)

I updated capybara-webkit and now the "contains" selector is not working
(which is kind and straightforward as it is deprecated in CSS3).

I cannot find an easy way to rewrite this. Any ideas?

+4
source share
2 answers

I think you have updated not only capybara-webkit, but also capybara.

Capybara 2.1 now uses the CSS selector driver implementation.

It used to work because Capybara converted the CSS selector to XPath using Nokogiri. Nokogiri seems to support the pseudo-selector :contains (since this code worked previously).

You can rewrite it with XPath, for example:

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

However, I believe that it is not too readable, so it is better to choose the best selector, which will be shorter and more readable.

+2
source

If you want to avoid the xpath definition, 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 
+4
source

Source: https://habr.com/ru/post/1484606/


All Articles