I am new to zombie and just trying to get a basic test running. I have the following code:
var Browser = require('zombie');
var startTime = +new Date();
Browser.visit("http://zombie.labnotes.org/", function(e, browser) {
var duration;
console.log("Successfully visted the page");
console.log(browser.html());
duration = (+(new Date())) - startTime;
console.log("Finished in (milliseconds): " + duration);
});
For some reason all I get back in the console is:
Successfully visted the page
<html>
<head></head>
<body></body>
</html>
Finished in (milliseconds): 5020
This is obviously not the right mark up and it takes quite a bit of time (5 seconds) to do that. Any ideas?
UPDATE: ended up switching to a simpler model using request and jsdom. Here is the code I used: var request = require('request'), jsdom = require('jsdom');
//Tell the request that we want to fetch youtube.com, send the results to a callback function
request({uri: 'http://youtube.com'}, function(err, response, body){
var self = this;
self.items = [];
//Just a basic error check
if(err && response.statusCode !== 200){console.log('Request error.');}
//Send the body param as the HTML code we will parse in jsdom
//also tell jsdom to attach jQuery in the scripts and loaded from jQuery.com
jsdom.env({
html: body,
scripts: ['http://code.jquery.com/jquery-1.6.min.js']
}, function(err, window){
//Use jQuery just as in a regular HTML page
var $ = window.jQuery;
console.log(body);
});
});
Taken from: http://net.tutsplus.com/tutorials/javascript-ajax/how-to-scrape-web-pages-with-node-js-and-jquery/
But I would still like to know what went wrong with Zombie as I would like to use it for testing on other projects.
Browser is the class that is loaded through the require. You want to create a variable that is an instance of Browser and then call visit using that variable. Your code should be:
var Browser = require('zombie');
var startTime = +new Date();
my_browser = new Browser(); // Here's where you need to call new
my_browser.visit("http://zombie.labnotes.org/", function(e, browser) {
var duration;
console.log("Successfully visted the page");
console.log(browser.html());
duration = (+(new Date())) - startTime;
console.log("Finished in (milliseconds): " + duration);
});
It seems to work now that I upgraded to a new version of both Node.js and Zombie.js. Note that you cannot use a pre version of Node with zombie.js (one of the dependencies will fail saying that).
Use NVM to install the latest stable version (version 0.8.9 at time of writing).