简体   繁体   中英

How to rescrape a page in Capybara?

Here's my spec for a page that uses a button to fire some Javascript to expand the truncated description text.

     it 'gets the description and inventory', js: true do
      product = Product.create!(name: "Test Product", inventory: 0, description: "This is a test description with more text than should be there.")
      customer = Customer.create(:name => Faker::Name.name)
      invoice = Invoice.create
      order = Order.create(customer: customer, invoice: invoice)

      order.products << product
      visit products_path
      expect(page).to have_content(product.name, count: 1)
      expect(page).not_to have_content product.description
      click_button "More Info"
      expect(page).to have_content product.description
      expect(page).to have_content "Sold Out"
      product.inventory = 1
      product.save
      visit products_path
      click_button "More Info"
      expect(page).to have_content "Available"
    end

When I run my rails server and visit the page in the browser, the "More Info" button definitely functions correctly. However, when I run the spec, the button doesn't appear to work:

  1) Products products index gets the description and inventory
     Failure/Error: expect(page).to have_content product.description
       expected to find text "This is a test description with more text than should be there." in "Flatiron Widgets Store Products Test Product This is a test description ... More Info"
     # ./spec/features/product_feature_spec.rb:47:in `block (3 levels) in <top (required)>'

Now, I know it's entirely possible that there's some problem that is causing the button to actually not work, but I'm wondering if the problem is actually that we're checking against a page that hasn't been scraped since before the button was clicked.

Does anyone know if that is indeed the problem, and if so is there a way to "rescrape" the page? I'm not looking to reload or refresh the page because that would put the JS to its initial pre-button state.

Assuming this a feature test and you have required capybara/rspec as indicated in the Capybara README then Capybara should see the js metadata on the test, swap to the driver configured by Capybara.javascript_driver (defaults to :selenium - which would open Firefox) and there would be no need to reload/refresh/rescrape the page because Capybara never caches the page (every time you check for text it is against the live page in the browser).

If the above is all true and you're seeing this issue then you either have a bug on your page, or you're using an obsolete driver (Poltergeist, etc) for running your tests which doesn't support the JS you're actually using in your pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM