问题
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