code everywhere
technology, web services and applications

access the angularJS $scope in phantomJS

posted on June 22, 2015, 10:03 pm in js, phantomjs

Use this code to access your AngularJS $scope variable inside of PhantomJS, useful for testing your e2e AngularJS pages.

var ng page.evaluate(function() {
    var 
scope angular.element(document.querySelector("[ng-controller]")).scope();

    
// You now have access to your application's $scope variables and functions.
    
scope.someVariable 25;
    
scope.someFunction();
    
scope.$apply();
});

With multiple controllers, use:

document.querySelector("[ng-controller='controllerName']")

to select a specific one.

recent posts

< back