Running phantomjs from a Ruby on Rails application
Use backticks in your ruby code e.g.:
output = `phantomjs rasterize.js http://raphaeljs.com/polar-clock.html clock.png`
Using PhantomJS in Ruby on Rails application
To initialize a capybara session in your app you can just do something like
session = Capybara::Session.new(:poltergeist)
( as documented here) and then rather than using page
just call Capybara methods on session
. One thing to note is that if you're going to test the app with Capybara too you will probably want to register a separate driver for the app and testing - https://github.com/jnicklas/capybara#configuring-and-adding-drivers . Also since Capybaras config is not thread-safe changing any of Capybaras setting would potentially affect both the test session and the in app session.
Deploying PhantomJS or headless gem on Engine Yard
I suggest the PhantomJS gem. It automatically installs PhantomJS the first time it's used, in a private location whose location you can get from an API. It provides a recent PhantomJS, currently 2.1.1. It works for me on both MacOS and Linux.
I haven't used that gem on Engine Yard, but I don't think there should be any Engine Yard-specific issues.
I use Cucumber, Capybara and poltergeist, so I have this in features/support/poltergeist.rb:
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, phantomjs: Phantomjs.path)
end
Using Selenium-Webdriver directly, I'd expect what rantingsonrails suggested would work:
Selenium::WebDriver::PhantomJS.path = Phantomjs.path
Rails/Poltergiest/PhantomJS not loading files in application.js during testing
I can think of two possible causes for this. The first would be a syntax error in a previous included JS file preventing the following files from being processed when they all get concatenated together. Since the file thats not working is the first file required it's probably not that. The second cause is that you have precompiled assets at some point, and have a manifest file in public/assets (probably something like public/assets/.sprockets-manifest-*.json) which can prevent the test environment from rebuilding and serving the latest version of the assets. Try deleting the file, or run rake assets:clobber
PhantomJS script hangs when executed from Rails controller on localhost
You have to use a multi-threaded web server like puma.
See here for an overview of different options.
Another pitfall might be your controller logic. If the page, that gets rendered by phantom, triggers the same controller action than the one, that calls phantom, you create an infinite loop.
Then, even a multi-threaded server would get stuck :)
this question is a duplicate of PhantomJS.rb freezing when trying to screen capture page in my rails application
Please close this one. I'm lacking reputation to do so or write this as a comment.
phantomjs: How to run phantomjs command when Url with queryparams
You need to protect all &
chars with a backslash :
phantomjs rasterize.js http://www.myurl.com?q=123\&s=foo form1.pdf
Ampersand is annoying in console so you need to protect it like above. In a ruby script you need to protect backslash that protect &
:
`phantomjs rasterize.js http://www.myurl.com?q=123\\&s=foo form1.pdf`
example :
`phantomjs rasterize.js https://www.google.fr/search?q=rasterize.js+special+char\\&oq=rasterize.js google1.pdf`
Related Topics
How to Disable a Form Submit Button "A Là Ruby on Rails Way"
Rspec Any_Instance Deprecation: How to Fix It
(Ruby) How to Check Whether a Range Contains a Subset of Another Range
How to Do Case-Insensitive Order in Rails with Postgresql
Why Does Date Exist in Ruby Before It Is Required
How to Get Name of the Month in Ruby on Rails
How to Test If All Items in an Array Are Identical
Error: While Executing Gem ... (Typeerror) Incompatible Marshal File Format (Can't Be Read)
Rails: Difference Between Env.Fetch() and Env[]
Split Array Up into N-Groups of M Size
Is the Unix Philosophy Falling Out of Favor in the Ruby Community
Rails: Url/Path with Parameters
How to Save a Timezone Correctly with Ruby and Mongoid