Created
April 11, 2013 12:32
-
-
Save eoy/5363019 to your computer and use it in GitHub Desktop.
PhantomJS for TodoMVC Test #2
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() { | |
// your code here | |
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(); | |
// 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<1000; i++) { | |
page.sendEvent('keypress', 'This is todo number: '+i+'\n'); | |
page.evaluate(function() { | |
$('#todo-list li').each(function(){ | |
$(this).find('input').click(); | |
}); | |
return true; | |
}); | |
page.evaluate(function() { | |
$('.destroy').click(); | |
}); | |
} | |
// Print the number of todos to confirm | |
var length = page.evaluate(function() { | |
return $('#todo-list li').length | |
}); | |
console.log(length +" To-Dos remain"); | |
// 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