4

I'm using protractor with angularjs to create my e2e tests. I have many files for which my specs array is quite big and I want to share a common function across all files. Is there a way to create a global beforeEach of some sort where I can inject my function? Does the exports.config object expose something so that I can have a common variable across all files? Currently I'm piggy backing off the "browser" variable but that can potentially be dangerous. Any help is much appreciated. Thanks

climboid
  • 6,932
  • 14
  • 44
  • 71

2 Answers2

12

Yes, you can easily do that using the onPrepare()hook in the protractor configuration:

exports.config = {
    // ...

    // A callback function called once protractor is ready and available, and
    // before the specs are executed
    // You can specify a file containing code to run by setting onPrepare to
    // the filename string.
    onPrepare: function() {
        // you can also add properties to globals here
    }
 };
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I've tried that but it isn't working for me I did onPrepare: function(){ var ptor = protractor.getInstance(); } and ptor is no longer available in my first test – climboid Jan 27 '14 at 17:29
  • 2
    `var ptor` makes ptor a local variable of the onPrepare() funtion. You need to add it to globals: `global.ptor = protractor.getInstance()`. Anyway, this is unnecessary with recent versions of protractor, which automatically publishes a 'browser' global variable (that it used to call ptor before). You just need to use `browser` in your tests. – JB Nizet Jan 28 '14 at 07:05
  • 2
    Ahh ok so in my comment I was just trying to make the point of the variable but I see what you mean. What I'm actually doing right now is using the browser variable and adding properties to it such as browser.projectId = 10 and that does persist along all of my files. I guess this is the right way to go then – climboid Jan 28 '14 at 14:07
-1

This works wonderfully...Just make sure you properly terminate the previous config params by using proper delimiter ',' .

        exports.config = {
           seleniumAddress: **,**
           onPrepare: function() {
              browser.driver.manage().window().maximize();
           }**,**
           baseUrl:
           }
Abhijeet
  • 8,561
  • 5
  • 70
  • 76