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
});
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With