-
-
Save AlexKamaev/8c1eb8a5fb638fa366b44447f6d7c5a4 to your computer and use it in GitHub Desktop.
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); | |
}) |
I checked this code using [email protected]
, 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.
@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.
@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();
Awesome, thank you @AlexKamaev
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
@LathaSan
As far as I understand this functionality is integrated in TestCafe: https://github.com/DevExpress/testcafe/blob/d3df85f2d95aa67792044e72e8f20f7e1a0504fb/src/browser/provider/built-in/dedicated/chrome/cdp-client/index.ts#L108
So, it's better to ask questions in the official repository: https://github.com/DevExpress/testcafe/issues/new/choose
await browser.browserClient.getActiveClient();
returns undefined.
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.