Created
February 23, 2015 18:19
-
-
Save johntitus/8bb14c69f89e5241057f to your computer and use it in GitHub Desktop.
Second page.evaluate is slow
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
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