How to trigger a tap event on Chrome devtool console?

↘锁芯ラ 提交于 2019-12-04 07:53:53

I suppose you are doing some kind of functional testing on your mobile app. I was doing the same thing some time ago (using CasperJS) and, in the process, I've created this function:

// I've commented out CasperJS specific stuff, don't use it if you don't need it
function triggerEventOnPage(selector, eventName, memo) {
    //casper.evaluate(function(selector, eventName, memo){
        var event;
        var element = document.querySelector(selector);

        event = document.createEvent("Event");
        event.initEvent(eventName, true, true);
        event.memo = memo || { };

        element.dispatchEvent(event);
    //}, selector, eventName, memo);
    //wait();
}

You can use it in your tests by calling:

triggerEventOnPage(".edit-list-button", 'tap');

However, mind that there is no native tap event. There are only touchstart, tachmove, touchend events and implementation of tap is done based on those three. Therefore, implementation of tap event that you are using may differ from one that I was using and the function above may not work for you.

EDIT: since you are using jQuery, $('button.nominate').trigger('tap') should work just fine to. @NULL may be right that your selector is invalid.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!