How to make a globally accessible variable?

前端 未结 6 2080
难免孤独
难免孤独 2021-01-11 18:22

How can I make a globally accessible variable in nightwatch.js? I\'m using a variable to store a customized url (dependent on which store is loaded in our online product), b

6条回答
  •  北恋
    北恋 (楼主)
    2021-01-11 18:50

    To expand on Tricote's answer, Nightwatch has built-in support for this. See the documentation.

    You can either specify it in the nightwatch.json file as "globals": {"myvar": "whatever"} or in a globals.js file that you reference within nightwatch.json with "globals": "path/to/globals.js". In the latter case, globals.js could have:

    module.exports = {
      myvar: 'whatever'
    };
    

    In either case, you can access the variable within your tests as Tricote mentioned:

    module.exports = {
      "test": function(browser) {
        console.log(browser.globals.myvar); // "whatever"
      }
    };
    

提交回复
热议问题