25

I am trying to set a global variable on protractor to use in all describe blocks.

var glob = 'test';

describe('glob test', function () {
    it('should set glob', function () {
        browser.get('http://example.com/test');
        browser.executeScript(function () {
            window.glob = glob;
        });
    });    
});

But this returns me the following error:

Message:
[firefox #2]      UnknownError: glob is not defined

I also looked at this question: protractor angularJS global variables

so i tried to set the variable glob in conf.js in this way:

exports.config = {
  ...,
  onPrepare: function () {
      global.glob = 'test';
  }
};

Still, having the same error.

How can i add properly global variables in protractor tests?

Community
  • 1
  • 1
pietrovismara
  • 6,102
  • 5
  • 33
  • 45

4 Answers4

52

It is possible to set globals from the Protractor config file with the help of params property:

exports.config = {
    // ...

    params: {
        glob: 'test'
    }

    // ...
};

And you can access it in the specs using browser.params.glob.

See the reference config file.

The params object will be passed directly to the Protractor instance, and can be accessed from your test as browser.params. It is an arbitrary object and can contain anything you may need in your test. This can be changed via the command line as:

protractor conf.js --params.glob 'other test'

Update:

From the docs for browser.executeScript:

If the script is provided as a function object, that function will be converted to a string for injection into the target window. Any arguments provided in addition to the script will be included as script arguments and may be referenced using the arguments object.

So, JavaScript scope in this case does not work, you function that is passed to browser.executeScript won't have any closure variables from spec like browser. But you can pass these variables explicitly:

browser.executeScript(function (glob) {

    // use passed variables on the page
    console.log(glob);

}, browser.params.glob);
Michael Radionov
  • 12,859
  • 1
  • 55
  • 72
27

You can also set global variables in onPrepare() using global:

onPrepare: function () {
    global.myVariable = "test";
},

Then, you would just use myVariable throughout the tests as is.

This is actually how protractor, browser and other built-in global variables were made available globally:

Runner.prototype.setupGlobals_ = function(browser_) {
  // Export protractor to the global namespace to be used in tests.
  global.protractor = protractor;
  global.browser = browser_;
  global.$ = browser_.$;
  global.$$ = browser_.$$;
  global.element = browser_.element;
  global.by = global.By = protractor.By;

  // ...
}

Note that with this approach you are polluting your global scope/namespace, be careful.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I've been struggling with this for a while - using this approach works except for when I do something like `global.foo = require('./foo.js');` where `foo.js` is something simple like `module.exports = 'asdf';` – Kevin Friedheim Jun 02 '16 at 22:58
0

Another option is to use a process variable

Protractor is a node process. Any node process can be started with custom node variables. Not sure how it's done in windows (please comment if you know how) but for mac and any linux/unix OS you can

Start protractor with environment variable like this

MY_VAR=Dev protractor tmp/config.js

And then it will be available anywhere within your process and even in your config

console.log(process.env.MY_VAR)
Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
-6

I know I'm a bit late to the answer but here's another way of setting global variables that can be used throughout the file

describe("Some Global most outer describe", function(){
    var glob = "some global variable";
    var blob = "some other global variable";

    it('should test glob', function(){
        expecte(glob).toEqual("some global variable");
    });

    it('should test blob', function(){
        expecte(glob).toEqual("some other global variable");
    });

    describe('Some inner describe', function(){
        //Some other inner tests can also see glob and blob
    });
});
Haseeb
  • 190
  • 1
  • 6
  • 16
  • 2
    **glob** and **blob** can not be used in scope outside describe ("Some Global most outer describe", function()) Instead declare them outside , initiate and use them wherever through out that file. – Subhash Feb 02 '17 at 09:37
  • Well this is the same as my example in question, except you moved the variables declaration inside the describe function. It doesn't work. – pietrovismara Mar 15 '17 at 18:32
  • It works if you have the other describes inside the outer most describe. That's how I used it a while ago... I agree now that it's not the best solution. – Haseeb Mar 16 '17 at 03:20