Skip to content

Commit

Permalink
tests(downloads): add a test for Blob downloads (#1936) (#1939)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwoll authored Apr 23, 2020
1 parent 471ccc7 commit dc23c56
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
29 changes: 29 additions & 0 deletions test/assets/download-blob.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>Blob Download Example</title>
</head>
<body>
<script>
const download = (data, filename) => {
const a = document.createElement("a");
a.style = "display: none";
document.body.appendChild(a);
a.style = "display: none";

const blob = new Blob([data], { type: "octet/stream" });
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
};

const downloadIt = () => {
download("Hello world", "example.txt");
}
</script>
<a onclick="javascipt:downloadIt();">Download</a>
</body>
</html>
19 changes: 16 additions & 3 deletions test/download.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,29 @@ describe('Download', function() {
expect(fs.readFileSync(path).toString()).toBe('Hello world');
await page.close();
});
it(`should report download path within page.on('download', …) handler`, async({browser, server}) => {
it(`should report download path within page.on('download', …) handler for Files`, async({browser, server}) => {
const page = await browser.newPage({ acceptDownloads: true });
const onDownloadPathPath = new Promise((res) => {
const onDownloadPath = new Promise((res) => {
page.on('download', dl => {
dl.path().then(res);
});
});
await page.setContent(`<a href="${server.PREFIX}/download">download</a>`);
await page.click('a');
const path = await onDownloadPathPath;
const path = await onDownloadPath;
expect(fs.readFileSync(path).toString()).toBe('Hello world');
await page.close();
})
it.fail(FFOX || WEBKIT)(`should report download path within page.on('download', …) handler for Blobs`, async({browser, server}) => {
const page = await browser.newPage({ acceptDownloads: true });
const onDownloadPath = new Promise((res) => {
page.on('download', dl => {
dl.path().then(res);
});
});
await page.goto(server.PREFIX + '/download-blob.html');
await page.click('a');
const path = await onDownloadPath;
expect(fs.readFileSync(path).toString()).toBe('Hello world');
await page.close();
})
Expand Down

0 comments on commit dc23c56

Please sign in to comment.