如何动态修改protractor-html-screenshot-reporter的测试结果文件夹路径?
Got it, let's walk through practical ways to dynamically set the results directory for your protractor-jasmine2-screenshot-reporter setup, using your existing conf.js as a starting point. Here are three common, flexible approaches:
1. Pass the Path via Command-Line Parameters
This lets you specify a custom folder every time you run your tests, which is great for ad-hoc runs or targeting different output locations.
First, update your conf.js to accept a parameter with a default fallback:
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter'); exports.config = { // Define a default results directory (used if no param is passed) params: { resultsDir: '/opt/src/protractor/results/' }, // ... rest of your existing config options beforeLaunch: function() { // Initialize the reporter with the dynamic path from params var reporter = new HtmlScreenshotReporter({ dest: exports.config.params.resultsDir, filename : 'index.html', showSummary : true, showQuickLinks : true, showConfiguration : true, cleanDestination : true, ignoreSkippedSpecs : false, reportOnlyFailedSpecs : false, captureOnlyFailedSpecs : true, }); // Don't forget to add the reporter to Jasmine (assuming you had this logic) jasmine.getEnv().addReporter(reporter); }, // ... remaining config code };
Then run your tests with the custom path like this:
protractor conf.js --params.resultsDir=/opt/src/protractor/my-custom-results/
2. Generate a Timestamped Folder (Auto-Archiving)
If you want to avoid overwriting previous test results (great for debugging or tracking runs over time), generate a unique folder with a timestamp each time tests run.
Modify your conf.js like this:
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter'); // Create a unique directory name using a timestamp (replace invalid chars) const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const dynamicResultsDir = `/opt/src/protractor/results-${timestamp}/`; var reporter = new HtmlScreenshotReporter({ dest: dynamicResultsDir, filename : 'index.html', showSummary : true, showQuickLinks : true, showConfiguration : true, cleanDestination : true, ignoreSkippedSpecs : false, reportOnlyFailedSpecs : false, captureOnlyFailedSpecs : true, }); exports.config = { // ... your existing config beforeLaunch: function() { jasmine.getEnv().addReporter(reporter); }, // ... rest of config };
Now every test run will create a folder like results-2024-05-20T14-30-00-123Z/, keeping all results separate.
3. Use Environment Variables (Great for CI/CD)
If you're running tests in a CI/CD pipeline or want to set the path globally for your environment, use an environment variable with a default fallback.
Update your conf.js:
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter'); // Use the environment variable if set, else fall back to the default path const resultsDir = process.env.PROTRACTOR_RESULTS_DIR || '/opt/src/protractor/results/'; var reporter = new HtmlScreenshotReporter({ dest: resultsDir, // ... other reporter options }); exports.config = { // ... your existing config beforeLaunch: function() { jasmine.getEnv().addReporter(reporter); }, // ... rest of config };
Then set the environment variable before running your tests:
# Linux/macOS export PROTRACTOR_RESULTS_DIR=/opt/src/protractor/ci-results/ protractor conf.js # Windows (Command Prompt) set PROTRACTOR_RESULTS_DIR=C:\src\protractor\ci-results\ protractor conf.js
Pick the approach that best fits your workflow—command-line params for manual control, timestamps for auto-archiving, or environment variables for CI environments.
内容的提问来源于stack exchange,提问作者jissay

