TestCafe:测试在新选项卡中打开 PDF 的页面

TestCafe: Testing a Page Opening a PDF in a New Tab

我正在测试的 Web 应用程序的一部分是单击一个按钮,该按钮通常会在新选项卡中打开 PDF。这使用户能够在应用程序中继续前进。 PDF可以忽略。

目前,我的策略是单击按钮,然后使用 ClientFunction 导航回来。 TestCafe 在点击按钮时成功打开 PDF,但在同一个选项卡中打开,然后测试卡住了。向后导航会更改 URL,但 PDF 仍会显示。


    const goBack = ClientFunction(() => window.history.back())

    await this.t.click(this.button)
    await this.t.wait(10000)

    await goBack()

TestCafe 目前是否能够绕过这个问题,因为我实际上不需要对 PDF 做任何事情?

TestCafe 允许测试 html 页面,但不能测试 PDF 文件。因此,您可以将生成的 link 作为字符串检查到 PDF 文件中,而无需实际遵循此 link。例如:

const overrideWindowOpen = ClientFunction(() => {
    window.open = function (url) {
        window.__lastWindowOpenUrl = url;
    };
});
const getResultUrl = ClientFunction(() => window.__lastWindowOpenUrl);

await overrideWindowOpen();
await t
    .click(this.button)
    .expect(getResultUrl()).eql('http://example.com/path/to/PDF/93023813-0984-1');

另请参阅:Multiple Browser Windows