测试在Jasmine中传递但在grunt-contrib-jasmine中失败:document.getElementsByClassName

时间:2014-06-30 12:28:47

标签: javascript jasmine grunt-contrib-jasmine

当我通过webbrowser(safari或chrome)运行它时,此测试通过,但是通过grunt-contrib-jasmine失败。

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

奇怪的是document.getElementsByClassName('video')。length = 1,但是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();
    });
});

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我的症状错了。 它实际上是phantomjs中不存在的.click()函数。 Object.keys(document.getElementsByClassName(' video')[0])返回数据,即使console.log为空。

我使用

解决了这个问题
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);
}

在这里找到:  Triggering click event PhantomJS