如何在package.json中配置AWS环境变量并实现Protractor脚本动态化
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 ofNODE_ENVif it’s set, otherwise fall back todevelopmentas the default.- Now running
NODE_ENV=staging npm run protractorwill loadstaging.js, whilenpm run protractor(without settingNODE_ENV) will usedevelopment.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.
Option B: Use dotenv for Local Development (Recommended)
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 protractorwithout settingNODE_ENV—it should loaddevelopment.jsautomatically. - Run
NODE_ENV=test npm run protractor—it should targettest.js. - Log
process.env.AWS_ACCESS_KEY_IDin your Protractor config temporarily to confirm variables are loading correctly.
内容的提问来源于stack exchange,提问作者Amr Salem

