Search code examples
angularjstestingprotractorangularjs-e2ee2e-testing

How to run a javascript function before starting any test case in Protactor?


Actually, i am writing some test cases for one angular app but the first page is taking too much time too load and having a popup box for instruction. So i need to store some value in localStorage of browser for making that popup box invisible. So how can i run any javascript function before running any test case?


Solution

  • By default, protractor is syncing with angular, which makes it wait for $http or $timeout you can check if that's the case on this particular page with this: Canonical way to debug Protractor-to-Angular sync issues

    or you can inject the behaviour with mocked module

    protractor.config.js

    onPrepare: function() {
        var doTheFuncyStuff= function() {
            angular
                .module('doTheFuncyStuff', [])
                .run([function(){
                  //do something here so the popup isn't displayed
                  window.localStorage.setItem('my-var', 'my-val');
                }]);
        };
    
        browser.addMockModule('doTheFuncyStuff', doTheFuncyStuff);
    }