Created
June 13, 2016 09:16
-
-
Save artjomb/54f4ab35940170f7449a1cf274c63bec to your computer and use it in GitHub Desktop.
Testing the double click capability of PhantomJS
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 f = require('fs'); | |
var page = require('webpage').create(); | |
// http://phantomjs.org/api/webpage/handler/on-console-message.html | |
page.onConsoleMessage = function(msg, lineNum, sourceId) { | |
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")'); | |
}; | |
// http://phantomjs.org/api/webpage/handler/on-error.html | |
page.onError = function(msg, trace) { | |
var msgStack = ['ERROR: ' + msg]; | |
if (trace && trace.length) { | |
msgStack.push('TRACE:'); | |
trace.forEach(function(t) { | |
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : '')); | |
}); | |
} | |
console.error(msgStack.join('\n')); | |
}; | |
phantom.onError = function(msg, trace) { | |
var msgStack = ['PERROR: ' + msg]; | |
if (trace && trace.length) { | |
msgStack.push('TRACE:'); | |
trace.forEach(function(t) { | |
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : '')); | |
}); | |
} | |
console.error(msgStack.join('\n')); | |
}; | |
// http://phantomjs.org/api/webpage/handler/on-resource-error.html | |
page.onResourceError = function(resourceError) { | |
console.log('Unable to load resource (#' + resourceError.id + ' URL:' + resourceError.url + ')'); | |
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString); | |
}; | |
// http://phantomjs.org/api/webpage/handler/on-resource-timeout.html | |
page.onResourceTimeout = function(request) { | |
console.log('Response Timeout (#' + request.id + '): ' + JSON.stringify(request)); | |
}; | |
page.viewportSize = { | |
width: 1920, | |
height: 1200 | |
}; | |
var sel = 'a.page[ondblclick][onclick]'; | |
var type = 'dblclick'; | |
page.open('http://unixpapa.com/js/testmouse.html', function(status){ | |
setTimeout(function(){ | |
page.evaluate(function(sel, type){ | |
var el = document.querySelector(sel); | |
var ev = document.createEvent("MouseEvent"); | |
ev.initMouseEvent( | |
type, | |
true /* bubble */, true /* cancelable */, | |
window, null, | |
0, 0, 0, 0, /* coordinates */ | |
false, false, false, false, /* modifier keys */ | |
0 /*left*/, null | |
); | |
el.dispatchEvent(ev); | |
}, sel, type); | |
}, 0); | |
setTimeout(function(){ | |
page.render("test_1.png"); | |
}, 100); | |
setTimeout(function(){ | |
page.evaluate(function(sel, type){ | |
var el = document.querySelector(sel); | |
var e = document.createEvent('MouseEvents'); | |
e.initMouseEvent(type, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); | |
el.dispatchEvent(e); | |
}, sel, type); | |
}, 1000); | |
setTimeout(function(){ | |
page.render("test_2.png"); | |
}, 1100); | |
setTimeout(function(){ | |
var rect = page.evaluate(function(sel){ | |
return document.querySelector(sel).getBoundingClientRect(); | |
}, sel); | |
page.sendEvent(type === 'click' ? 'click' : 'doubleclick', rect.left + rect.width / 2, rect.top + rect.height / 2); | |
}, 2000); | |
setTimeout(function(){ | |
page.render("test_3.png"); | |
}, 2100); | |
setTimeout(function(){ | |
var rect = page.evaluate(function(sel){ | |
document.querySelector(sel).click(); | |
}, sel); | |
}, 3000); | |
setTimeout(function(){ | |
page.render("test_4.png"); | |
phantom.exit(); | |
}, 3100); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment