Puppeteer无法在GitHub Actions中启动

9

我正在使用 Puppeteer 测试这个 Chrome 扩展程序。我在本地运行测试很好,但在 GitHub Actions 上却无法工作。我怀疑问题出在 GitHub Actions 环境中运行 Puppeteer,但我不确定根本问题是什么。

我尝试了 config.ymlruns-on 选项的 linux-latestwindows-latestmacos-latest,它们给了我不同的错误信息:

linux-latest

    Failed to launch the browser process!
    [2801:2801:0606/033446.894051:ERROR:browser_main_loop.cc(1468)] Unable to open X display.
    Received signal 11 SEGV_MAPERR 000000000000
    #0 0x5617d755b399 base::debug::CollectStackTrace()
    #1 0x5617d74bc2a3 base::debug::StackTrace::StackTrace()
    #2 0x5617d755af35 base::debug::(anonymous namespace)::StackDumpSignalHandler()
    #3 0x7efd5b4f6890 (/lib/x86_64-linux-gnu/libpthread-2.27.so+0x1288f)
    #4 0x5617d9f51f38 ChromeBrowserMainExtraPartsViewsLinux::~ChromeBrowserMainExtraPartsViewsLinux()
    #5 0x5617d7103140 ChromeBrowserMainParts::~ChromeBrowserMainParts()
    #6 0x5617d7102cfe ChromeBrowserMainPartsLinux::~ChromeBrowserMainPartsLinux()
    #7 0x5617d57fc1bf content::BrowserMainLoop::~BrowserMainLoop()
    #8 0x5617d57fc2ce content::BrowserMainLoop::~BrowserMainLoop()
    #9 0x5617d5800c2b content::BrowserMainRunnerImpl::Shutdown()
    #10 0x5617d5800738 content::BrowserMainRunnerImpl::~BrowserMainRunnerImpl()
    #11 0x5617d57fbd30 content::BrowserMain()
    #12 0x5617d7086796 content::ContentMainRunnerImpl::RunServiceManager()
    #13 0x5617d70863c7 content::ContentMainRunnerImpl::Run()
    #14 0x5617d70e7ad1 service_manager::Main()
    #15 0x5617d7084631 content::ContentMain()
    #16 0x5617d4ada5ae ChromeMain
    #17 0x7efd54e61b97 __libc_start_main
    #18 0x5617d4ada3ea _start
      r8: 0000000000000000  r9: 0000000000000001 r10: 0000000000000002 r11: 00001471e8e09708
     r12: aaaaaaaaaaaaaaaa r13: 00001471e8df6a50 r14: 00001471e8e09878 r15: 00001471e8d52460
      di: 00001471e8e09840  si: 00001471e8ea4220  bp: 00007ffedc9b6d30  bx: 00001471e8e09840
      dx: 00001471e8ea4220  ax: 0000000000000000  cx: fffffffd4f415a7b  sp: 00007ffedc9b6d20
      ip: 00005617d9f51f38 efl: 0000000000010202 cgf: 002b000000000033 erf: 0000000000000004
     trp: 000000000000000e msk: 0000000000000000 cr2: 0000000000000000
    [end of stack trace]
    Calling _exit(1). Core file will not be generated.
    [0606/033446.978197:ERROR:nacl_helper_linux.cc(308)] NaCl helper process running without a sandbox!
    Most likely you need to configure your SUID sandbox correctly

    TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md

      at onClose (node_modules/puppeteer/lib/launcher/BrowserRunner.js:159:20)
      at Interface.helper_1.helper.addEventListener (node_modules/puppeteer/lib/launcher/BrowserRunner.js:149:65)

这种情况发生在puppeteer.launch()期间。我尝试了--no-sandbox--disable-setuid-sandbox标志,但没有起作用。

windows-latest

    net::ERR_BLOCKED_BY_CLIENT at chrome-extension://acdcddifhaiiiagbodmcnebcgdmlgdkl/popup/popup.html

> 20 |     await page.goto('chrome-extension://acdcddifhaiiiagbodmcnebcgdmlgdkl/popup/popup.html');

      at navigate (node_modules/puppeteer/lib/FrameManager.js:95:23)
        -- ASYNC --
      at Frame.<anonymous> (node_modules/puppeteer/lib/helper.js:94:19)
      at Page.goto (node_modules/puppeteer/lib/Page.js:476:53)
      at Page.goto (node_modules/puppeteer/lib/helper.js:95:27)
      at Object.beforeEach (tests/e2e.test.js:20:16)

这是当我使用page.goto()访问我的扩展程序的HTML页面时发生的。 macos-latest
    Timeout - Async callback was not invoked within the 30000 ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 30000 ms timeout specified by jest.setTimeout.

      at mapper (node_modules/jest-jasmine2/build/queueRunner.js:29:45)

    TimeoutError: Timed out after 30000 ms while trying to connect to the browser! Only Chrome at revision r756035 is guaranteed to work.

      at onTimeout (node_modules/puppeteer/lib/launcher/BrowserRunner.js:170:20)
      at Timeout.task (node_modules/jsdom/lib/jsdom/browser/Window.js:391:19)

这两个超时错误发生在不同的位置。

这是我的beforeEach(),它无法在GitHub Actions上成功执行:

