1

我试图弄清楚如何在 docker 容器中运行无头 Chrome。然后我找到了这个。但是现在我不知道如何在那个容器中运行我的测试。

有人可以给我一个大致的方向,我应该在哪里挖掘,我尝试浏览Pupeeteer的文档,但找不到任何东西。也许在野外有一个最小的例子,我可以使用 Karma 或其他任何东西在容器中运行测试并记录结果。

请注意,尽管我想在容器之外编译/捆绑 javascript,并使用它来执行其中的已编译/捆绑测试。

也许稍后我想使用相同的方法来运行我的验收测试,但这次是通过在外部运行 Web 服务器,可能在一个单独的容器中。

我的最终目标是能够运行一堆用 Clojurescript 编写的测试,但我认为虽然还没有人做过类似的事情。也许有人有。

4

1 回答 1

0

我想我已经草拟了一个游戏计划:

  • 首先,需要运行容器:

    docker run -it --rm -p=0.0.0.0:9222:9222 --name=chrome-headless \
    -v /tmp/chromedata/:/data alpeware/chrome-headless-trunk
    
  • 现在,当 Chrome 运行时,您可以通过打开http://localhost:9222来检查。您应该在那里看到一个选项卡。我们需要找到该选项卡的 websocketUrl,运行:

    curl http://localhost:9222/json
    
    # should get you something like this:
    
    [{"description": "",
      "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/2f428ea1-7229-484c-b7c7-57ef2f098ffe",
      "id": "2f428ea1-7229-484c-b7c7-57ef2f098ffe",
      "title": "Google",
      "type": "page",
      "url": "https://www.google.com/",
      "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/2f428ea1-7229-484c-b7c7-57ef2f098ffe"}]
    
  • 现在你可以使用 Puppeteer 连接并做一些疯狂的事情:

    const puppeteer = require('puppeteer');
    
    puppeteer.connect({
      browserWSEndpoint: "ws://localhost:9222/devtools/page/2f428ea1-7229-484c-b7c7-57ef2f098ffe"
    }).then (async browser => {
      const page = await browser.newPage();
      await page.goto('https://www.google.com');
      ;; you just opened another tab
    });
    

这一切都很好,现在我要用这些砖块“盖房子”

于 2017-08-29T06:19:01.487 回答