Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: fail E2E test if PDF download fails in downloadSimulation() #10719

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions front/tests/pages/stdcm-page-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,40 +705,36 @@ class STDCMPage extends HomePage {
}

async downloadSimulation(downloadDir: string): Promise<void> {
try {
// Wait until there are no network requests for stability
await this.page.waitForLoadState('networkidle');
// 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$/);
// 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!);
const downloadPath = path.join(downloadDir, suggestedFilename!);

await fs.promises.mkdir(downloadDir, { recursive: true });
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');
}
// 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 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));
});
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));
// 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);
}
logger.info(`The PDF was successfully downloaded to: ${downloadPath}`);
}

async startNewQuery() {
Expand Down
Loading