1

I have a feature test that is trying to validate an edit action. It's a super simple test but I have not written very many and I'm making some mistake that I can't figure out. Basically, when the page is visited it's not rendering the info that is text that is intended to edit and the "update" button is not there. Here is my error and code for clarity.

TEST

scenario "Staff can edit the response messages" do
  group = Group.create!(name: "Group A", response: "You are now subscribed for updates")
  visit "/groups/#{group.id}/edit"

  user = FactoryGirl.create(:user)
  fill_in "Email", with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"

  expect(page).to have_content("You are now subscribed for updates")
end

enter image description here

VIEW

 <div class="container text-center">
  <div class="row">
  <div class="col-lg-8 col-lg-offset-2 well">
     <%= form_for @group do |form| %>
       <div class="form-group">
         <%= form.label :body, "Edit The Response:", class: "message_label"%>
         <%= form.text_field :response, class: "form-control", placeholder: "New things are happening!" %>
       </div>
       <%= form.submit "Update", class: "btn btn-primary" %>
    <% end %>
  </div>
 </div>
</div>

What is even more strange is when I run "save_and_open_page" the content I expect is present? But, my test still fails saying expected content is not displayed WEIRD!

enter image description here

Cambass
  • 35
  • 4
  • Hi there. In your view I don't see where the text you're testing again should be displayed. The image with the failure message doesn't show all the text that actually is on the page (its cut off on the right side) In general I recommend to use save_and_open_page or save_and_open_screenshot (if you have capybara-screenshot in your bundle) to have a better look on what the page actually displays. – trueunlessfalse Apr 25 '16 at 18:07
  • Yea, that's even more strange because if I use save_and_open_page it shows the content I expect on the page but if I run the test it doesn't show up? – Cambass Apr 25 '16 at 18:12
  • insert `pause 3` after sing in for test purposed only – Малъ Скрылевъ Apr 25 '16 at 18:17
  • Insert pause 3 after sing? I'm sorry but I'm not sure what that means? – Cambass Apr 25 '16 at 18:18
  • The 'sleep 3' could help if the content in question is dynamically loaded (e.g. via ajax). If not, I would really double check that the expected text is included in the dom exactly as you match against it. Line breaks, or splitting it up over multiple tags could cause the problem. – trueunlessfalse Apr 25 '16 at 18:20
  • I'm new to feature test. I really appreciate the help but I still don't understand where to put that? – Cambass Apr 25 '16 at 18:21
  • On your picture it looks like the text 'You are now subscribed' (is the 'for updates' just cut off? is inside an input field. I'm not sure if the have_content matcher will look inside an input field.. – trueunlessfalse Apr 25 '16 at 18:21
  • @trueunlessfalse in mose cases, in some cases, the page isn't yet ready even without ajax/js – Малъ Скрылевъ Apr 25 '16 at 18:22
  • Yeah it's just cut off – Cambass Apr 25 '16 at 18:22
  • http://stackoverflow.com/questions/10503802/how-can-i-check-that-a-form-field-is-prefilled-correctly-using-capybara could help with matching against the input field value, e.g. page.should have_xpath("//input[@value='You are now subscribed for updates']") – trueunlessfalse Apr 25 '16 at 18:23
  • @МалъСкрылевъ A purely server-side rendered page is definitely ready at the point where capybara runs the matcher., but in doubt you can be right, adding the sleep can proof that / if you have a timing issue. – trueunlessfalse Apr 25 '16 at 18:25

1 Answers1

1

As far as I see, the issue is:

  1. test runs visit
  2. fill_in expects the page is already loaded

Second expectation can be wrong, you have to wait page loading either manually with magic sleep 2 or automaticaly using any capybara's finder/expectation

Your test may look like this one:

  group = Group.create!(name: "Group A", response: "You are now subscribed for updates")
  user = FactoryGirl.create(:user)

  visit "/groups/#{group.id}/edit"
  // <= check any static text to ensure the page is loaded
  // expect will wait for a defailt timeout ~ 2 seconds
  expect(page).to have_content("Tulim time text")

  fill_in "Email", with: user.email

  // ...

Example of manual delay

  visit "/groups/#{group.id}/edit"
  // wait a few seconds for page loading
  sleep 5
  fill_in "Email", with: user.email
andrykonchin
  • 2,507
  • 3
  • 18
  • 24