Skip to content

Commit 0877655

Browse files
authored
test: navigate to download url (#558)
1 parent 70b9e2e commit 0877655

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

playwright/src/test/java/com/microsoft/playwright/TestDownload.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,42 @@ void addRoutes() {
7373

7474
}
7575

76+
@Test
77+
void shouldReportDownloadWhenNavigationTurnsIntoDownload() throws IOException {
78+
Page page = browser.newPage(new Browser.NewPageOptions().setAcceptDownloads(true));
79+
80+
Response[] response = new Response[]{null};
81+
PlaywrightException[] error = new PlaywrightException[]{null};
82+
Download download = page.waitForDownload(() -> {
83+
try {
84+
response[0] = page.navigate(server.PREFIX + "/download");
85+
} catch (PlaywrightException e) {
86+
error[0] = e;
87+
}
88+
});
89+
90+
assertEquals(page, download.page());
91+
assertEquals(server.PREFIX + "/download", download.url());
92+
Path path = download.path();
93+
assertTrue(Files.exists(path));
94+
byte[] bytes = readAllBytes(path);
95+
assertEquals("Hello world", new String(bytes, UTF_8));
96+
if (isChromium()) {
97+
assertNotNull(error[0]);
98+
assertTrue(error[0].getMessage().contains("net::ERR_ABORTED"));
99+
assertEquals("about:blank", page.url());
100+
} else if (isWebKit()) {
101+
assertNotNull(error[0]);
102+
assertTrue(error[0].getMessage().contains("Download is starting"));
103+
assertEquals("about:blank", page.url());
104+
} else {
105+
assertNotNull(response[0]);
106+
assertEquals(200, response[0].status());
107+
assertEquals(server.PREFIX + "/download", page.url());
108+
}
109+
page.close();
110+
}
111+
76112
@Test
77113
void shouldReportDownloadsWithAcceptDownloadsFalse() {
78114
page.setContent("<a href='" + server.PREFIX + "/downloadWithFilename'>download</a>");

0 commit comments

Comments
 (0)