Skip to main content

Download and verify a PDF

Click a download link, wait for the file to land, and assert it's a real PDF. DownloadManager polls the download directory and ignores partial (.crdownload) files until they're complete — no Thread.sleep().

InvoiceTest.java
import io.testfly.browser.DownloadManager;
import io.testfly.test.BaseTest;
import java.io.File;
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class InvoiceTest extends BaseTest {

@Test
public void downloadsInvoicePdf() {
open("/invoices/123");
find("#download-pdf").click();

// Dynamic filename (timestamped) → wait for any file, up to 15s:
File pdf = DownloadManager.waitForAnyFile(15);

softAssert().that(pdf.getName().endsWith(".pdf"), "Downloaded file should be a PDF");
softAssert().that(pdf.length() > 0, "Downloaded file should not be empty");
}
}

Known filename? Wait for it by name instead:

File pdf = DownloadManager.waitForFile("invoice-123.pdf", 15);

Set the download directory in config (defaults to ./target/downloads):

testfly.yml
browser:
downloadDir: ./target/downloads
Clean state between tests

Call DownloadManager.clearDownloads() in @BeforeMethod so a stale file from a previous run can't satisfy the wait.

Deeper reference: DownloadManager — waiting strategies, partial-download handling, and cleanup.