0

I can't for the life of me figure out how to fire a click event on a button in qunit. I'm using grunt-contrib-qunit which I believe uses phantomJS to run the tests. I've tried multiple solutions people have said relating to phantomJS (like this one: PhantomJS; click an element ) but none of them seem to work.

Has anyone been able to get a test to work with grunt-contrib-qunit in the command line after you simulate a click event on an element?

Community
  • 1
  • 1
Kyle Paulsen
  • 956
  • 1
  • 8
  • 22

1 Answers1

0

OK I fixed my problem. It turns out that PhantomJS implements touch events and my code was checking for the ontouchend event and binding to it if it was there. Wow I did not expect that! So I was able to solve my problem by making a function that looks like this:

function fireClickEvent(el) {
    var eventType = "ontouchend" in document.documentElement ? "touchstart" : "click";
    var clickEvent = document.createEvent('MouseEvent');
    clickEvent.initMouseEvent(eventType, true, true, window,
        0, 0, 0, 0, 0, false, false, false, false, 0, null);

    el.dispatchEvent(clickEvent);
}

I'll try to check my work a little more before I post a question on here.

Kyle Paulsen
  • 956
  • 1
  • 8
  • 22