Skip to content

Instantly share code, notes, and snippets.

@AlexKamaev
Created July 18, 2019 08:35
Show Gist options
  • Save AlexKamaev/8c1eb8a5fb638fa366b44447f6d7c5a4 to your computer and use it in GitHub Desktop.
Save AlexKamaev/8c1eb8a5fb638fa366b44447f6d7c5a4 to your computer and use it in GitHub Desktop.
Enable file downloading for Headless Chrome using TestCafe internal API
import path from 'path';
import { Selector } from 'testcafe';
async function enableDownloadForHeadlessChrome (t) {
const browserConnection = t.testRun.browserConnection;
const client = browserConnection.provider.plugin.openedBrowsers[browserConnection.id].client;
const { Network, Page } = client;
await Promise.all([
Network.enable(),
Page.enable()
]);
Network.requestWillBeSent((param) => {
// console.log("Network.requestWillBeSent: " + JSON.stringify(param));
});
Network.responseReceived((param) => {
// console.log("Network.responseReceived: " + JSON.stringify(param));
});
await Page.setDownloadBehavior({
behavior: 'allow',
downloadPath: path.resolve(__dirname, 'downloaded')
});
}
fixture`test`
.page`https://github.com/DevExpress/testcafe/`;
test('test', async t => {
await enableDownloadForHeadlessChrome(t);
await t.click(Selector('summary').withText('Clone or download'));
await t.click(Selector('a').withText('Download ZIP'));
//NOTE: please replace with string with your function which detects that the file exists
await t.wait(60000);
})
@danielHollander
Copy link

await browser.browserClient.getActiveClient();
returns undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment