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
});
Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96

1 Answers1

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

Community
  • 1
  • 1
Meligy
  • 35,654
  • 11
  • 85
  • 109
  • TNX for the answer. This did not work for me. I get: TypeError: 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 element – Yaniv Efraim May 19 '14 at 05:59
  • 1
    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. – Meligy May 20 '14 at 07:05
  • Hey. tnx again for the answer. triggerHandler resulted in similar error: TypeError: Object # has no method 'triggerHandler'. I guess the element(by.css('.video-poster')) is not a jqLite element as well... – Yaniv Efraim May 20 '14 at 11:46
  • 1
    Oops, forgive my ignorance. Yes, it's a Selinium element finder https://github.com/angular/protractor/blob/master/docs/api.md#protractorby – Meligy May 20 '14 at 13:13
  • 1
    Thanks man! this is working (I had to add "browser" to "executeScript" in your last update). – Yaniv Efraim May 21 '14 at 06:58
  • thanks m8, but what if I want to simulate a touchStart, a move and then a TouchEnd event? – Alexandros Spyropoulos Sep 04 '14 at 11:32
  • have you tried something like http://stackoverflow.com/questions/25664551/how-to-simulate-a-drag-and-drop-action-in-protractor/25667634#25667634 – Meligy Sep 04 '14 at 23:48