module.exports = function (opt) { 'use strict'; opt = opt || {}; opt.scale = opt.scale || 1; opt.format = opt.format || 'png'; var phantomProcess = phridge.spawn(); return through.obj(function (file, enc, cb) { var that = this; // Do nothing if no contents if (file.isNull()) { return cb(); } if (file.isBuffer()) { rasterize(phantomProcess, file.contents.toString(), opt.format, opt.scale, function (err, data) { if (err) { that.emit('error', new PluginError(PLUGIN_NAME, err)); } file.contents = data; that.push(file); return cb(); }); } }).on('end', function () { phantomProcess .then(phridge.disposeAll) .catch(function (err) { phridge.disposeAll(); throw err; }); }); };
function run_phantom_js(test_serialized, next) { var rootpath = path.resolve(__dirname, '../../lib'); var projectbasepath = path.resolve(__dirname + '/../projects'); var projectjsonfile = projectbasepath + '/' + test_serialized.id + ".json"; if (!fs.existsSync(projectjsonfile)) { var projectfetcher = require('../../lib/projectfetcher.js'); log.info("Resources for project #" + test_serialized.id + " not available."); log.info("Start project fetcher to retrieve data of project #" + test_serialized.id); projectfetcher.fetchProject(test_serialized.id, projectbasepath); } phridge .spawn() .then(function (phantom) { return phantom.createPage(); }) .then(function (page) { return page.run(rootpath, 'file://' + projectbasepath + '/', test_serialized, run_phridge); }) .finally(phridge.disposeAll) .done( function () { next(); }, function (err) { throw err; } ); }
function spawnPhantomAndRunFn(promiseReturningFn) { return phridge.spawn() .then(function(phantom) { return promiseReturningFn(phantom).finally(function() { phantom.dispose().then(function () { logger.debug('Phantom process disposed'); }); }); }); }
Server.prototype.spawnPhantomProcess = function(count) { this.logger.info('Spawn PhantomJS process'); return phridge.spawn().catch(function(error) { if(count > 0) { this.logger.info('Error spawning phantom process - retry - error: ' + error); return Q.delay(1000).then(this.spawnPhantomProcess.bind(this, count - 1)); } else { this.logger.error('Phantom spawn error: ' + error); process.exit(); } }.bind(this)); };
server.createPhantom = function() { var _this = this; var args = {'--load-images': false, '--ignore-ssl-errors': true, '--ssl-protocol': 'tlsv1.2'}; if(this.options.phantomArguments && !_.isEmpty(this.options.phantomArguments)) { args = _.clone(this.options.phantomArguments); } util.log('starting phantom...'); if(this.options.onStdout) { phridge.config.stdout = this.options.onStdout; } if(this.options.onStderr) { phridge.config.stderr = this.options.onStderr; } phridge.spawn(args).then(_.bind(_this.onPhantomCreate, _this)); };
// npm var phridge = require('phridge'), express = require('express'); // local var phantom = require('./app/phantom'), settings = require('./app/settings'); // OpenShift ready port/ip var port = process.env.OPENSHIFT_NODEJS_PORT || settings.port, ip = process.env.OPENSHIFT_NODEJS_IP; // set up express, routes, middleware var app = express(); require('./app/routes.js')(app, express); // spawn some phantom! phridge.spawn().then(function (process) { phantom.process = process; app.listen(port, ip); });
'use strict'; var Promise = require('es6-promise').Promise; var phridge = require('phridge'); var path = require("path"); phridge.spawn({ loadImages: false }).then(function(phantom) { var page = phantom.createPage(); var jquery = path.resolve(__dirname, "./jquery.js"); console.log(jquery); return page.run(jquery, function(jquery, resolve, reject) { var page = this; // onInitialized is called after the web page is created but before // a URL is loaded according to the docs of PhantomJS // @see http://phantomjs.org/api/webpage/handler/on-initialized.html page.onInitialized = function() { page.injectJs(jquery); }; console.log(jquery) page.open("http://en.wikipedia.org/wiki/The_Book_of_Mozilla", function(status) { var hasJQuery; var title;
'use strict'; const ControllerFactory = require('boar-server').lib.controllerFactory; const phridge = require('phridge'); let phantom = null; phridge.spawn().then(function (_phantom) { phantom = _phantom; console.log('Phantom is working'); }); module.exports = ControllerFactory.create(function(router) { router.post('/api/render', function* () { try { var result = yield phantom.run(this.request.body.render, function(content) { var page = webpage.create(); page.viewportSize = { width: 800, height: 400 }; page.content = content; return page.renderBase64('PNG'); }); } catch(e) { console.log('Error', e); }
return new promise(function (resolve) { resolve(phridge.spawn({ ignoreSslErrors: 'yes', sslProtocol: 'any' })); }).then(function (ph) {
grunt.registerMultiTask('funcunit', 'Run QUnit unit tests in a headless PhantomJS instance.', function () { // Merge task-specific and/or target-specific options with these defaults. var options = this.options({ // Default PhantomJS timeout. timeout: 60000, // Explicit non-file URLs to test. urls: [], force: false, // Connect phantomjs console output to grunt output console: true, // Do not use an HTTP base by default httpBase: false, // output as tap tap: !!this.options("tap"), // save code coverage coverage: !!this.options("coverage") }); var urls; if (options.httpBase) { //If URLs are explicitly referenced, use them still urls = options.urls; // Then create URLs for the src files this.filesSrc.forEach(function (testFile) { urls.push(options.httpBase + '/' + testFile); }); } else { // Combine any specified URLs with src files. urls = options.urls.concat(this.filesSrc); } // This task is asynchronous. var done = this.async(); // if testcase fail this will set to true var fail = false; // quit all child process process.on('exit', function () { phridge.disposeAll(); }); phridge.spawn({ webSecurity: false }).then(function (phantom) { // Process each filepath in-order. grunt.util.async.forEachLimit(urls, 1, function (url, next) { var page = phantom.createPage(); // phantom is now a reference to a specific PhantomJS process grunt.log.writeln("Open page: " + url); page.run(url, options.timeout, function (url, timeout, resolve) { // this code runs inside PhantomJS var checkInterval = null, checkTimeout = null, page = this; page.open(url, function (status) { if (status !== "success") { reject(new Error('Open on: ' + url)); return; } // check if test is donec function checkTestIsDone() { return page.evaluate(function () { return typeof QUnit !== "undefined" && QUnit.jigMagga && QUnit.jigMagga.done; }); } function getTestResult() { return page.evaluate(function () { if (typeof QUnit !== "undefined" && QUnit.jigMagga && QUnit.jigMagga.done) { return QUnit.jigMagga.eventQueue; } }); } // check interval to check test done checkInterval = setInterval(function () { if (checkTestIsDone()) { clearTimeout(checkTimeout); clearInterval(checkInterval); resolve(getTestResult()); } }, 1000); // set timeout when test fails checkTimeout = setTimeout(function () { clearInterval(checkInterval); reject(new Error('Timeout on: ' + url)); }, timeout); }); }).then(function (result) { // inside node again var doneResult = JSON.parse(result[result.length - 2])[1]; if (typeof doneResult.failed !== "undefined" && doneResult.failed == 0) { grunt.log.ok("Test Done: " + url); } else { fail = true; grunt.log.error(url); grunt.log.error(JSON.stringify(result)); } page.dispose().then(function () { next(); }); }).catch(function (err) { grunt.log.error(url); grunt.log.error(new Error(JSON.stringify(err))); page.dispose().then(function () { next(); }); }); }, // All tests have been run. function () { phridge.disposeAll().then(function () { // All done! if (fail) { done(false); } else { done(); } }); }); }).catch(function (err) { grunt.log.error(url); grunt.log.error(err); phridge.disposeAll().then(function () { // All done! done(); }); }); });