Permalink
Browse files

Add some test case

  • Loading branch information...
1 parent 0433329 commit 1d235098156a39f6d4b9f04c59bf28bc5ae40185 @superbrothers committed Dec 23, 2012
View
1 .gitignore
@@ -4,3 +4,4 @@ node_modules
*.gif
*.jpg
*.tgz
+test/cookies-file.txt
View
BIN test/expected/external_script.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN test/expected/viewportsize.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1 test/external_script
@@ -1 +1,2 @@
document.getElementById('wrapper1').style.backgroundColor = 'yellow';
+document.getElementById('wrapper1').style.width = '100%';
View
77 test/test_capturejs.js
@@ -9,14 +9,15 @@ var fs = require('fs'),
async = require('async'),
capturejs = require(path.join(__dirname, '..', 'lib', 'capturejs.js')),
server,
+ request,
PORT = 9999,
ROOT_URI = 'http://' + require('os').hostname() + ':' + PORT;
function md5sum(path, callback) {
var md5 = crypto.createHash('md5');
fs.readFile(path, function (err, data) {
if (err) {
- return callback(err, null);
+ return callback(null, err);
}
callback(null, md5.update(data).digest('hex'));
});
@@ -33,11 +34,19 @@ function expectedPath(fileName) {
module.exports = {
'setUp': function (callback) {
server = http.createServer(function (req, res) {
- fs.readFile(path.join(__dirname, 'test.html'), 'utf8', function (err, data) {
- res.writeHead('200', {'Content-Type': 'text/html'});
- res.write(data);
- res.end();
- });
+ request = req;
+ var u = url.parse(req.url, true);
+ setTimeout(function () {
+ fs.readFile(path.join(__dirname, 'test.html'), 'utf8', function (err, data) {
+ res.writeHead('200', {
+ 'Content-Type': 'text/html',
+ 'Set-Cookie': 'cookie=test; path=/;'
+
+ });
+ res.write(data);
+ res.end();
+ });
+ }, (u.query.responsetime ? u.query.responsetime : 0));
}).listen(PORT);
callback();
},
@@ -60,7 +69,7 @@ module.exports = {
}, 100);
});
},
- 'use selector': function (test) {
+ 'selector': function (test) {
var expected = expectedPath('selector.png'),
actual = actualPath(Date.now() + '.png');
capturejs.capture({
@@ -76,7 +85,7 @@ module.exports = {
}, 100);
});
},
- 'use external script': function (test) {
+ 'external script': function (test) {
var expected = expectedPath('external_script.png'),
actual = actualPath(Date.now() + '.png');
capturejs.capture({
@@ -91,5 +100,57 @@ module.exports = {
});
}, 100);
});
+ },
+ 'user-agent': function (test) {
+ var useragent = 'This is user-agent test';
+ capturejs.capture({
+ 'uri': ROOT_URI,
+ 'output': actualPath(Date.now() + '.png'),
+ 'user-agent': useragent
+ }, function () {
+ test.equal(useragent, request.headers['user-agent']);
+ test.done();
+ });
+ },
+ 'cookies-file': function (test) {
+ var cookiesFile = path.join(__dirname, 'cookies-file.txt');
+ fs.unlinkSync(cookiesFile);
+ capturejs.capture({
+ 'uri': ROOT_URI,
+ 'output': actualPath(Date.now() + '.png'),
+ 'cookies-file': cookiesFile
+ }, function () {
+ fs.exists(cookiesFile, function (exists) {
+ test.ok(exists);
+ test.done();
+ });
+ });
+ },
+ 'timeout': function (test) {
+ capturejs.capture({
+ 'uri': ROOT_URI + '/?responsetime=2000',
+ 'output': actualPath(Date.now() + '.png'),
+ 'timeout': 500
+ }, function (err) {
+ test.ok(!!err);
+ test.done();
+ });
+ },
+ 'viewportsize': function (test) {
+ var expected = expectedPath('viewportsize.png'),
+ actual = actualPath(Date.now() + '.png');
+ capturejs.capture({
+ 'uri': ROOT_URI,
+ 'output': actual,
+ 'javascript-file': path.join(__dirname, 'external_script'),
+ 'viewportsize': '1000x400'
+ }, function () {
+ setTimeout(function () {
+ async.map([expected, actual], md5sum, function (err, results) {
+ test.equal(results[0], results[1], expected + ' eq ' + actual);
+ test.done();
+ });
+ }, 100);
+ });
}
};

0 comments on commit 1d23509

Please sign in to comment.