Skip to content

Instantly share code, notes, and snippets.

@johntitus
Created February 23, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johntitus/8bb14c69f89e5241057f to your computer and use it in GitHub Desktop.
Save johntitus/8bb14c69f89e5241057f to your computer and use it in GitHub Desktop.
Second page.evaluate is slow
var phantom = require('phantom');
var debug = require('debug')('phantom');
var p;
var click = function( selector, callback ){
var self = this;
p.evaluate( function( selector ) {
var element = document.querySelector(selector);
var event = document.createEvent('MouseEvent');
event.initEvent('click', true, true);
element.dispatchEvent(event);
}, callback, selector );
};
var wait = function( time, callback ){
setTimeout( callback, time );
};
var type = function( selector, text, callback ){
debug('starting evaluate');
p.evaluate( function( selector ){
document.querySelector( selector ).focus();
return true;
}, function(){
debug('back from evaluate')
for (var i = 0, len = text.length; i < len; i++){
debug('sending letter ' + text[i]);
p.sendEvent( 'keypress', text[i], null, null, 0 );
debug('done with letter ' + text[i]);
}
callback();
}, selector);
}
phantom.create(function (ph) {
ph.createPage(function (page) {
p = page;
page.open('https://github.com', function (status) {
console.log("opened github? ", status);
click('a[href="/login"]', function(){
wait( 2000, function(){
type( 'input[name=login]', 'myusername', function(){
type( 'input[name=password]', 'mypassword', function(){
debug('rendering screenshot');
page.render('out.png');
debug('done rendering');
ph.exit();
});
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment