javascript 如何在 Angularjs Protractor 中使用系统环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23224883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:34:16  来源:igfitidea点击:

How can I use System Environment Variables in Angularjs Protractor?

javascriptnode.jsangularjsprotractor

提问by Brendan

My plan is to store a username and password as system environment variables and reference them in an Angularjs Protractor config file. I defined the variables in /etc/environment. This is what I've tried so far:

我的计划是将用户名和密码存储为系统环境变量,并在 Angularjs Protractor 配置文件中引用它们。我在/etc/environment. 这是我迄今为止尝试过的:

params: {
  login: {
    user: $E2E_USER,
    pass: $E2E_PASS
  }
}

I also tried this:

我也试过这个:

params: {
  login: {
    user: process.env.E2E_USER,
    pass: process.env.E2E_PASS
  }
}

Any help would be much appreciated!

任何帮助将非常感激!

采纳答案by Brendan

I used export E2E_USER=usernameand that worked.

我用过export E2E_USER=username,效果很好。

回答by cvakiitho

You have to use process.env.YOUR_ENVin NodeJS, so for example inside onPrepare function, not directly inside params:

您必须process.env.YOUR_ENV在 NodeJS 中使用,例如在 onPrepare 函数内部,而不是直接在 params 内部:

exports.config = {
    onPrepare: function() {
        //load env variables for testing
        if (typeof process.env.URL !== "undefined") {
            browser.baseUrl = process.env.URL;
            console.log('Base URL = ' +process.env.URL);
        }
},
params: { ...