Last active
December 16, 2015 01:39
-
-
Save eoy/5356255 to your computer and use it in GitHub Desktop.
PhantomJS for TodoMVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PHANTOM_FUNCTION_PREFIX = '/* PHANTOM_FUNCTION */'; | |
var page = require('webpage').create(), | |
system = require('system'), | |
t, | |
address, | |
url = system.args[1], | |
t = Date.now(), | |
length; | |
page.onInitialized = function() { | |
page.evaluate(function(domContentLoadedMsg) { | |
document.addEventListener('DOMContentLoaded', function() { | |
window.callPhantom('--------------- START ---------------'); | |
}, false); | |
}); | |
}; | |
console.log('--------------- START ---------------'); | |
page.onCallback = function() { | |
console.log('DOMContentLoaded'); | |
page.onConsoleMessage = function(msg) { | |
if (msg.indexOf(PHANTOM_FUNCTION_PREFIX) === 0) { | |
eval('(' + msg + ')()'); | |
} else { | |
console.log(msg); | |
} | |
}; | |
// Fetch jQuery for easier selectors | |
page.includeJs("http://localhost:3000/assets/jquery.min.js", function() { | |
var t = Date.now(), | |
entries = 1000; // Change this to adjust the number of To-do items created. | |
// Print out the title of the page | |
var title = page.evaluate(function() { | |
return document.title | |
}); | |
console.log(title); | |
// Create 10000 todos | |
console.log("Creating todos..."); | |
for (var i=0; i<entries; i++) { | |
page.sendEvent('keypress', 'This is todo number: '+i+'\n'); | |
} | |
// page.render('resluts.png'); | |
// Print the number of todos to confirm | |
var length = page.evaluate(function() { | |
return $('#todo-list li').length | |
}); | |
if (entries !== length) { | |
console.log("* ERROR * Created an invalid number of entries!"); | |
} | |
console.log(length +" Created"); | |
// Mark the todos as complete | |
console.log("Marking todos as complete..."); | |
page.evaluate(function() { | |
$('#todo-list li').each(function(){ | |
$(this).find('input').click(); | |
}); | |
return true; | |
}); | |
// page.render('resluts2.png'); | |
// Destroy all todos | |
console.log("Destroying todos..."); | |
page.evaluate(function() { | |
$('.destroy').click(); | |
}); | |
// page.render('resluts3.png'); | |
// Print out total time | |
t = Date.now() - t; | |
console.log('Loading time ' + t + ' msec'); | |
console.log('---------------- END ----------------'); | |
phantom.exit(); | |
}); | |
}; | |
page.open(url, function(status) { | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment