是否可以在 Travis Chrome 上 --disable-web-security?

Is it possible to --disable-web-security on Travis Chrome?

我正在开发一个需要 --disable-web-security 进行测试的项目。我使用具有以下设置的 Karma:

    browsers: [
        "ch"
    ],
    customLaunchers: {
        "ch": {
            "base": "Chrome",
            "flags": ["--disable-web-security"]
        }
    },

if (process.env.TRAVIS)
    options.customLaunchers.ch.flags.push("--no-sandbox");

这可以在 Chrome v69.0.3497.100 win7 x64 的本地主机上正常工作。

我正在尝试使用以下 yml 运行 Travis 上的相同代码(通过将更改推送到 GitHub):

language: node_js
node_js:
  - "9"
before_install:
  - export CHROME_BIN=chromium-browser
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start
  - sleep 5

Chromium != Chrome 以来,我想我们在这里谈论的是具有相同引擎的 2 种不同浏览器,但我不确定。无论如何,我尝试在 Travis 上构建时收到一条错误消息:

Blocked a frame with origin "http://localhost:9876" from accessing a cross-origin frame.

这清楚地表明网络安全已启用。有什么方法可以在 Travis 上禁用网络安全吗?

解决方案:

使用真正的 Trusty Chrome 解决了它:

language: node_js
node_js:
  - "9"
dist: trusty
addons:
  chrome: stable
before_install:
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start
  - sleep 5

根据 this documentation 你可以 运行 Chrome 通过在你的 .travis.yml 配置文件中写入以下内容

dist: trusty
addons:
  chrome: stable
before_install:
  - # start your web application and listen on `localhost`
  - google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost &

至于您的 Karma 配置文件,请检查 this page。表示你还要加一个flag。

For security reasons, Google Chrome is unable to provide sandboxing when it is running in the container-based environment.

To use Chrome in the container-based environment, pass the --no-sandbox flag to the chrome executable.

module.exports = function(config) {
  config.set({
    browsers: ['Chrome', 'ChromeHeadless', 'ChromeHeadlessNoSandbox'],

    // you can define custom flags
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    }
  })
}

请注意,我以前从未做过。我只是向您指出正确的文档。