简体   繁体   English

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

[英]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.这是我对使用按钮触发一些 Javascript 以展开截断的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

When I run my rails server and visit the page in the browser, the "More Info" button definitely functions correctly.当我运行 Rails 服务器并访问浏览器中的页面时,“更多信息”按钮肯定可以正常工作。 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.现在,我知道完全有可能是有一些问题导致按钮实际上不起作用,但我想知道问题是否真的是因为我们正在检查一个自按钮之前就没有被刮过的page点击。

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.我不打算重新加载或刷新页面,因为这会将 JS 置于其初始按钮前状态。

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).假设这是一个功能测试,并且您需要capybara/rspec如 Capybara README 中所示,那么 Capybara 应该看到测试中的js元数据,切换到Capybara.javascript_driver配置的驱动程序(默认为 :selenium - 这将打开 Firefox)和无需重新加载/刷新/重新抓取页面,因为 Capybara 从不缓存页面(每次您检查文本时,它都会针对浏览器中的实时页面)。

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.如果以上都是正确的并且您看到此问题,那么您的页面上有错误,或者您正在使用过时的驱动程序(Poltergeist 等)来运行不支持您使用的 JS 的测试实际在您的页面中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用水豚获取动态页面内容 - How to get dynamic page content with capybara 如何在Capybara / Poltergeist的页面上运行一个函数? - How to run a function on a page with Capybara/Poltergeist? 如何在Rails / Capybara中测试页面重新加载? - How to test a page reloading in Rails / Capybara? 如何使用水豚在页面上测试自动定期更新? - How to test auto periodic updates on the page using capybara? 如何在 Ruby 中使用 selenium-webdriver/capybara 截取完整浏览器页面及其元素的屏幕截图? - How to take a screenshot of a full browser page and its elements using selenium-webdriver/capybara in Ruby? 如何在Capybara中测试页面是否*没有重新加载(JavaScript onClick intercept有效)? - How can I test in Capybara that a page has *not* reloaded (JavaScript onClick intercept has worked)? 当页面上出现JavaScript错误时,如何使用Capybara-Webkit在功能测试中失败? - How do I fail a step in a feature test with Capybara-Webkit when there is a JavaScript error on a page? Capybara / rspec在页面上找到角度链接的问题 - Problems with capybara/rspec finding an angular link on the page Capybara JS测试page.all失败 - Capybara JS test fails for page.all 如何在Capybara和Selenium中打开浏览器 - How to open a browser in Capybara and Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM