tests(downloads): add a test for Blob downloads (#1936) (#1939)

This commit is contained in:
Ross Wollman 2020-04-23 13:02:37 -07:00 committed by GitHub
parent 471ccc72d3
commit dc23c567c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 3 deletions

View File

@ -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>

View File

@ -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();
})