PhantomJS 1.9 and KeyboardEvent

February 09, 2016

PhantomJS is awesome and one common use case is to use it as a headless browser for running a test suite. I noticed that I was getting different results in my tests where code was relying on fabricating a KeyboardEvent and dispatching it on an element. Well it looks like others have noticed that some of their events are missing, too. One proposed solution controls the type of event that is dispatched, but in all other cases I am pretty happy to use new KeyboardEvent() , I would prefer not to write special code just to appease my tests.

As a workaround I did this:

// in test (ie: test setup)
if (window._phantom) {
window.KeyboardEvent = function(eventString) {
var keyboardEvent = document.createEvent('KeyboardEvent');
keyboardEvent.initKeyboardEvent(eventString, true, true, window, 1, 0, 0);
return keyboardEvent;
// in code, single implementation for phantom or browser
var event = window.KeyboardEvent('keypress');

This could be pretty dangerous depending on your use case, but at least it’s isolated to your test. I wasn’t sure what other method to use, but if you have one I would love to hear it in the comments. Also, Phantom 2.x should fix this, but it wasn’t an option in this case.