Skip to content
Open
Changes from 2 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
66 changes: 53 additions & 13 deletions tests/playwright/page/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, Page } from "@playwright/test";
import * as fs from "fs";
import AdvancedSearch from "@hipanel-core/helper/AdvancedSearch";
import { Alert, BulkActions, ColumnFilters } from "@hipanel-core/shared/ui/components";
import {Download} from "@playwright/test";

export default class Index {
advancedSearch: AdvancedSearch;
Expand Down Expand Up @@ -212,30 +213,69 @@ export default class Index {
}
}

private async checkDownloadByLinkName(linkName: string) {
const linkLocator = this.page.locator(`a:has-text("${linkName}")`);
private async checkDownloadByLinkName(linkName: string): Promise<void> {
const download = await this.triggerDownloadByLinkName(linkName);
const downloadPath = this.buildDownloadPath(download);

await linkLocator.highlight();
await this.saveDownload(download, downloadPath);

await this.waitForFile(downloadPath);
}

private async triggerDownloadByLinkName(linkName: string) {
const link = this.page.locator(`a:has-text("${linkName}")`);
await link.highlight();

const [download] = await Promise.all([
this.page.waitForEvent("download"),
linkLocator.click(),
link.click(),
]);

const downloadPath = `runtime/${download.suggestedFilename()}`;
await download.saveAs(downloadPath);
return download;
}

private buildDownloadPath(download: Download): string {
return `runtime/${download.suggestedFilename()}`;
}

const fileExissts = fs.existsSync(downloadPath);
const stats = fs.statSync(downloadPath);
private async saveDownload(download: Download, filePath: string): Promise<void> {
await download.saveAs(filePath);

const path = await download.path();
console.log("Temporary download path:", await download.path());
}

console.log("Temporary path:", path);
private async waitForFile(filePath: string, timeoutMs = 5000): Promise<void> {
const pollingIntervalMs = 100;
const deadline = Date.now() + timeoutMs;

expect(fileExissts, `File not found at path: ${downloadPath}`).toBeTruthy();
while (Date.now() < deadline) {
if (this.isFileReady(filePath)) {
return;
}

console.log(`File ${path}: ${stats.size} bytes`);
console.log(`Waiting for file to be ready: ${filePath}`);
await this.delay(pollingIntervalMs);
}

throw new Error(`Timed out waiting for file: ${filePath}`);
}

private isFileReady(filePath: string): boolean {
if (!fs.existsSync(filePath)) {
return false;
}

const { size } = fs.statSync(filePath);

if (size > 0) {
console.log(`Downloaded file ready: ${filePath} (${size} bytes)`);
return true;
}

return false;
}

expect(stats.size).toBeGreaterThan(0);
private delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
}