Search code examples
javascriptjasminegrunt-contrib-jasmine

Test passing in Jasmine but failing in grunt-contrib-jasmine: document.getElementsByClassName


This test passes when I run it via a webbrowser (safari or chrome) but fails via grunt-contrib-jasmine.

 TypeError: 'undefined' is not a function (evaluating 'document.getElementsByClassName('video')[0].click()' 

strangely document.getElementsByClassName('video').length = 1, but document.getElementsByClassName('video')[0] === undefined

describe('ringing incomming', function (){
    var ringing_url = "ringing_payload"
    var webRtcUi = false;
    var accept = false;
    var hangup = false
    beforeEach(function (){
        accept = jasmine.createSpy('accept');
        hangup = jasmine.createSpy('hangup');
        webRtcUi = new WeemoWebRtcUi.WebRtcUi({dn: "wassupHomeBoy", accept: accept, hangup: hangup }, {legacy: false, uiRingToneUrl: ringing_url}, false);
        webRtcUi.ringing();
    });
    afterEach(function (){
        document.body.removeChild(document.getElementsByClassName('incoming-call')[0]);
    });

    it('on accept clicked callobject accept called', function (){
        document.getElementsByClassName('video')[0].click();
        expect(accept).toHaveBeenCalledWith();
    });
});

Any help is appreciated!


Solution

  • I got the symptoms wrong. It was actually the .click() function that does not exist in phantomjs. Object.keys(document.getElementsByClassName('video')[0]) returned data even though console.log was blank.

    I solved the problem by using

    function click(el){
        var ev = document.createEvent("MouseEvent");
        ev.initMouseEvent(
        "click",
            true /* bubble */, true /* cancelable */,
            window, null,
            0, 0, 0, 0, /* coordinates */
            false, false, false, false, /* modifier keys */
            0 /*left*/, null
        );
        el.dispatchEvent(ev);
    }
    

    found here: Triggering click event PhantomJS