【问题标题】:VSCode debugger can not connect to puppeteer instance run by KarmaVSCode 调试器无法连接到 Karma 运行的 puppeteer 实例
【发布时间】:2020-01-13 12:22:42
【问题描述】:

我有使用 Karma 和 Jasmine 运行的 Angular 前端测试,我想使用 Visual Studio Code 进行调试。测试在 puppeteer 实例中运行。

  • 当调试器尝试连接时,它会超时,并告诉我套接字已挂起。
  • 当我访问我的 puppeteer 实例正在监听的 http://localhost:9333/ 时,我得到一个 ERR_EMPTY_RESPONSE
  • 将调试器连接到以 chrome --headless --remote-debugging-port=9333 开头的无头 Chrome 实例可以正常工作。

我的 karma.conf.js:

// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html

process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-spec-reporter'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
    ],
    client: {
      clearContext: false, // leave Jasmine Spec Runner output visible in browser
    },
    mime: {
      'text/x-typescript': ['ts', 'tsx'],
    },
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true,
    },
    angularCli: {
      environment: 'dev',
    },
    reporters:
      config.angularCli && config.angularCli.codeCoverage
        ? ['progress', 'coverage-istanbul']
        : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        debug: true,
        flags: ['--no-sandbox', '--remote-debugging-port=9333'],
      },
    },
    browserNoActivityTimeout: 120000,
    singleRun: false,
  });
};

我的launch.json的相关部分:

{
  "type": "chrome",
  "request": "attach",
  "name": "Debug Frontend Tests",
  "address": "localhost",
  "port": 9333,
  "sourceMaps": true,
  "webRoot": "${workspaceFolder}/client/web",
}

【问题讨论】:

    标签: google-chrome visual-studio-code karma-jasmine karma-runner puppeteer


    【解决方案1】:

    TL,DR:将标志 '--remote-debugging-address=0.0.0.0' 添加到您的 customLaunchers' flags 数组中。

    事实证明,添加'--remote-debugging-port=9333' 标志只会为本地计算机上的客户端打开端口。由于 puppeteer 在 Docker 容器中运行,因此它将 VSCode 视为远程客户端并拒绝连接。

    在容器内运行 lsof -i -P -n | grep LISTEN 表明 Chrome 仅为本地客户端打开端口 9333:

    root@b6ff203497dc:~# lsof -i -P -n | grep LISTEN 
    node    247 root   21u  IPv4 224919      0t0  TCP *:9876 (LISTEN)
    chrome  260 root  137u  IPv4 229390      0t0  TCP 127.0.0.1:9333 (LISTEN)
    

    另外添加标志 '--remote-debugging-address=0.0.0.0' 会使 Chrome 打开所有 IP 地址的端口。

    root@b6ff203497dc:~# lsof -i -P -n | grep LISTEN 
    node    247 root   21u  IPv4 224919      0t0  TCP *:9876 (LISTEN)
    chrome  260 root  137u  IPv4 229390      0t0  TCP *:9333 (LISTEN)
    

    现在 VSCode 可以连接了。

    关键提示来自:

    Chrome remote debugging from another machine

    https://github.com/puppeteer/puppeteer/issues/5063#issuecomment-561595885

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2021-11-27
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多