Angularjs - simulate touch event in Protractor e2e test
Asked Answered
D

1

8

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
});
Diamagnetism answered 18/5, 2014 at 10:13 Comment(0)
B
8

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

Buddie answered 19/5, 2014 at 0:41 Comment(7)
TNX for the answer. This did not work for me. I get: TypeError: Object #<Object> has no method 'trigger'. It might be related to the fact that this is a Protractor test and the element is not a regular jQuery elementDiamagnetism
Hey, sorry, I didn't follow up when the comment was incomplete. By now you hopefully have come past it, but if not, see if the updated version helps.Buddie
Hey. tnx again for the answer. triggerHandler resulted in similar error: TypeError: Object #<Object> has no method 'triggerHandler'. I guess the element(by.css('.video-poster')) is not a jqLite element as well...Diamagnetism
Oops, forgive my ignorance. Yes, it's a Selinium element finder github.com/angular/protractor/blob/master/docs/…Buddie
Thanks man! this is working (I had to add "browser" to "executeScript" in your last update).Diamagnetism
thanks m8, but what if I want to simulate a touchStart, a move and then a TouchEnd event?Miserere
have you tried something like #25665051Buddie

© 2022 - 2024 — McMap. All rights reserved.