将 Testcafe 连接到 AWS Devicefarm

问题描述

我们最近决定将 E2E 包含在我们的前端管道中,为此我们使用了 testcafe,由于我们使用 AWS 作为我们的 SaaS,我们被要求使用 Devicefarm 进行远程测试,而我正面临连接问题他们。我基于 selenium 实现和 testcafe 文档,但似乎无法在它们之间建立连接,有人知道为什么吗?

我的代码

const AWS = require('aws-sdk');
const createtestcafe = require('testcafe');

const PROJECT_ARN = "arn:aws:devicefarm:us-west-2:XXXXXXXX:testgrid-project:XXXXXX-XXXX-XXXX-XXXX-XXXXXXXX";
const devicefarm = new AWS.DeviceFarm({ region: "us-west-2",credentials: AWS.config.credentials });

(async () => {
    const testGridUrlResult = await devicefarm.createTestGridUrl({
        projectArn: PROJECT_ARN,expiresInSeconds: 6000
    }).promise();

    const url = new URL(testGridUrlResult.url || '');
    const testcafe         = await createtestcafe(url.host,443);
    const runner           = testcafe.createRunner();
    const remoteConnection = await testcafe.createbrowserConnection();

    remoteConnection.once('ready',async () => {
        await runner
            .src(['./load-mfc.ts'])
            .browsers('devicefarm:firefox')
            .run();

        await testcafe.close();
    });

})().catch((e) => console.error(e));

解决方法

根据有关 CreateTestGridUrl 方法的 AWS Device Farm 文档:

创建可以传递给 Selenium RemoteWebDriver 构造函数的签名的短期 URL。

此 URL 无法传递到 hostname 方法的 createTestCafe 属性,因为 TestCafe 未实现 WebDriver 协议。 TestCafe 的工作方式与 Selenium 不同。使用 TestCafe 创建 remote browser connection 后,您应该将远程浏览器导航到 browserConnection.url URL 以连接到 TestCafe 服务器实例并在此浏览器中开始测试执行。

由于 AWS Device Farm 服务使用 WebDriver 协议来操作远程浏览器,因此您需要为 TestCafe 编写自定义 Browser Provider Plugin。在 TestCafe 文档中阅读有关此主题的更多信息并了解如何在 BrowserStack provider 中实现类似的内容可能会有所帮助。