如何在 Capybara 中重新抓取页面?

How to rescrape a page in Capybara?

这是我的页面规范,该页面使用按钮触发一些 Javascript 以展开 t运行 对应的 description 文本。

     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

当我 运行 我的 rails 服务器并访问浏览器中的页面时, "More Info" 按钮确实可以正常工作。但是,当我 运行 规范时,按钮似乎不起作用:

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

现在,我知道完全有可能是某些问题导致按钮实际上不起作用,但我想知道问题是否真的是我们正在检查 page在单击按钮之前就被抓取了。

有谁知道这是否确实是问题所在,如果是,有没有 "rescrape" 页面的方法?我不想重新加载或刷新页面,因为那样会使 JS 进入其初始的按钮前状态。

假设这是一个功能测试,并且您已经按照 Capybara README 中的指示要求 capybara/rspec,那么 Capybara 应该会在测试中看到 js 元数据,切换到由 [=12= 配置的驱动程序] (默认为 :selenium - 这将打开 Firefox)并且不需要 reload/refresh/rescrape 页面,因为 Capybara 从不缓存页面(每次你检查文本时它都是针对浏览器中的实时页面)。

如果以上都是正确的并且您看到了这个问题,那么您的页面上有错误,或者您使用的是过时的驱动程序(Poltergeist 等)运行 您的测试不支持您在页面中实际使用的 JS。