code everywhere

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
find something helpful? send us some coin BTC 19u6CWTxH4cH9V2yTfAemA7gmmh7rANgiv

(ad) Need Hosting? Try Linode, VPS Starting from $5

privacy policy