【问题标题】:Setting page/respone.body in Capybara used to work in Webrat在 Capybara 中设置 page/response.body 曾经在 Webrat 中工作
【发布时间】:2012-03-28 07:57:57
【问题描述】:

我正在迁移到水豚。 我遇到的问题之一是迁移 pdf 步骤。

此步骤将 page.body 设置为已解析的 pdf。 这样我就可以使用默认的黄瓜步骤了。

When 'I follow the PDF link "$label"' do |label|
  click_link(label)
  page.body = PDF::Inspector::Text.analyze(page.body).strings.join(" ")
end

例如

When I follow the PDF link "Catalogue"
Then I should see "Cheap products"

我得到的错误是这个:

undefined method `body=' for #<Capybara::`enter code here`Document> (NoMethodError)

【问题讨论】:

    标签: ruby-on-rails cucumber capybara webrat


    【解决方案1】:

    最重要的是,确保您像这样设置:js =&gt; true

    scenario 'do something', :js => true do
        str = PDF::Inspector::Text.analyze(page.body).strings.join(" ") # or whatever string you want
        # then just use javascript to edit or add the body
        page.evaluate_script("document.write(#{str});")
    end
    

    现在这取决于驱动程序,但这是一种解决方案...

    【讨论】:

    • 我知道这是我对 SO 的第一个答案,但我已经潜伏了很长时间。欢呼
    【解决方案2】:

    在 capybara 的源代码中没有定义 body 的 setter,所以你不能像这样在外部设置它。试试这个(未经测试):

    page.instance_variable_set(:@body, PDF::Inspector::Text.analyze(page.body).strings.join(" "))
    

    【讨论】:

    • 您的回答无效,但它引导我走向了正确的方向。我将'page'替换为'page.driver'并且它有效。谢谢!
    【解决方案3】:

    这对我有用:

    Then /^I should be served the document as a PDF$/ do
      page.response_headers['Content-Type'].should == "application/pdf"
      pdf = PDF::Inspector::Text.analyze(page.source).strings.join(" ")
      page.driver.response.instance_variable_set('@body', pdf)
    end
    
    Then /^I should see the document details$/ do
      page.should have_content("#{@document.customer.name}")
      page.should have_content("#{@document.resources.first.resource.name}")
      page.should have_content("Document opened at #{@document.created_at.strftime("%e-%b-%4Y %r")}")
    end
    

    请注意,我正在提供我的 PDF 内联

    pdf = DocumentPdf.new(@document)
    send_data pdf.render, :filename => "document_#{@document.created_at.strftime("%Y-%m-%d")}",
      :type => "application/pdf",
      :disposition => "inline"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 2015-01-25
      • 2011-08-26
      • 2011-01-15
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多