0

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?

Shravan Jain
  • 720
  • 1
  • 11
  • 32

1 Answers1

1

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);
}
Community
  • 1
  • 1
maurycy
  • 8,455
  • 1
  • 27
  • 44
  • Thanks for your quick reply buddy. Now i tried your provided solution but i need to use browser inside .run. So how can i use browser inside browser? – Shravan Jain Apr 07 '17 at 18:22
  • i need to run browser.executeScript() inside .run method – Shravan Jain Apr 07 '17 at 18:23
  • 1
    You can run any code in the `run` block, whatever you put in executeScript you can do it there too – maurycy Apr 07 '17 at 18:27
  • but it is throwing the error when i am trying to access browser inside run `browser.executeScript("window.localStorage.setItem('my-var', 'my-val');");` – Shravan Jain Apr 07 '17 at 18:29
  • I've edited my answer, you just need to skip the `browser.executeScript` since the mocked module will run in browser, everything else runs in protractor instance – maurycy Apr 07 '17 at 18:40