Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to $rootScope.$broadcast event in Protractor

Does anybody have a working example of how to $rootScope.$broadcast event in Protractor tests?

I know Protractor is not used for this, and that i should use Karma for unit testing. But the situation is that I need to broadcast an event in Protractor test. For this i need to get the $rootScope, i guess. But i do not know how to do it in Protractor and I failed to find an answer in the internet after long hours of searching.

I guess the below regards unit testing. There is no inject in protractor.

var rootScope;
beforeEach(inject(function($injector) {
    rootScope = $injector.get('$rootScope');
}));

$rootScope.$broadcast("EVENT_TO_TEST", other, possible, args);

Hope the solution exists.

like image 870
Peter Babukh Avatar asked Mar 13 '23 06:03

Peter Babukh


1 Answers

You could run a script within the browser this way:

browser.executeScript("
  angular.element('body').injector().get('$rootScope').$broadcast(...);
");

Or using a different syntax to get the root scope:

browser.executeScript("
  angular.element('body').scope().$root.$broadcast(...);
");

You can replace body by whichever element contain a scope.

like image 71
floribon Avatar answered Mar 20 '23 19:03

floribon