Skip to content

Commit

Permalink
front: fail E2E test if PDF download fails in downloadSimulation()
Browse files Browse the repository at this point in the history
downloadSimulation() was logging an error and silently continuing
the tests on failure. CI wouldn't complain in that case.

Don't catch the error to ensure that we notice when the function
fails.

Signed-off-by: Simon Ser <[email protected]>
  • Loading branch information
emersion committed Feb 7, 2025
1 parent 14fa21b commit 908be70
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions front/tests/pages/stdcm-page-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,43 +712,39 @@ class STDCMPage extends HomePage {
}

async downloadSimulation(browserName: string, isLinkedTrain: boolean = false): Promise<void> {
try {
// 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 downloadDir = isLinkedTrain
? path.join('./tests/stdcm-results/linkedTrain', browserName)
: path.join('./tests/stdcm-results', browserName);
const downloadPath = path.join(downloadDir, suggestedFilename!);

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

// 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 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));
});

// Write the file to the local file system
await fs.promises.writeFile(downloadPath, Buffer.from(fileContent));

logger.info(`The PDF was successfully downloaded to: ${downloadPath}`);
} catch (error) {
logger.error('Failed to download simulation', error);
}
// 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 downloadDir = isLinkedTrain
? path.join('./tests/stdcm-results/linkedTrain', browserName)
: path.join('./tests/stdcm-results', browserName);
const downloadPath = path.join(downloadDir, suggestedFilename!);

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

// 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 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));
});

// Write the file to the local file system
await fs.promises.writeFile(downloadPath, Buffer.from(fileContent));

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

async startNewQuery() {
Expand Down

0 comments on commit 908be70

Please sign in to comment.