Skip to content

Commit dc23c56

Browse files
authored
tests(downloads): add a test for Blob downloads (#1936) (#1939)
1 parent 471ccc7 commit dc23c56

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

test/assets/download-blob.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Blob Download Example</title>
5+
</head>
6+
<body>
7+
<script>
8+
const download = (data, filename) => {
9+
const a = document.createElement("a");
10+
a.style = "display: none";
11+
document.body.appendChild(a);
12+
a.style = "display: none";
13+
14+
const blob = new Blob([data], { type: "octet/stream" });
15+
const url = window.URL.createObjectURL(blob);
16+
a.href = url;
17+
a.download = filename;
18+
a.click();
19+
window.URL.revokeObjectURL(url);
20+
document.body.removeChild(a);
21+
};
22+
23+
const downloadIt = () => {
24+
download("Hello world", "example.txt");
25+
}
26+
</script>
27+
<a onclick="javascipt:downloadIt();">Download</a>
28+
</body>
29+
</html>

test/download.spec.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,29 @@ describe('Download', function() {
7070
expect(fs.readFileSync(path).toString()).toBe('Hello world');
7171
await page.close();
7272
});
73-
it(`should report download path within page.on('download', …) handler`, async({browser, server}) => {
73+
it(`should report download path within page.on('download', …) handler for Files`, async({browser, server}) => {
7474
const page = await browser.newPage({ acceptDownloads: true });
75-
const onDownloadPathPath = new Promise((res) => {
75+
const onDownloadPath = new Promise((res) => {
7676
page.on('download', dl => {
7777
dl.path().then(res);
7878
});
7979
});
8080
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
8181
await page.click('a');
82-
const path = await onDownloadPathPath;
82+
const path = await onDownloadPath;
83+
expect(fs.readFileSync(path).toString()).toBe('Hello world');
84+
await page.close();
85+
})
86+
it.fail(FFOX || WEBKIT)(`should report download path within page.on('download', …) handler for Blobs`, async({browser, server}) => {
87+
const page = await browser.newPage({ acceptDownloads: true });
88+
const onDownloadPath = new Promise((res) => {
89+
page.on('download', dl => {
90+
dl.path().then(res);
91+
});
92+
});
93+
await page.goto(server.PREFIX + '/download-blob.html');
94+
await page.click('a');
95+
const path = await onDownloadPath;
8396
expect(fs.readFileSync(path).toString()).toBe('Hello world');
8497
await page.close();
8598
})

0 commit comments

Comments
 (0)