How to make a globally accessible variable?

前端 未结 6 2060
难免孤独
难免孤独 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 19:10

    Not sure it's the best way, but here is how I do it : you can define a variable in the browser.globals and access it in your different tests

    For instance :

     module.exports = {
      before: function(browser) {
        console.log("Setting up...");
    
        // initialize global variable state
        browser.globals.state = {};
      },
    
      "first test": function(browser) {
        var settings = browser.globals,
            state = browser.globals.state;
    
        state.my_shared_var = "something";
    
        browser.
          // ...
          // use a shared variable
          .setValue('input#id', state.my_shared_var)
          // ...
    
          // ...
          // save something from the page in a variable 
          .getText("#result", function(result) {
            state.my_shared_result = result.value;
          })
          // ...
      },
    
      "second test": function(browser) {
        var settings = browser.globals,
            state = browser.globals.state;
    
        browser.
          // ...
          // use the variables
          .url("http://example.com/" + state.my_shared_result + "/show")
          .assert.containsText('body', state.my_shared_var)
          // ...
      }
    }
    

提交回复
热议问题