Skip to content

Instantly share code, notes, and snippets.

@joemasilotti
Last active October 25, 2022 08:40
Show Gist options
  • Save joemasilotti/6045144 to your computer and use it in GitHub Desktop.
Save joemasilotti/6045144 to your computer and use it in GitHub Desktop.
How To Test PDFs with Capybara Blog post with details: http://pivotallabs.com/test-pdfs-with-capybara/
group :test do
gem 'rspec-rails'
gem 'capybara'
gem 'pdf-reader'
end
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
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
PdfValidation::Application.routes.draw do
resources :pdfs, only: [:index]
root to: "pdfs#index"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment