在CasperJS中,我可以关闭事件处理程序=>发送单击事件而不调整大小事件

时间:2016-07-18 19:21:30

标签: javascript jquery casperjs

我正在测试一个页面,它有一个事件处理程序,可以在resize上重新加载页面。 它干扰了测试,我想把它关闭。

在页面上,处理程序是:

        $(window).bind('resize', function(e) {
            if (window.RT) clearTimeout(window.RT);
            window.RT = setTimeout(function() {
                this.location.reload(false); /* false to get page from cache */
            }, 100);
        });

所以在CasperJS中,我尝试过这样做:

        this.evaluate(function() {
            $(window).unbind('resize');
        });

虽然没有成功。我宁愿把它从CasperJS中删除而不是更改页面。

[更新]虽然我无法取消绑定resize事件处理程序,但我设法从CasperJS / PhantomJS发送了一个单击而没有触发resize事件。如果这对您有帮助,请参考以下代码:

// This is a workaround alternative to this.click(link)
// It does not trigger a resize event. (1 test)
function myclick( test, casper, link ) {
    test.assertExists(link);
    test.comment('myclick: send click event to ' + link);
    casper.evaluate(function(link) {
        var evt = document.createEvent('MouseEvents');
        evt.initEvent('click', true, false);
        document.querySelector(link).dispatchEvent(evt); 
    }, link);
}

您可以这样称呼它:

        myclick( test, casper, 'a#logout' );

所以现在我不必禁用resize事件处理程序。

0 个答案:

没有答案