开发者

Setting page/respone.body in Capybara used to work in Webrat

开发者 https://www.devze.com 2023-02-15 06:52 出处:网络
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.

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 her开发者_如何学编程e`Document> (NoMethodError)


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...


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(" "))


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"
0

精彩评论

暂无评论...
验证码 换一张
取 消