You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importCDPfrom'chrome-remote-interface';importfsfrom'fs';exportinterfaceTakeHeapSnapshotType{filePath: string;beforeTakeCallback?: ()=>void;afterTakeCallback?: ()=>void;}letport=0;letclient: CDP.Client|null=null;constsetRdpPortWhenBrowserLaunch=(launchOptionsOrArgs: Cypress.BrowserLaunchOptions)=>{constargs=Array.isArray(launchOptionsOrArgs) ? launchOptionsOrArgs : launchOptionsOrArgs.args;constensureRdpPort=(args: string[]|(Cypress.BrowserLaunchOptions&any[]))=>{constexisting=args.find((arg)=>arg.slice(0,23)==='--remote-debugging-port');if(existing){returnNumber(existing.split('=')[1]);}constport=40000+Math.round(Math.random()*25000);args.push(`--remote-debugging-port=${port}`);returnport;};port=ensureRdpPort(args);console.log('Ensure remote debugging port %d',port);};constinitCDPClient=async()=>{if(!port){thrownewError('Please set the remote debugging port first!');}if(!client){console.log('generating client, port:',port);client=awaitCDP({
port,});console.log('client',client);}};consttakeHeapSnapshot=async(opts: TakeHeapSnapshotType)=>{console.log('start take snapshot');if(!client){console.warn('client not init');thrownewError('Please init the cdp client first!');}const{ filePath, beforeTakeCallback =null, afterTakeCallback =null}=opts;if(beforeTakeCallback){beforeTakeCallback();}constwriteStream=fs.createWriteStream(filePath,{encoding: 'utf-8'});constdataHandler=(data: {chunk: string})=>{writeStream.write(data.chunk);};constprogressHander=(data: {done: number;total: number;finished: boolean})=>{constpercent=((100*data.done)/data.total)|0;console.log(`heap snapshot ${percent}% complete`);};client.on('HeapProfiler.addHeapSnapshotChunk',dataHandler);client.on('HeapProfiler.reportHeapSnapshotProgress',progressHanderasSafeAny);awaitclient.send('HeapProfiler.takeHeapSnapshot',{reportProgress: true,captureNumericValue: true,});writeStream.end();if(afterTakeCallback){afterTakeCallback();}};exportconstcdpPlugin=()=>{return{
setRdpPortWhenBrowserLaunch,
initCDPClient,
takeHeapSnapshot,};};
Cypress test
describe('MemLab Test',()=>{it('should take a snapshot',()=>{cy.task('takeHeapSnapshot',{filePath: path.join(__dirname,'./snapshots/s1.heapsnapshot'),});});});
The text was updated successfully, but these errors were encountered:
Environment
Is the client running in a container? YES
Description
Trying to use CDP to grab heap snapshots with Cypress.
Cypress fails to run the CDP init function:
Example
cypress.config.ts
cdpPlugin
Cypress test
The text was updated successfully, but these errors were encountered: