2011-03-08 42 views
1

我正在遷移到水豚。 我遇到的其中一個問題就是遷移pdf步驟。水豚的設置頁/ respone.body曾經在Webrat工作

此步驟將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 

Ex。

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

我得到的錯誤是這個:

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

回答

1

有一種在有水豚源定義的身體沒有setter,所以你不能把它從外部類的。試試這個(未經測試):

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

你的答案沒有奏效,但它讓我走向了正確的方向。我將'page'替換爲'page.driver',並且工作正常。謝謝 ! – 2011-03-09 13:18:55

0

這爲我工作:

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

在頂部,請確保您設置: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 

現在這是依賴於驅動程序,但它是一種解決方案...

+1

我知道這是我的第一個答案,但我一直潛伏了很長時間。乾杯 – bentael 2012-03-27 20:38:26