javascript - Bitbucket 管道和非 headless Puppeteer?

标签 javascript continuous-integration automated-tests puppeteer bitbucket-pipelines

我正在尝试运行非 headless puppeteer 来测试管道中的 chrome 扩展。

当我在谷歌上搜索这个主题时,我发现很多人能够让 headless puppeteer 师在管道上工作,但由于某种原因,我无法让它与非 headless 人一起工作。

Puppeteer 故障排除文档说 TravisCI 是可能的,所以管道也应该是可能的,不是吗?

我已经尝试了很多不同的 docker 图像,但它们似乎都不起作用。这是我目前的设置:

image: node:9

pipelines:
  branches:
    staging:
      - step:
        script:
          - node -v
          - yarn -v
          - yarn install
          - apt update && apt install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
          - apt-get install -y xvfb
          - export DISPLAY=:99.0
          - Xvfb :99 -ac &
          - yarn start build.staging
          - yarn start test.unit
          - yarn start test.e2e.staging

当我尝试这样做时:
export const loadBrowser = async () => {
  browser = await puppeteer.launch({
    headless: false,
    args: [
    `--disable-extensions-except=${absDistPath}`,
    `--load-extension=${absDistPath}`,
    "--user-agent=PuppeteerAgent",
    "--no-sandbox",
    "--disable-setuid-sandbox"
  ]
});

我得到的错误是:

TimeoutError: Timed out after 30000 ms while trying to connect to Chrome! The only Chrome revision guaranteed to work is r594312



有没有人设法让非 headless Puppeteer 真正在 Bitbucket Pipelines 上工作?

最佳答案

circlci 的人们构建了一个很好的 docker 镜像来帮助 headless puppeteer。我用它来测试 circlCI 和 bitbucket 管道。

我的测试设置:

一个非常简单的 mocha/chai 测试文件,我没有为 circlCI 和 bitbucket 管道测试配置任何 puppeteer 参数。

// index.js
module.exports = {
  async getLocation(page) {
    return page.evaluate(() => window.location.href);
  },
};

// test.js
const { expect, assert } = require('chai');
const puppeteer = require('puppeteer');
const example = require('./index');

describe('Puppeteer', () => {
  before(async function () {
    this.browser = await puppeteer.launch();
    this.page = await this.browser.newPage();
  });

  after(async function () {
    await this.browser.close();
    process.exit(0);
  });

  describe('Startup', () => {
    it('should start', async function () {
      assert.equal('object', typeof this.browser);
    });
  });

  describe('In Browser', () => {
    it('url should be blank', async function () {
      const url = await example.getLocation(this.page);
      expect(url).to.include('about:blank');
    });

    it('url should have example.com', async function () {
      await this.page.goto('http://example.com');
      const url = await example.getLocation(this.page);
      expect(url).to.include('example.com');
    });
  });
});

管道文件:
image: circleci/node:8.12.0-browsers

pipelines:
  default:
    - step:
        caches:
          - node
        script: 
          - yarn install
          - yarn run lint
          - yarn run test

bitbucket 和 circleci 的结果:

enter image description here

资源:
  • 使用图片circleci/node:8.12.0-browsers , 他们的 Dockerfile .
  • 还使用 this answer 上提供的 dockerfile 测试了类似的设置.

  • 笔记:
  • CirclCI 提取图像的时间更短,缓存时间几乎为 1-2 秒。整个运行只需约 13 秒。
  • Bitbucket 拉取图像需要更多时间,第一次拉取需要 2 分钟,下一次需要 10~30 秒缓存。总共约 45 秒来完成整个运行。
  • 这可能是因为分配给我用于测试的免费版本的资源。

  • 头部模式

    幸运的是,我上面提到的两个 dockerfile 都提供了 Xvfb。你只需要使用它们。代码还必须具有沙箱参数才能正常工作。

    添加参数:
    this.browser = await puppeteer.launch({
          headless: false,
          args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
          ],
    })
    

    将测试线替换为以下内容,
    xvfb-run -a --server-args="-screen 0 1024x768x24" yarn run test
    

    结果:

    关于javascript - Bitbucket 管道和非 headless Puppeteer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999911/

    相关文章:

    javascript - 如何将原型(prototype)函数作为参数?

    node.js - 你能在运行 npm publish 之前检查 npm 版本号是否有效吗?

    c# - 自动测试从设置中列出的代码中抛出错误,尽管它已经在其他测试中正确执行

    ruby - 如何使用 Capybara 和 Ruby 切换 Frame?

    testing - Loadrunner 11.5 中的 View_State 关联

    javascript - 切换按钮不会返回到初始状态 - Javascript

    javascript - 通过 jquery corrupt carousel 添加 css 样式

    javascript - AJAX 调用返回脚本的先前版本

    amazon-web-services - lambda 函数未使用 AWS 中的 cloudformation 和 pipeline 进行更新

    c# - WACK 检查构建