beforeEach (async () => {
    browser = await puppeteer.launch({
      headless: false,
      args: [
        `--disable-extensions-except=${extensionPath}`,
        `--load-extension=${extensionPath}`
      ],
      slowMo: 50
    });
    page = await browser.newPage();
    await page.goto('chrome-extension://acdcddifhaiiiagbodmcnebcgdmlgdkl/popup/popup.html');
  });

这是我 config.yml 文件中的 jobs 部分:
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Use Node.js 
      uses: actions/setup-node@v1

    - name: Install dependencies
      run: npm ci

    - name: Run Extension
      run: npm run build  

    - name: Run tests
        run: npm run test

我正在使用最新版本的Puppeteer(v3.1.0),并使用Jest(v26.0.1)运行我的测试文件。我的其他测试(仅使用Jest进行单元测试)没有任何问题。该扩展程序使用Vue.js,但我认为这并不重要。
编辑
在本地计算机上使用headless: true运行也会出错。如果是Windows 10,则可能有关。
    net::ERR_ABORTED at chrome-extension://acdcddifhaiiiagbodmcnebcgdmlgdkl/popup/popup.html

    > 23 |     await page.goto('chrome-extension://acdcddifhaiiiagbodmcnebcgdmlgdkl/popup/popup.html');

      at navigate (node_modules/puppeteer/lib/FrameManager.js:120:37)
      at FrameManager.navigateFrame (node_modules/puppeteer/lib/FrameManager.js:94:17)
      at Frame.goto (node_modules/puppeteer/lib/FrameManager.js:406:12)
      at Page.goto (node_modules/puppeteer/lib/Page.js:672:12)
      at Object.<anonymous> (tests/e2e.test.js:23:5)
        -- ASYNC --
      at Frame.<anonymous> (node_modules/puppeteer/lib/helper.js:111:15)
      at Page.goto (node_modules/puppeteer/lib/Page.js:672:49)
      at Page.goto (node_modules/puppeteer/lib/helper.js:112:23)
      at Object.<anonymous> (tests/e2e.test.js:23:16)

我猜你需要在“无头”模式下运行。 - Sirko
1个回答

2
我建议进行以下更改:
在代码中:
beforeEach (async () => {
    browser = await puppeteer.launch({
      headless: true,
      args: [
        `--no-sandbox`
        `--disable-setuid-sandbox`
        `--disable-extensions-except=${extensionPath}`,
        `--load-extension=${extensionPath}`
      ],
      slowMo: 50
    });
    page = await browser.newPage();
    await page.goto('chrome-extension://acdcddifhaiiiagbodmcnebcgdmlgdkl/popup/popup.html');
  });

根据Sirko的建议:如果您在GitHub Actions中运行puppeteer,则始终使用headless: true,因为“headful” Chromium会导致工作流程中出现问题。 --no-sandbox--disable-setuid-sandbox也可能是必需的,即使它们以前没有解决过您的问题,请不要将它们删除! 在config.yml中: 如果您想使用puppeteer 3+,则需要安装libgbm1以在unix上无头运行它(请参阅puppeteer文档)。
[...]
runs-on: ubuntu-latest
[...]
steps:
- name: install puppeteer libraries
  run: |
    sudo apt-get update
    sudo apt-get install -y libgbm1
[...]


如果您不需要所有最新的Puppeteer 3功能:您可以降级到2.1.1,它不需要额外的库。

编辑:在有关headless: true本地失败的新信息后

根据puppeteer 3.1.0的api.MD,问题很可能是:

Chrome / Chromium中的扩展目前只能在非无头模式下工作。

默认情况下,您无法在GitHub Actions headful中运行puppeteer,只能以headless方式运行。

解决方案:

可以帮助您的是名为puppeteer-headful的GitHub Action。

在按照操作页面上如何配置您的config.yml的说明后,您需要确保像这样启动puppeteer:

beforeEach (async () => {
    browser = await puppeteer.launch({
      executablePath: process.env.PUPPETEER_EXEC_PATH, // set by docker container
      headless: false,
      args: [
        `--disable-extensions-except=${extensionPath}`,
        `--load-extension=${extensionPath}`
      ],
      slowMo: 50
    });
    page = await browser.newPage();
    await page.goto('chrome-extension://acdcddifhaiiiagbodmcnebcgdmlgdkl/popup/popup.html');
  });

所以它可以在Github Actions和本地运行。

1
我忘记提到了,我尝试过 headless: true ,但它无法在我的本地机器上运行。在我的原始问题中添加了更多信息。 - Hansae Lee
我明白了。你是否已经查看了这个api.MD,pptr 3.1.0?根据这个文档,问题似乎是:“(e)xtensions in Chrome / Chromium currently only work in non-headless mode.”,而默认情况下,你无法在Github Actions headful模式下运行puppeteer,只能在headless模式下运行。可以帮助你的是这个名为puppeteer-headful的GH Action。我用这个来扩展我的答案。 - theDavidBarton
是的,我已经尝试了两种不同的方法来帮助Puppeteer运行headful模式,但都没有成功。另外一个正在开发Chrome扩展的人说他在他的应用中运行成功了,所以我认为我的问题出在别处。对于这篇文章,我认为你的回答对其他人也有好处。谢谢。 - Hansae Lee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接