在 Capybara 中设置 page/respone.body 曾经在 Webrat 中工作

发布于 2024-10-20 14:08:16 字数 507 浏览 1 评论 0原文

我要迁移到水豚。 我遇到的问题之一是迁移 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)

I am migrating to Capybara.
One of the problems I have is migrating the pdf step.

This step sets page.body to a parsed pdf.
That way I can use the default cucumber steps.

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

Ex.

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

The error I get is this one:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花随流水 2024-10-27 14:08:16

最重要的是,确保设置 :js =>; 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

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

On top, make sure you set :js => true like this:

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

Now this is dependent on the driver, but it's one solution...

寄与心 2024-10-27 14:08:16

水豚的源代码中没有定义 body 的设置器,因此您不能像这样在外部设置它。试试这个(未经测试):

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

There is no setter for body defined in the source in capybara, so you cannot set it externally like that. Try this (untested):

page.instance_variable_set(:@body, PDF::Inspector::Text.analyze(page.body).strings.join(" "))
塔塔猫 2024-10-27 14:08:16

这对我有用:

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"

This worked for me:

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

Note that I'm serving my PDF inline

pdf = DocumentPdf.new(@document)
send_data pdf.render, :filename => "document_#{@document.created_at.strftime("%Y-%m-%d")}",
  :type => "application/pdf",
  :disposition => "inline"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文