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);
})
@lpelypenko
Copy link

Can you please share which version of the testcafe you are using?
When I try this code on the latest testcafe version I am getting an error that Page is undefined.

@AlexKamaev
Copy link
Author

I checked this code using testcafe@1.8.8, and it works as expected. I ran the test with the following command: testcafe chrome:headless test.js.

It seems that GitHub changed its markup, so I modified the following code:

await t.click(Selector('summary').withText('Clone or download'));

Now, it looks as follows:

await t.click(Selector('summary').withText('Code'));

I this does not help, feel free to create a separate issue in the TestCafe repository using the following form: https://github.com/DevExpress/testcafe/issues/new?template=bug-report.md. Please share your example with us to demonstrate the issue.

@dlangerenken
Copy link

@AlexKamaev, I'm also seeing the issue described by @Ipelypenko:

TypeError: Cannot destructure property 'Network' of 'client' as it

[2021-02-02T05:01:59.033Z]       is undefined.

@AlexKamaev
Copy link
Author

@dlangerenken
We modified the internal API, so the described approach should be modified.
Check the following code to get the client instance:

    const browserConnection = t.testRun.browserConnection;
    const browser           = browserConnection.provider.plugin.openedBrowsers[browserConnection.id];
    const client            = await browser.browserClient.getActiveClient();

@dlangerenken
Copy link

Awesome, thank you @AlexKamaev

@LathaSan
Copy link

LathaSan commented Jul 3, 2021

Hi @AlexKamaev

Files are not downloading when testcafe scripts are running from team city using headless chrome. In local it is working fine.
Also need to set up the custom download path, can you guide me on this please.
Team City Version: 1.11.0

@AlexKamaev
Copy link
Author

@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