You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
注册

使用Protractor与browsermob-proxy-api调用addURLWhiteList时遇500服务器错误

Fixing "Error from server: 500" When Using addURLWhiteList with browsermob-proxy-api in Protractor

Let’s break down why you’re hitting that 500 error and how to fix it, so you can generate trimmed HAR files without all the third-party bloat.

Common Causes of the 500 Error

First, let’s cover the most likely culprits:

  • Incorrect parameter formatting: The addURLWhiteList API has strict requirements for regex patterns and required fields (like the proxy client ID).
  • Timing issues: You’re calling the API before the proxy client is fully initialized or linked to your browser.
  • Version mismatches: Your browsermob-proxy-api npm module doesn’t align with the version of the browsermob-proxy core service you’re running.

Step-by-Step Solutions

1. Fix Your addURLWhiteList Parameter Format

The API expects a properly escaped regex and the client ID of your active proxy instance. Here’s the correct syntax:

proxy.addURLWhiteList({
  client: yourProxyClientId, // Critical: Must match the active proxy port/client ID
  regex: 'https://your-target-domain\\.com/.*', // Double-escape dots in regex!
  matchType: 'REGEX' // Explicitly specify match type (required in some versions)
}, function(err, response) {
  if (err) console.error('Whitelist failed:', err);
  else console.log('Whitelist applied successfully');
});

Don’t skip escaping special regex characters (like .\\.)—invalid regex is a top cause of 500 errors here.

2. Align Proxy Initialization with Protractor Hooks

You need to sequence proxy setup, browser launch, and whitelist application correctly using Protractor’s lifecycle hooks. Here’s a complete, corrected conf.js snippet:

/* global exports, require, jasmine, browser */
var Proxy = require('browsermob-proxy-api');
var Q = require('q');
var fs = require('fs');

// Initialize proxy (point to your running browsermob-proxy service)
var proxy = new Proxy({ host: 'localhost', port: 8080 });
var proxyClientPort = 8081;

exports.config = {
  chromeOnly: true,
  capabilities: {
    browserName: 'chrome',
    proxy: {
      proxyType: 'manual',
      httpProxy: `localhost:${proxyClientPort}`
    }
  },
  beforeLaunch: function() {
    var deferred = Q.defer();
    // Start proxy client and initialize HAR capture first
    proxy.startPort(proxyClientPort, function(err, data) {
      if (err) return deferred.reject(err);
      // Save client ID for later use
      global.proxyClientId = data.port;
      // Initialize HAR capture
      proxy.createHAR({
        client: global.proxyClientId,
        captureHeaders: true,
        captureContent: true
      }, function(harErr) {
        harErr ? deferred.reject(harErr) : deferred.resolve();
      });
    });
    return deferred.promise;
  },
  onPrepare: function() {
    // Apply whitelist AFTER browser has launched and linked to proxy
    return Q.nfcall(proxy.addURLWhiteList, {
      client: global.proxyClientId,
      regex: 'https://your-target-domain\\.com/.*'
    });
  },
  afterLaunch: function() {
    var deferred = Q.defer();
    // Export HAR and clean up proxy
    proxy.getHAR({ client: global.proxyClientId }, function(err, har) {
      if (err) return deferred.reject(err);
      fs.writeFileSync('./trimmed-test.har', JSON.stringify(har));
      // Stop proxy client
      proxy.stopPort(global.proxyClientId, function() {
        deferred.resolve();
      });
    });
    return deferred.promise;
  }
};

Key points here:

  • Use beforeLaunch to start the proxy and initialize HAR before the browser opens.
  • Wait until onPrepare (browser is running) to apply the whitelist—this ensures the proxy is fully linked to your browser session.

3. Verify Version Compatibility

Make sure your browsermob-proxy-api module matches the version of the browsermob-proxy core service you’re running. To fix mismatches:

  1. Uninstall and reinstall the npm module:
    npm uninstall browsermob-proxy-api
    npm install browsermob-proxy-api --save-dev
    
  2. Confirm you’re running a compatible browsermob-proxy binary (e.g., v2.1.4 is a stable choice). Start it explicitly before running Protractor:
    java -jar browsermob-proxy-2.1.4/bin/browsermob-proxy --port 8080
    

4. Debug with Proxy Logs

If you still get 500 errors, enable debug logs for browsermob-proxy to see the exact server-side issue:

java -jar browsermob-proxy-2.1.4/bin/browsermob-proxy --port 8080 --log-level DEBUG --log-file ./bmp-debug.log

Check the log file for details like invalid regex syntax or missing parameters—this will point you straight to the problem.

Alternative: Use Blacklist Instead

If whitelisting continues to give you trouble, try blacklisting third-party URLs instead—it’s often more straightforward:

proxy.addURLBlackList({
  client: global.proxyClientId,
  regex: 'https://third-party-domain\\.com/.*',
  status: 200 // Return empty 200 response to exclude from HAR
}, function(err) {
  if (err) console.error('Blacklist failed:', err);
});

内容的提问来源于stack exchange,提问作者John Doe

火山引擎 最新活动