A ruby wrapper for phantomjs.
You have to have phantomjs
installed.
gem install phantomjs.rb
Use Phantomjs.run
to run the passed script file.
// my_runner.js
var arg1 = phantom.args[0];
var arg2 = phantom.args[1];
console.log(arg1 + ' ' + arg2);
Then in ruby:
Phantomjs.run('my_runner.js', 'hello', 'world')
#=> 'hello world'
You can also pass a javascript string as an argument and call
Phantomjs.inline
. This will create a temporary file for it
and run the script.
NOTE: Just don't forget to call phantom.exit
.
js = <<JS
console.log(phantom.args[0] + ' ' + phantom.args[1]);
phantom.exit();
JS
Phantomjs.inline(js, 'hello', 'world')
#=> 'hello world'
Well it works for that too! Just pass a block
to the method call and the
argument will be whatever your script puts out on stdout
.
js = <<JS
ctr = 0;
setInterval(function() {
console.log('ctr is: ' + ctr);
ctr++;
if (ctr == 3) {
phantom.exit();
}
}, 5000);
JS
Phantomjs.inline(js) do |line|
p line
end
#=> ctr is: 0
ctr is: 1
ctr is: 2
If you are using phantomjs in a non-typical installation path or are including the binary inside your application, you can configure phantomjs.rb to use a custom path.
The following example is a good starting point to follow.
osx = Rails.env.development? || Rails.env.test?
Phantomjs.configure do |config|
config.phantomjs_path = "#{Rails.root}/bin/phantomjs-#{osx ? 'osx' : 'x86'}"
end
bundle exec rake spec
MIT