Skip to content

Commit

Permalink
front: use Playwright download event for simulation PDF
Browse files Browse the repository at this point in the history
This is less fragile and also ensures that clicking results in the
expected behavior (a file being downloaded).

Signed-off-by: Simon Ser <[email protected]>
  • Loading branch information
emersion committed Mar 4, 2025
1 parent 1a626a3 commit 4742906
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions front/tests/pages/stdcm/simulation-results-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class SimulationResultPage extends STDCMPage {

private readonly downloadSimulationButton: Locator;

private readonly downloadLink: Locator;

private readonly startNewQueryButton: Locator;

private readonly startNewQueryWithDataButton: Locator;
Expand All @@ -50,7 +48,6 @@ class SimulationResultPage extends STDCMPage {
this.allViasButton = page.getByTestId('all-vias-button');
this.retainSimulationButton = page.getByTestId('retain-simulation-button');
this.downloadSimulationButton = page.locator('.download-simulation a[download]');
this.downloadLink = page.locator('.download-simulation a');
this.startNewQueryButton = page.getByTestId('start-new-query-button');
this.startNewQueryWithDataButton = page.getByTestId('start-new-query-with-data-button');
this.simulationList = page.locator('.stdcm-results .simulation-list');
Expand Down Expand Up @@ -122,35 +119,20 @@ class SimulationResultPage extends STDCMPage {
// Wait until there are no network requests for stability
await this.page.waitForLoadState('networkidle');

// Get the download link element and suggested filename
const suggestedFilename = await this.downloadLink.getAttribute('download');
expect(suggestedFilename).toMatch(/^Stdcm.*\.pdf$/);

const downloadPath = path.join(downloadDir, suggestedFilename!);

await fs.promises.mkdir(downloadDir, { recursive: true });

// react-pdf will create an <a> element without an href attribute while
// it's generating the PDF file
await expect(this.downloadSimulationButton).toHaveAttribute('href');

// Get the file content from the `blob:` URL
const fileContent = await this.downloadSimulationButton.evaluate(async (el) => {
if (!(el instanceof HTMLAnchorElement)) {
throw new Error('Element is not an anchor tag');
}
const downloadPromise = this.page.waitForEvent('download');
await this.downloadSimulationButton.click();
const download = await downloadPromise;

const response = await fetch(el.href);
if (!response.ok) {
throw new Error(`Failed to fetch the blob: ${response.status} ${response.statusText}`);
}

const buffer = await response.arrayBuffer();
return Array.from(new Uint8Array(buffer));
});
expect(download.suggestedFilename()).toMatch(/^Stdcm.*\.pdf$/);

// Write the file to the local file system
await fs.promises.writeFile(downloadPath, Buffer.from(fileContent));
const downloadPath = path.join(downloadDir, download.suggestedFilename());
await download.saveAs(downloadPath);

logger.info(`The PDF was successfully downloaded to: ${downloadPath}`);
}
Expand Down

0 comments on commit 4742906

Please sign in to comment.