javascript - 如何在 Capybara 中重新抓取页面?
问题描述
description
这是我对使用按钮触发一些 Javascript 以扩展截断文本的页面的规范。
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 服务器并访问浏览器中的页面时,“更多信息”按钮肯定可以正常工作。但是,当我运行规范时,该按钮似乎不起作用:
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
自单击按钮之前就没有被刮掉的.
有谁知道这是否确实是问题所在,如果是,有没有办法“重新刮擦”页面?我不打算重新加载或刷新页面,因为这会使 JS 进入其初始按钮前状态。
解决方案
假设这是一个功能测试,并且您已按照capybara/rspec
Capybara README 中的说明要求,那么 Capybara 应该会看到js
测试中的元数据,切换到由配置的驱动程序Capybara.javascript_driver
(默认为 :selenium - 这将打开 Firefox)并且不需要重新加载/refresh/rescrape 页面,因为 Capybara 从不缓存页面(每次检查文本时,它都是针对浏览器中的实时页面)。
如果以上都是真的并且您看到了这个问题,那么您的页面上可能存在错误,或者您正在使用过时的驱动程序(Poltergeist 等)来运行不支持您的 JS 的测试实际在您的页面中使用。
推荐阅读
- git - 为主应用创建文档侧 webapp
- colors - 使用一种颜色绘制存储在缓冲区中的三角形opengl es 2.0
- amazon-web-services - 使用条件表达式更新 dynamoDB 列
- php - 如何显示“用户名”表的值?
- python-3.x - 从不同的文件夹中检索图像
- opayo - 3D Secure - test.sagepay.com 返回一个空白页
- javascript - 如何修复 ReactTable 的“TypeError:resolveData(...).map 不是函数”
- log4j2 - 使用 slf4j 绑定自定义 log4j2 包装器
- java - 如何以编程方式获取 jmx 正在监听的端口?
- sql - 两个表 - 从其中一个表中为第二个表中的每一行选择 TOP