簡體   English   中英

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

[英]How to rescrape a page in Capybara?

這是我對使用按鈕觸發一些 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

當我運行 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)和無需重新加載/刷新/重新抓取頁面,因為 Capybara 從不緩存頁面(每次您檢查文本時,它都會針對瀏覽器中的實時頁面)。

如果以上都是正確的並且您看到此問題,那么您的頁面上有錯誤,或者您正在使用過時的驅動程序(Poltergeist 等)來運行不支持您使用的 JS 的測試實際在您的頁面中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM