Bitwise
Bitwise

Reputation: 8461

Calling RSpec sleep

I'm writing a feature test right now that is trying test an edit action. It's a bit strange to me because it's not seeing what I expect when I run RSpec normally but when I use save_and_open_page command it shows that what I'm expecting is actually there in the browser. I think this is a selenium issue but I'm new to rspec and I'm not sure. I've been suggested to use "Sleep" or "Pause" but I'm not sure how to use these methods and I can't find any good docs on it. I'm wondering if someone can show me a valid way to use this on my current code?

TEST

require "rails_helper"

RSpec.feature "Edit 'Bounce Back' Message", :type => :feature do
given!(:group) { Group.create!(name: "Group A", response: "You are now    subscribed for updates") }

scenario "Staff can see the response messages" do
visit "/groups"

  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")
  expect(page).to have_content("Group a")
 end
scenario "Staff can edit the response messages" do
  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 have now subscribed for updates")
 end
end 

I hope this is enough info please let me know if you need more. Thank You! I've also tried using rspec_wait but it still is sending an error. here it is

enter image description here

 <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>

Upvotes: 2

Views: 948

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49870

The currently accepted answer for this question is wrong. There is no need to use rspec-wait with Capybara matchers since they already have waiting/retrying behavior built in. You can adjust how long they wait/rety for using Capybara.default_max_wait_time, or passing a wait: <time in seconds> option to the matcher.

From reading the question and the attached image I assume the "Staff can see the response messages" test is passing but the "Staff can edit the response messages" test fails. If so it's most likely because on the edit page the "You have now subscribed for updates" string is actually the content of a field (textbox etc) rather than text content of the page. have_content is used for checking for text node content on the page, to test for a field with a given value you would use something like

expect(page).to have_field('Response', with: 'You have now subscribed for updates')

That assumes there is a label associated with the field with text of "Response", if not you can pass the id of the field, name, etc. or as of Capybara 2.7 you can pass nil and it will just look for any field with the given value - all depends exactly what you are needing to test for.

expect(page).to have_field(nil, with: 'You have now subscribed for updates')

Upvotes: 1

Related Questions