Angularjs - simulate touch event in Protractor e2e test

允我心安 提交于 2019-12-12 09:37:12

问题


I use Protractor with Jasmine for my mobile Angularjs app. I want to test a touch event (touchStart / touchEnd etc...) on a particular element. Something like:

it('should play video', function(){
    var poster = by.css('.video-poster');
    element(poster).??? //Simulate touch event here
});

回答1:


Update:

Since Protractor returns a Selenium element finder not an angular element, you'll have to use the executeScript() function to call a JavaScript method on it, like:

var poster = element(by.css('.video-poster'));
browser.executeScript(
    'angular.element(arguments[0]).triggerHandler("touchstart");', poster);

Original:

You should be able to trigger the event, like:

element(poster).triggerHandler("touchstart");

If you need more stuff in the event object, you can create one like this answer: jQuery Trigger Event in AngularJS Karma Test

Note:

It seems Angular does not provide you with trigger() method like jQuery, only triggerHandler(), as per https://docs.angularjs.org/api/ng/function/angular.element



来源:https://stackoverflow.com/questions/23720823/angularjs-simulate-touch-event-in-protractor-e2e-test

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