Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check for multiple occurrences of a word using Capybara (or Webrat, I guess) and Cucumber?
    text
    copied!<p>I know that <code>/Interface \d/</code> occurs three times on the page. But I don't know how to test for this with Capybara in Cucumber. Here was my first attempt:</p> <pre><code>Then /^(?:|I )should see \/([^\/]*)\/ (\d+)(?:x|X| times?)?$/ do |regexp, count| regexp = Regexp.new(regexp) count = count.to_i if page.respond_to? :should page.should have_xpath('//*', { :text =&gt; regexp, :count =&gt; count }) else assert page.has_xpath?('//*', { :text =&gt; regexp, :count =&gt; count }) end end </code></pre> <p>However, this returns false for my <code>Then I should see /Interface \d+/ 3 times</code>.</p> <p>I figured out that this is because <a href="http://rdoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers%3ahas_xpath%3F" rel="nofollow"><code>has_xpath</code></a> uses <a href="http://rdoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders%3aall" rel="nofollow"><code>all</code></a>. Putting this in my test:</p> <pre><code>puts all(:xpath, '//*', { :text =&gt; regexp}).map {|e| pp e} </code></pre> <p>results in </p> <pre><code>#&lt;Capybara::Element tag="html" path="/html"&gt; #&lt;Capybara::Element tag="body" path="/html/body"&gt; #&lt;Capybara::Element tag="div" path="/html/body/div"&gt; #&lt;Capybara::Element tag="div" path="/html/body/div/div[2]"&gt; #&lt;Capybara::Element tag="table" path="/html/body/div/div[2]/table"&gt; #&lt;Capybara::Element tag="tbody" path="/html/body/div/div[2]/table/tbody"&gt; #&lt;Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[1]"&gt; #&lt;Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[1]/td[3]"&gt; #&lt;Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[2]"&gt; #&lt;Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[2]/td[3]"&gt; #&lt;Capybara::Element tag="tr" path="/html/body/div/div[2]/table/tbody/tr[3]"&gt; #&lt;Capybara::Element tag="td" path="/html/body/div/div[2]/table/tbody/tr[3]/td[3]"&gt; </code></pre> <p>So I am getting a count of every step along the way to the elements that contain my text. :-\</p> <p>I thought maybe <a href="http://rdoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers%3ahas_content%3F" rel="nofollow"><code>has_content</code></a> would save me, but it doesn't accept a count.</p> <p>Help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload