Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - simulate touch event in Protractor e2e test

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
});
like image 490
Yaniv Efraim Avatar asked May 18 '14 10:05

Yaniv Efraim


1 Answers

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

like image 141
Meligy Avatar answered Nov 06 '22 12:11

Meligy