Uncertain Capybara match - must have_content

I use Capybara and Rspec in a Rails application, and I continue to drop some of my tests with this post:

Failure/Error: it { should have_content('error') } Capybara::Ambiguous: Ambiguous match, found 2 elements matching xpath "/html" 

And this is logical, because in my test my application should display two messages with the contents of "error".

 <% if object.errors.any? %> <div id="error_explanation"> <div class="alert alert-error"> <%= t('the_form_contains') %> <%= pluralize(object.errors.count, t('error')) %>. </div> <ul> <% object.errors.full_messages.each do |msg| %> <li>* <%= msg %></li> <% end %> </ul> </div> <% end %> 

And here is my application layout:

 <!DOCTYPE html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> <%= csrf_meta_tags %> <%= render 'layouts/shim' %> </head> <body> <%= render 'layouts/header' %> <div class="container"> <div class="row-fluid"> <div class="offset2 span8 offset2"> <% flash.each do |key,value| %> <div class="alert alert-<%= key %>"><%= value %></div> <% end %> </div> </div> <%= yield %> <%= render 'layouts/footer' %> <%= debug(params) if Rails.env.development? %> </div> </body> </html> 

Do you know a way (as an option) for it to go through?

EDIT

I used save_and_open_page and I did not find any additional html tag nor error messages outside the page. But when I delete <!DOCTYPE html> , then it works. Should I have a <!DOCTYPE html> and an opening <html> ? Can I delete <!DOCTYPE html> without consequences?

 <!DOCTYPE html> <html> <head> <title>Le Troquet</title> <link data-turbolinks-track="true" href="/assets/application.css" media="all" rel="stylesheet" /> <script data-turbolinks-track="true" src="/assets/application.js"></script> <!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> . . . . . <!-- There is no html tag here --> . . . . </body> </html> 
+4
source share
3 answers

It seems like the problem is that you are either:

  • there are two <html> tags on your page or maybe
  • the error text appears outside the <html></html> tags.

See this discussion: Ambiguous match, 2 elements found matching xpath "/ html" .

+4
source

Check items outside of the closing tag </html> . In my case (HAML), I had a javascript tag that was incorrectly deflated

 %html ... :javascript 

Fix

 %html ... :javascript ... 

Indentation: javascript, so it was contained in the html tag, solved the problem.

+4
source

Ok, I understand the problem. When I use should have_content , Capybara searches through the entire layout of the content. But he finds two html tags that make him fail because he does not know which one is correct. And why are there two html tags? Because Capybara does not ignore <!DOCTYPE html> .

I solved my problem, but this is more a workaround. Instead of searching for content, I did Capybara to search for the 'error' div, which contains error messages. This way, he does not need to search the entire application layout, and he is not stuck with two html tags.

So I just had to replace it { should have_content('error') } with it { should have_selector('div', text: 'error') } and it worked.

-2
source

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


All Articles