You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在package.json中配置AWS环境变量并实现Protractor脚本动态化

Dynamic Protractor Scripts with AWS Environment Variables in package.json

Got it, let's break this down into actionable steps to make your Protractor setup dynamic and integrate AWS environment variables seamlessly.

1. Make Protractor Load Environment-Specific Config Files

Your original script uses a fixed conf.js file—we can replace that with a dynamic reference to NODE_ENV using shell variable expansion (perfect for Linux/macOS environments, which aligns with your use of xvfb-run in a pipeline).

Update the scripts section in your package.json like this:

"scripts": {
  "protractor": "xvfb-run --server-args='-screen 0 1920x1080x24' protractor src/test/tests/${NODE_ENV:-development}.js"
}
  • ${NODE_ENV:-development} means: use the value of NODE_ENV if it’s set, otherwise fall back to development as the default.
  • Now running NODE_ENV=staging npm run protractor will load staging.js, while npm run protractor (without setting NODE_ENV) will use development.js.

2. Integrate AWS Environment Variables

There are two clean approaches to inject AWS variables, depending on your pipeline and security needs:

Option A: Inject Variables Directly in Scripts (CI Pipeline-Friendly)

If your CI/CD pipeline already injects AWS credentials (like AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY) as environment variables, you don’t need to hardcode them—your Protractor config can access them directly via process.env.

If you want to set non-sensitive AWS variables explicitly in package.json, you can prefix the script:

"scripts": {
  "protractor:dev": "NODE_ENV=development AWS_REGION=us-east-1 xvfb-run --server-args='-screen 0 1920x1080x24' protractor src/test/tests/${NODE_ENV}.js",
  "protractor:prod": "NODE_ENV=production AWS_REGION=eu-west-1 xvfb-run --server-args='-screen 0 1920x1080x24' protractor src/test/tests/${NODE_ENV}.js"
}

Note: Never hardcode sensitive credentials like AWS_SECRET_ACCESS_KEY in package.json—rely on your pipeline’s secret management instead.

For local testing, store AWS variables in a .env file (add this to .gitignore to avoid committing sensitive data):

NODE_ENV=development
AWS_ACCESS_KEY_ID=your-local-access-key
AWS_SECRET_ACCESS_KEY=your-local-secret-key
AWS_REGION=us-east-1

Install dotenv as a dev dependency:

npm install dotenv --save-dev

Load the env file before running Protractor by either adding a pre-script:

"scripts": {
  "preprotractor": "node -r dotenv/config",
  "protractor": "xvfb-run --server-args='-screen 0 1920x1080x24' protractor src/test/tests/${NODE_ENV:-development}.js"
}

Or requiring it directly in your environment-specific Protractor config (e.g., development.js):

require('dotenv').config();

exports.config = {
  // Your existing Protractor config
  params: {
    aws: {
      accessKey: process.env.AWS_ACCESS_KEY_ID,
      secretKey: process.env.AWS_SECRET_ACCESS_KEY,
      region: process.env.AWS_REGION
    }
  }
};

3. Verify the Setup

  • Run npm run protractor without setting NODE_ENV—it should load development.js automatically.
  • Run NODE_ENV=test npm run protractor—it should target test.js.
  • Log process.env.AWS_ACCESS_KEY_ID in your Protractor config temporarily to confirm variables are loading correctly.

内容的提问来源于stack exchange,提问作者Amr Salem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:32:56