We have a requirement to load the input HTML string into browser kind of environment, run the html (which should internally run all the inline scripts i.e there are a few ajax calls) and then provide the rendered HTML as output.
Is this possible with node?
Is there any node module which we can use for this purpose.
Please help in this regard.
Sound like you looking for headless browser for NodeJS. see a list here http://github.com/dhamaniasad/HeadlessBrowsers
This is example from CasperJS
You can install as Node module
npm install -g casperjs
And
var casper = require('casper').create();
casper.start('http://www.google.fr/', function() {
this.echo(this.getHTML());
});
casper.run();
Run
casperjs app.js
For NODEJS runtime
Try Nightmare
Install
npm install nightmare
code
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: false });
nightmare.goto("http://www.google.com")
.evaluate(function(){
return document.body.outerHTML;
})
.end()
.then(function (result) {
console.log(result)
})
Run
nodejs app.js
Related
I know the title of the question looks very vague! But that's there's to it.
I installed nodejs on my production server, which had phantomjs working properly, then I installed nightmare via npm install nightmare, I can see it in node_modules, I tried the example listed by the developers on github:
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })
nightmare
.goto('http://yahoo.com')
.type('input[title="Search"]', 'github nightmare')
.click('#uh-search-button')
.wait('#main')
.evaluate(function () {
return document.querySelector('#main .searchCenterMiddle li a').href
})
.end()
.then(function (result) {
console.log(result)
})
Nothing happened, the script did not output anything, I simplified the script to a simple single goto, for a page on my server, the page was never called when I ran the script via node file.js
I have CentOS 6.7, phantomjs 1.1
I also tested it on a fresh CentOS 7 installation with latest version of phantomjs, same thing.
Am I missing some kind of prerequisite or something? How do I debug the issue since node script.js is not giving any output
UPDATE: Apparently the problem is, electron, which is used by nightmare 'instead of phantomjs' requires a graphical enviroment, which is why it fails to run in my enviroment.
New version of Nightmare requires electron, Not PhantomsJs. Make sure electron command is in your $PATH variable.
Install Electron
npm i -g electron-prebuilt
To debug:
DEBUG=nightmare* node script.js
Look at this Dockerfile: https://github.com/aheuermann/docker-electron/blob/master/7/Dockerfile
It s the minimal libs you need. And to start you script:
Xvfb -ac -screen scrn 1280x2000x24 :9.0 &
export DISPLAY=:9.0
DEBUG=* node src/index.js
Electron based app should no more crash
You can also try to set electron in the background without actually showing any GUI. You check if this works:
var nightmare = Nightmare({ show: false});
Note: I am using Mac OS 10.10 Yosemite
Important Note: None of the other questions and answers have worked for me.
I am following a tutorial which will have it so that I could have a multiplayer game. There is a file, which I have to download, which has a game.js file that I need to add this code into:
Note: I correctly downloaded socket.io in the correct directory.
var util = require("util"),
io = require("socket.io").listen(80);
var socket,
players;
function init() {
players = [];
socket = io.listen(8000);
socket.configure(function() {
socket.set("transports", ["websocket"]);
socket.set("log level", 2);
});
};
init();
But when I run node game.js, I get an error that looks like this:
Note: The Robhawks-mozzilla-festival-92336f2 folder is the folder that has all of the files in it
Why is the socket.configure messing up? Also, how can I fix it?
.configure() was removed when socket.io went to version 1.0. I'm guessing that the tutorial you're following is using an older version (0.9), but you have installed 1.0.
It would be best to move your code base to 1.0, as it's the latest. Configuration should be done as part of the server initialization:
var socket = require('socket.io')({
transports : [ 'websocket' ],
...
});
More info here.
However, since you're following a tutorial it may be easier to first get things up and running using the older version of socket.io, and once you have familiarized yourself with it, move to 1.0. In that case, install the older version:
$ npm install socket.io#0.9
Looging socket.io v1.0 log-level option is removed. Thus for logging one has to start program using debug module.
install debug: npm install debug -S
then run the program: DEBUG=* node entry_file.js
When running karma from a grunt task I get the following warning:
Running "karma:unit" (karma) task
Warning: The api interface has changed. Please use
server = new Server(config, [done])
server.start()
instead. Use --force to continue.
Aborted due to warnings.
I have tested running karma with my configuration, both using the 'run' and 'start' karma commands and they seem to work fine.
Using grunt --force can complete the task, but it completes with warnings.
This are the versions that I'm currently using:
Karma 0.13.0
Grunt 0.4.5
grunt-cli 0.1.13
node.js 0.12.7
npm 2.11.3
The project was generated using yeoman (1.4.7) but I have the same problem using Karma in a separate project with just jasmine, karma and Grunt (also tested it with Gulp).
I have searched for the warning message but found nothing. I don't know if this is the expected behavior or if there is another way of completing the tasks without warnings.
They changed with new version here:
https://github.com/karma-runner/karma/blob/master/CHANGELOG.md#breaking-changes
var Server = require('karma').Server;
var config = {
configFile: path.join(__dirname, '/../karma.conf.js'),
singleRun: singleRun,
autoWatch: !singleRun
};
var server = new Server(config, done)
server.start()
If you are using the grunt-karma plugin to start the Karma tests from Grunt, you need to update the grunt-karma dependency in your package.json file to 0.12.0:
"devDependencies": {
...
"grunt-karma": "~0.12.0",
...
}
Version 0.12.0 of grunt-karma was released earlier today, and it uses the new API: https://github.com/karma-runner/grunt-karma/releases
I am using Gulp and I had to either setup my test task as follows:
var Server = require('karma').Server;
gulp.task('test', function (done) {
new Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done).start();
});
or roll the karma version back to 0.10.0 to get it working. At this time there is not pull request on gulp-karma to change how this works, but not sure if there will be a change in the future.
Here is a helpful example that was updated after the Karma API change: https://github.com/karma-runner/gulp-karma
If you're using Yeoman's generator-angular like I was, you'll need to switch it out in your grunt-karma.js file.
Replace var server = require(‘karma’).server;
with var Server = require('karma').Server;
and then at the bottom of the file in the else block instead of server.start(config, done);
you'll find server.start(data, finished.bind(done));
just put var server = new Server(data, finished.bind(done)); right above it and it should work fine.
I've an issue with Node.js. With Python, if I wanted to execute an external command, I used to do something like this:
import subprocess
subprocess.call("bower init", shell=True)
I've read about child_process.exec and spawn in Node.js but I can't do what I want. And what's I want?
I want to execute an external command (like bower init) and see its output in real time and interact with bower itself. The only thing I can do is receive the final output but that don't allow me to interact with the program.
Regards
Edit: I saw this question but the answer doesn't work here. I want to send input when the external program needs it.
How about this?
var childProcess = require('child_process');
var child = childProcess.spawn('bower', ['init'], {
env: process.env,
stdio: 'inherit'
});
child.on('close', function(code) {
process.exit(code);
});
Seemed to work for me
I want to make continuous integration for my chrome extension.
Using tools: GitHub, Travis, Bower, components-jasmine.
In .travis.yml:
install bower
in bower download jasmine
change default SpecRunner.html on my SpecRunner.html, which contains my src and specs
load my SpecRunner.html in phantomjs
.travis.yml:
language: node_js
node_js:
- "0.10"
install:
- npm install -g bower
- bower install
- cd $TRAVIS_BUILD_DIR
script:
- mv -f ./test/SpecRunner.html ./vendor/components-jasmine
- phantomjs ./test/runTests.js
runTests.js:
var page = require('webpage').create();
page.open('../vendor/components-jasmine/SpecRunner.html', function(){
phantom.exit();
});
Tests should be failed, but status on travis - passed.
Why my tests don't run?
Results of tests is showing on the same SpecRunner.html.
For getting results need to print this loaded page.
If html loaded in phantomjs locally, then url should to follow classic Url/Uri rules, especially for a local file (that's why page not loaded):
file:///c:/path/to/the%20file.txt #win
file:///etc/fstab #unix
For getting results of tests need parse logs in after_script in .travis.yml and return value - 0 if tests passed or non-null if tests failed.
Also, probably, it's possible with help itself jasmine (but i'm not sure).
Try to add after_script: echo $? to .travis.yml to see what is the return value of your test. If it's 0 then Travis behavior is correct.
I merely guess that your test just return 0 regardless of a result. According to the documentation this code just tries to open the file and calls a callback on failure - since there is no not-0-exit callback whole scripts exits normally and build passes.
EDIT:
From what you wrote I understand that Travis runs just fine, and it's phantomjs ./test/runTests.js that isn't returning non 0 value. Try to change:
function(){
phantom.exit();
}
into something like:
function(status){
if (status === 'success')
phantom.exit(0);
else
phantom.exit(-1);
}
And say it that fail the build (I assume that page opening always fails).