public
Last active

How To Test PDFs with Capybara Blog post with details: http://pivotallabs.com/test-pdfs-with-capybara/

  • Download Gist
Gemfile
Ruby
1 2 3 4 5
group :test do
gem 'rspec-rails'
gem 'capybara'
gem 'pdf-reader'
end
pdf_viewing_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
require 'spec_helper'
 
feature 'Viewing a PDF' do
scenario 'User requests the PDF page' do
visit "/pdfs.pdf"
convert_pdf_to_page
page.should have_content("Hi, PDF!") # Passes
end
end
 
def convert_pdf_to_page
temp_pdf = Tempfile.new('pdf')
temp_pdf << page.source.force_encoding('UTF-8')
reader = PDF::Reader.new(temp_pdf)
pdf_text = reader.pages.map(&:text)
temp_pdf.close
page.driver.response.instance_variable_set('@body', pdf_text)
end
pdfs_controller.rb
Ruby
1 2 3 4 5 6 7 8 9 10
class PdfsController < ApplicationController
def index
respond_to do |format|
format.pdf do
render :pdf => 'filename.pdf',
:show_as_html => params[:debug]
end
end
end
end
routes.rb
Ruby
1 2 3 4
PdfValidation::Application.routes.draw do
resources :pdfs, only: [:index]
root to: "pdfs#index"
end
show.pdf.haml
Haml
1
%h1 Hi, PDF!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.