-
-
Save galliani/44fdb3fc1748e48b99f0caa100808d1e 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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group :test do | |
gem 'rspec-rails' | |
gem 'capybara' | |
gem 'pdf-reader' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PdfValidation::Application.routes.draw do | |
resources :pdfs, only: [:index] | |
root to: "pdfs#index" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1 Hi, PDF! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment