最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Playwright always showing "canceled" when calling download.saveAs - only in github action - Stack Overflow

programmeradmin0浏览0评论

My test code looks like

test("can download the excel file", async ({ page }) => {
  await page.goto("/download-page");
  const downloadPromise = page.waitForEvent("download");
  await page.getByRole("button", { name: "Download", exact: true }).click();
  const download = await downloadPromise;
  await download.saveAs(path.join(__dirname, "downloads", download.suggestedFilename()));

When executing this on my machine it works perfectly fine. However, when executing in in a github action it fails with the error

    Error: download.saveAs: canceled

      114 |   await page.getByRole("button", { name: "Download", exact: true }).click();
      115 |   const download = await downloadPromise;
    > 116 |   await download.saveAs(path.join(__dirname, "downloads", download.suggestedFilename()));
          |                  ^

The github action is using the following image: mcr.microsoft/playwright:v1.49.0-jammy

The code executed when the "Download Button" is clicked looks liek

const link = document.createElement("a");
link.href = `/path-to-my-file-xlsx`;
link.download = `template.xlsx`;
link.click();

I'm running out of ideas as I already checked tracer etc. - I don't seem to get a more detailed error message than this.

UPDATE:

Debug logs show the following output

  pw:api => download.saveAs started +1ms
  pw:api => download.saveAs started +1ms

  pw:api <= download.saveAs failed +5ms
  pw:api <= download.saveAs failed +5ms

As it cancels already after 5ms I'd suppose the browser actually prevents the download ... But I cannot find out why.

My test code looks like

test("can download the excel file", async ({ page }) => {
  await page.goto("/download-page");
  const downloadPromise = page.waitForEvent("download");
  await page.getByRole("button", { name: "Download", exact: true }).click();
  const download = await downloadPromise;
  await download.saveAs(path.join(__dirname, "downloads", download.suggestedFilename()));

When executing this on my machine it works perfectly fine. However, when executing in in a github action it fails with the error

    Error: download.saveAs: canceled

      114 |   await page.getByRole("button", { name: "Download", exact: true }).click();
      115 |   const download = await downloadPromise;
    > 116 |   await download.saveAs(path.join(__dirname, "downloads", download.suggestedFilename()));
          |                  ^

The github action is using the following image: mcr.microsoft/playwright:v1.49.0-jammy

The code executed when the "Download Button" is clicked looks liek

const link = document.createElement("a");
link.href = `/path-to-my-file-xlsx`;
link.download = `template.xlsx`;
link.click();

I'm running out of ideas as I already checked tracer etc. - I don't seem to get a more detailed error message than this.

UPDATE:

Debug logs show the following output

  pw:api => download.saveAs started +1ms
  pw:api => download.saveAs started +1ms

  pw:api <= download.saveAs failed +5ms
  pw:api <= download.saveAs failed +5ms

As it cancels already after 5ms I'd suppose the browser actually prevents the download ... But I cannot find out why.

Share Improve this question edited Nov 21, 2024 at 7:25 Frnak asked Nov 20, 2024 at 14:56 FrnakFrnak 6,8226 gold badges37 silver badges74 bronze badges 6
  • 1 Is it running in headless mode in the CI? – HemChe Commented Nov 20, 2024 at 16:34
  • Yes - headless is set to true explicitly. – Frnak Commented Nov 20, 2024 at 17:34
  • Does it fail the same way when you run the test in headless mode in your local? – HemChe Commented Nov 20, 2024 at 17:36
  • No, it works in headless mode locally. – Frnak Commented Nov 20, 2024 at 18:01
  • Can you enable debug logs like this and see you get any clue from the logs DEBUG=pw:api npx playwright test – HemChe Commented Nov 20, 2024 at 19:03
 |  Show 1 more comment

1 Answer 1

Reset to default 1

I finally found out that the issue is not within playwright but my test setup. The download did not work at all (the backend replied with an error).

In my case the backend generates an excel that should be downloaded - if the generation fails the error

Error: download.saveAs: canceled

Will be shown.

I'll not delete the question as it might help others to find the same issue.

发布评论

评论列表(0)

  1. 暂无评论