Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't Capybara filling in these form elements?
    text
    copied!<p>I have been trying to set up Capybara to test a form but I keep getting the error<br> <code>cannot fill in, no text field, text area or password field with id, name, or label 'Name' found</code></p> <p>Here is what I have in my view:</p> <pre><code>&lt;%= form_for(@user) do |f| %&gt; &lt;%= f.label :name %&gt; &lt;%= f.text_field :name %&gt; ... &lt;%= f.submit "Create Account", class: "btn btn-large btn-primary" %&gt; &lt;% end %&gt; </code></pre> <p>which renders the following html:</p> <pre><code>&lt;label for="user_name"&gt;Name&lt;/label&gt; &lt;input id="user_name" name="user[name]" size="30" type="text" /&gt; </code></pre> <p>So it seems like it should be finding the field based on the label. </p> <p>Here is what my user_pages_test.rb file has (I am using Test::Unit and shoulda-context): </p> <pre><code>context "sign up page" do should "add user to database when fields are filled in" do fill_in "Name", with: "Bubbles" ... click_button "Create Account" end end </code></pre> <p>Here is what I've tried so far: </p> <p>1) changing the call to <code>fill_in</code> to match the id with <code>fill_in "user_name", with: "Bubbles"</code><br> 2) changing the call to <code>fill_in</code> to <code>page.fill_in "Name", with: "Bubbles"</code> to match the example in the <a href="http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions#fill_in-instance_method" rel="nofollow noreferrer">documentation</a><br> 3) changing the view to manually add the id <code>"Name"</code> with <code>&lt;%= f.text_field :name, id: "Name" %&gt;</code> (<a href="https://stackoverflow.com/a/13165829/1543283">this</a> answer)<br> 4) changing the call to <code>get sign_up_path</code> to <code>get "/sign_up"</code> (in case it was an issue with the routing) </p> <p>All these still give me the same error, which makes me think that the page isn't being loaded correctly for some reason. However, I have a different (passing) test in the same context that asserts the page has the correct title, so I know the page does get loaded correctly (in the <code>setup</code>). </p> <p>Based on this (and according to <a href="https://stackoverflow.com/a/11356353/1543283">this</a> answer), it seems like the problem might just be that the <code>fill_in</code> method isn't waiting for the page to load before trying to access the fields. According to <a href="https://stackoverflow.com/a/12671639/1543283">this</a> suggestion, I added the line <code>puts page.body</code> in my test to see that the HTML was being loaded completely before it was trying to fill in the fields, and got the following output: </p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt; </code></pre> <p>So, I was pretty sure that <code>fill_in</code> just wasn't waiting for the page to load. I then tried </p> <p>5) changing the <code>Capybara.default_wait_time</code> according to <a href="https://stackoverflow.com/a/11356353/1543283">this</a> answer, but this had no effect. I tried setting it in the <code>ActionDispatch::IntegrationTest</code> class in the test_helper file (where Capybara is included), and also in the test itself. </p> <p>Then I tried adding <code>puts page.body</code> in the passing test (both before and after it asserts the correct title), and I got the same output. THEN, I found <a href="https://stackoverflow.com/a/1852563/1543283">this</a> answer, and finally got the console to print out the page's HTML. I tried one more thing to get Capybara to fill in the fields: </p> <p>6) changed the call to <code>fill_in</code> to say <code>@response.fill_in</code>, since <code>@response</code> seems to do what I thought the <code>page</code> variable was supposed to do. </p> <p>So, I have two questions about this:</p> <p>1) What does the <code>page</code> variable actually refer to? The doctype declaration for my app is just <code>&lt;!DOCTYPE html&gt;</code>, so I have no idea where it gets the old one from.</p> <p>2) Why isn't Capybara able to find/fill_in these fields? </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