apparition icon indicating copy to clipboard operation
apparition copied to clipboard

Capybara::Apparition::WrongWorld when waiting for a page to redirect

Open macfanatic opened this issue 4 years ago • 1 comments

First time using this instead of any other JS drivers, so thanks for the framework and exciting!

I'm trying to write a test for a Stripe checkout.js page (subscribing to a plan) and get intermittent failures. I'm trying to use capybara matchers that wait for the page to change before asserting that things happen. I haven't tried with a chromedriver based setup yet, but I imagine it will work.

For the script below, a sleep will cause capybara to wait and the assertions work. If I try to use the content changing on the page (content or css) then it fails intermittently. It appears the page object is stale while the redirect is happening?

Any thoughts or recommendations?

require 'rails_helper'

RSpec.feature 'Subscriptions' do
  let(:user) { create(:user) }

  background do
    login_user(user)
  end

  context 'as a new customer' do
    background do
      visit organization_path
    end

    scenario 'I can subscribe', js: true do
      click_link 'Subscribe Now'

      expect do
        within 'form.new_pay_subscription' do
          select 'Freelancer Plan', from: 'Subscription Plan'
          fill_in 'Name on Card', with: 'Johnny Appleseed'
          fill_stripe_elements(card: '4111 1111 1111 1111') # stripe elements for interacting with iframe contents

          click_link_or_button 'Create Subscription'
        end

        # ----HERE----
        #
        # If I simply sleep after the button is clicked, enough time is given for the
        # page to redirect and my assertions below pass.
        #
        # If I am a "good" capybara dev and wait for visual or DOM changes to indicate my
        # operation has completed instead, I get failures.  I have tried several with the same outcome.
        #

        # WORKS, not ideal obviously and brittle
        # sleep 2 

        # all of the following will fail and raise an exception about being in the wrong world
        # they are not consistent about failing with this error.  I'm assuming that the faster the operation
        # completes and the page moves one (without the sleep of course) that the failure occurrs. 

        # expect(page).to have_no_css('form.new_pay_subscription')
        # expect(page).to_not have_css('form.new_pay_subscription')
        # expect(page).to have_current_path(after_registration_path)

      end.to change { user.organization.subscriptions.count }.by(1)
    end
  end

macfanatic avatar Mar 27 '20 20:03 macfanatic

I feel like this is the same scenario, using different methods/interactions, as originally described in issue #12 and resolved in this SHA: 2537511db8abd6f679b1a774ac7051aa284c943e

macfanatic avatar Mar 27 '20 20:03 macfanatic