1

当我通过网络浏览器(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();
    });
});

任何帮助表示赞赏!

4

1 回答 1

0

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

我通过使用解决了这个问题

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);
}

在这里找到: 触发点击事件 PhantomJS

于 2014-07-01T12:11:35.303 回答