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.
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.
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