0
votes

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.

1
Have you taken a screenshot or looked at the page source to see exactly what is shown on the page? Add the exact error message you're getting. - Thomas Walpole
I edited the original post to show this information. - Ilovebathroomlights
I have tried reformatting the test so it is evaluating the path, this is also not working. It seems like when I am using expect(page) it is looking at the current page, not the page my controller is redirecting the successful or failed action to. I think this is the issue I am having? - Ilovebathroomlights
For example, if the form is valid, I am redirecting to "/pages/thank_you", however, using expect(page).to have_current_path("pages/thank_you") fails because the test is looking at page, which still has the path "/orders/new". - Ilovebathroomlights
Assuming you have Capybara.default_max_wait_time set long enough for the new page to load it will be evaluating all the pages for that amount of time. The error message shows that you're looking 'alert' not '.alert' - Are you looking for an element <alert> or an element with the class of 'alert' (<div class="alert">,,,) - Thomas Walpole

1 Answers

1
votes

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