TestCafe可用于SAM Local,但不适用于SAM部署后

时间:2019-12-03 13:55:07

标签: amazon-web-services testing aws-lambda puppeteer testcafe

我目前正在尝试设置应该执行简单TestCafe测试的AWS Lambda(nodejs10.x)函数。

如果我使用sam local invoke --no-event在本地运行Lambda,它将执行得很好:

  

2019-12-03T13:39:46.345Z 7b906b79-d7e5-1aa6-edb7-3e749d4e4b08
  INFO hello world✓我的第一个测试1通过(1s)   2019-12-03T13:39:46.578Z 7b906b79-d7e5-1aa6-edb7-3e749d4e4b08
  INFO测试失败:0

sam buildsam deploy部署它后,它停止工作。它只是在AWS中引发以下错误:

{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "Error: Page crashed!",
  "trace": [
    "Runtime.UnhandledPromiseRejection: Error: Page crashed!",
    "    at process.on (/var/runtime/index.js:37:15)",
    "    at process.emit (events.js:203:15)",
    "    at process.EventEmitter.emit (domain.js:448:20)",
    "    at emitPromiseRejectionWarnings (internal/process/promises.js:140:18)",
    "    at process._tickCallback (internal/process/next_tick.js:69:34)"
  ]
}

我的lambda处理程序如下所示:

const createTestCafe = require("testcafe");
const chromium = require("chrome-aws-lambda");
let testcafe = null;

exports.lambdaHandler = async (event, context) => {
  const executablePath = await chromium.executablePath;

  await createTestCafe()
    .then(tc => {
      testcafe = tc;
      const runner = testcafe.createRunner();

      return runner
        .src("sample-fixture.js")
        .browsers(
          "puppeteer-core:launch?arg=--no-sandbox&arg=--disable-gpu&arg=--disable-setuid-sandbox&path=" + executablePath
        )
        .run({
          skipJsErrors: true,
          selectorTimeout: 50000
        });
    })
    .then(failedCount => {
      console.log("Tests failed: " + failedCount);
      testcafe.close();
    });

  return {
    statusCode: 200
  };
};

我的sample-fixture.js看起来像这样:

fixture `Getting Started`
    .page `http://devexpress.github.io/testcafe/example`;

test('My first test', async t => {
  console.log('hello world');
});

我正在使用以下依赖项:

  • “ testcafe”:“ ^ 1.7.0”

  • “ chrome-aws-lambda”:“ ^ 2.0.1”

  • “ testcafe-browser-provider-puppeteer-core”:“ ^ 1.1.0”

有人知道为什么我的Lambda函数可以在我的机器上本地工作但不能在AWS上工作吗?

2 个答案:

答案 0 :(得分:1)

我目前仅凭此信息无法说出任何准确的信息。 我建议您尝试以下操作:

答案 1 :(得分:0)

因此,我已经能够在AWS Lambda中运行testcafe。对我来说,关键是利用Lambda图层。 (https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

testcafe的问题在于它是一个庞大的软件包,它和chrome-aws-lambda都应部署在Layers中,因为您可能会超出捆绑包的大小限制。

此外,由于testcafe在babel中编译文件,因此Lambda上的速度可能会出现问题。如果您的文件已被编译,则可能会为执行增加不必要的时间。

希望这会有所帮助!