Ilovebathroomlights
Ilovebathroomlights

Reputation: 345

How to properly evaluate if a page has certain content with capybara?

I am currently trying to evaluate the result of clicking the "Pay" button on my site with Capybara if the form has invalid params. It seems like the test is doing everything correctly, filling out all fields correctly, clicking the "Pay" button, but then it is not "seeing" my .alert selector with the text "Email can't be blank, Email is invalid". I am not sure why it is not "seeing" this. Its as if its not even there. My controller is setting flash and then rendering the page with the flash method. Is Capybara trying to evaluate if the current page has the flash? The code for my current test is below. Any help is appreciated. Ive been going through the documentation but must be missing something about expect().

feature "order features", type: :feature do
    feature "making a new order" do
        before(:each) do
            visit "/orders/new"
        end

        scenario "with invalid params", js: true do
            fill_in "Your Name *", with: "Benedict Cumberbatch" 
            select("CAM Academy", from: 'order_school_name')
            fill_in "Street Address *", with: "221b Baker St."
            fill_in "City", with: "Vancouver"
            select("WA", from: 'order_state')
            fill_in "Zip code *", with: "98671"
            fill_in "groupNumberBooks", with: 1
            fill_stripe_elements(card: '4242 4242 4242 4242', selector: "#card-element-1 > div > iframe")
            click_button "Pay"
            expect(page).to have_selector('.alert', text: "Email can't be blank, Email is invalid")
        end
    end
end

On a side note, I have also tried expect(page).to have_content("Email can't be blank"). This also does not work. What am I missing here?

edit: the exact error message I am getting is:

Failures:

  1) order features making a new order with invalid params
     Failure/Error: expect(page).to have_selector('alert', text: "Email can't be blank, Email is invalid")
       expected to find css "alert" but there were no matches
     # ./spec/features/order_features_spec.rb:19:in `block (3 levels) in <top (required)>'

I have gone to the specified path and have manually filled out these fields and have evaluated what is being presented on the page when the render is called and the flash[:alert] is set.

Upvotes: 0

Views: 666

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49950

It sounds like your test is probably hitting the real stripe (sandbox instance) which means the failure is going to take longer than most of your tests. This means that Capybara.default_max_wait_time isn't long enough for the page to actually update to show the error message. You could increase Capybara.default_max_wait_time or you can tell individual expectations to allow longer wait times for a match using the wait parameter

expect(page).to have_selector('.alert', text: "Email can't be blank, Email is invalid", wait: 20)

which will wait up to 20 seconds for the expected element to appear on the page

Upvotes: 1

Related Questions