@@ -18,11 +18,9 @@ import utils from './utils'
1818import type { Browser } from './types'
1919import { BrowserCriClient } from './browser-cri-client'
2020import type { LaunchedBrowser } from '@packages/launcher/lib/browsers'
21- import type { CRIWrapper } from './cri-client'
21+ import type { CriClient } from './cri-client'
2222import type { Automation } from '../automation'
23-
24- // TODO: this is defined in `cypress-npm-api` but there is currently no way to get there
25- type CypressConfiguration = any
23+ import type { BrowserLaunchOpts , BrowserNewTabOpts } from '@packages/types'
2624
2725const debug = debugModule ( 'cypress:server:browsers:chrome' )
2826
@@ -123,7 +121,7 @@ const DEFAULT_ARGS = [
123121 '--disable-dev-shm-usage' ,
124122]
125123
126- let browserCriClient
124+ let browserCriClient : BrowserCriClient | undefined
127125
128126/**
129127 * Reads all known preference files (CHROME_PREFERENCE_PATHS) from disk and retur
@@ -320,15 +318,15 @@ const _handleDownloads = async function (client, dir, automation) {
320318let frameTree
321319let gettingFrameTree
322320
323- const onReconnect = ( client : CRIWrapper . Client ) => {
321+ const onReconnect = ( client : CriClient ) => {
324322 // if the client disconnects (e.g. due to a computer sleeping), update
325323 // the frame tree on reconnect in cases there were changes while
326324 // the client was disconnected
327325 return _updateFrameTree ( client , 'onReconnect' ) ( )
328326}
329327
330328// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
331- const _updateFrameTree = ( client : CRIWrapper . Client , eventName ) => async ( ) => {
329+ const _updateFrameTree = ( client : CriClient , eventName ) => async ( ) => {
332330 debug ( `update frame tree for ${ eventName } ` )
333331
334332 gettingFrameTree = new Promise < void > ( async ( resolve ) => {
@@ -433,8 +431,8 @@ const _handlePausedRequests = async (client) => {
433431 } )
434432}
435433
436- const _setAutomation = async ( client : CRIWrapper . Client , automation : Automation , resetBrowserTargets : ( shouldKeepTabOpen : boolean ) => Promise < void > , options : CypressConfiguration = { } ) => {
437- const cdpAutomation = await CdpAutomation . create ( client . send , client . on , resetBrowserTargets , automation , options . experimentalSessionAndOrigin )
434+ const _setAutomation = async ( client : CriClient , automation : Automation , resetBrowserTargets : ( shouldKeepTabOpen : boolean ) => Promise < void > , options : BrowserLaunchOpts ) => {
435+ const cdpAutomation = await CdpAutomation . create ( client . send , client . on , resetBrowserTargets , automation , ! ! options . experimentalSessionAndOrigin )
438436
439437 return automation . use ( cdpAutomation )
440438}
@@ -490,7 +488,7 @@ export = {
490488 return extensionDest
491489 } ,
492490
493- _getArgs ( browser : Browser , options : CypressConfiguration , port : string ) {
491+ _getArgs ( browser : Browser , options : BrowserLaunchOpts , port : string ) {
494492 const args = ( [ ] as string [ ] ) . concat ( DEFAULT_ARGS )
495493
496494 if ( os . platform ( ) === 'linux' ) {
@@ -551,43 +549,60 @@ export = {
551549 return args
552550 } ,
553551
554- async connectToNewSpec ( browser : Browser , options : CypressConfiguration = { } , automation : Automation ) {
552+ async connectToNewSpec ( browser : Browser , options : BrowserNewTabOpts , automation : Automation ) {
555553 debug ( 'connecting to new chrome tab in existing instance with url and debugging port' , { url : options . url } )
556554
557555 const browserCriClient = this . _getBrowserCriClient ( )
556+
557+ if ( ! browserCriClient ) throw new Error ( 'Missing browserCriClient in connectToNewSpec' )
558+
558559 const pageCriClient = browserCriClient . currentlyAttachedTarget
559560
561+ if ( ! pageCriClient ) throw new Error ( 'Missing pageCriClient in connectToNewSpec' )
562+
563+ if ( ! options . url ) throw new Error ( 'Missing url in connectToNewSpec' )
564+
565+ await this . attachListeners ( browser , options . url , pageCriClient , automation , options )
566+ } ,
567+
568+ async connectToExisting ( browser : Browser , options : BrowserLaunchOpts , automation ) {
569+ const port = await protocol . getRemoteDebuggingPort ( )
570+
571+ debug ( 'connecting to existing chrome instance with url and debugging port' , { url : options . url , port } )
572+ if ( ! options . onError ) throw new Error ( 'Missing onError in connectToExisting' )
573+
574+ const browserCriClient = await BrowserCriClient . create ( port , browser . displayName , options . onError , onReconnect )
575+
576+ if ( ! options . url ) throw new Error ( 'Missing url in connectToExisting' )
577+
578+ const pageCriClient = await browserCriClient . attachToTargetUrl ( options . url )
579+
580+ await this . _setAutomation ( pageCriClient , automation , browserCriClient . resetBrowserTargets , options )
581+ } ,
582+
583+ async attachListeners ( browser : Browser , url : string , pageCriClient , automation : Automation , options : BrowserLaunchOpts & { onInitializeNewBrowserTab ?: ( ) => void } ) {
584+ if ( ! browserCriClient ) throw new Error ( 'Missing browserCriClient in attachListeners' )
585+
560586 await this . _setAutomation ( pageCriClient , automation , browserCriClient . resetBrowserTargets , options )
561587
562- // make sure page events are re enabled or else frame tree updates will NOT work as well as other items listening for page events
563588 await pageCriClient . send ( 'Page.enable' )
564589
565- await options . onInitializeNewBrowserTab ( )
590+ await options . onInitializeNewBrowserTab ?. ( )
566591
567592 await Promise . all ( [
568593 this . _maybeRecordVideo ( pageCriClient , options , browser . majorVersion ) ,
569594 this . _handleDownloads ( pageCriClient , options . downloadsFolder , automation ) ,
570595 ] )
571596
572- await this . _navigateUsingCRI ( pageCriClient , options . url )
597+ await this . _navigateUsingCRI ( pageCriClient , url )
573598
574599 if ( options . experimentalSessionAndOrigin ) {
575600 await this . _handlePausedRequests ( pageCriClient )
576601 _listenForFrameTreeChanges ( pageCriClient )
577602 }
578603 } ,
579604
580- async connectToExisting ( browser : Browser , options : CypressConfiguration = { } , automation ) {
581- const port = await protocol . getRemoteDebuggingPort ( )
582-
583- debug ( 'connecting to existing chrome instance with url and debugging port' , { url : options . url , port } )
584- const browserCriClient = await BrowserCriClient . create ( port , browser . displayName , options . onError , onReconnect )
585- const pageCriClient = await browserCriClient . attachToTargetUrl ( options . url )
586-
587- await this . _setAutomation ( pageCriClient , automation , browserCriClient . resetBrowserTargets , options )
588- } ,
589-
590- async open ( browser : Browser , url , options : CypressConfiguration = { } , automation : Automation ) : Promise < LaunchedBrowser > {
605+ async open ( browser : Browser , url , options : BrowserLaunchOpts , automation : Automation ) : Promise < LaunchedBrowser > {
591606 const { isTextTerminal } = options
592607
593608 const userDir = utils . getProfileDir ( browser , isTextTerminal )
@@ -646,6 +661,8 @@ export = {
646661 // SECOND connect to the Chrome remote interface
647662 // and when the connection is ready
648663 // navigate to the actual url
664+ if ( ! options . onError ) throw new Error ( 'Missing onError in chrome#open' )
665+
649666 browserCriClient = await BrowserCriClient . create ( port , browser . displayName , options . onError , onReconnect )
650667
651668 la ( browserCriClient , 'expected Chrome remote interface reference' , browserCriClient )
@@ -669,7 +686,7 @@ export = {
669686 debug ( 'closing remote interface client' )
670687
671688 // Do nothing on failure here since we're shutting down anyway
672- browserCriClient . close ( ) . catch ( )
689+ browserCriClient ? .close ( ) . catch ( )
673690 browserCriClient = undefined
674691
675692 debug ( 'closing chrome' )
@@ -679,21 +696,7 @@ export = {
679696
680697 const pageCriClient = await browserCriClient . attachToTargetUrl ( 'about:blank' )
681698
682- await this . _setAutomation ( pageCriClient , automation , browserCriClient . resetBrowserTargets , options )
683-
684- await pageCriClient . send ( 'Page.enable' )
685-
686- await Promise . all ( [
687- this . _maybeRecordVideo ( pageCriClient , options , browser . majorVersion ) ,
688- this . _handleDownloads ( pageCriClient , options . downloadsFolder , automation ) ,
689- ] )
690-
691- await this . _navigateUsingCRI ( pageCriClient , url )
692-
693- if ( options . experimentalSessionAndOrigin ) {
694- await this . _handlePausedRequests ( pageCriClient )
695- _listenForFrameTreeChanges ( pageCriClient )
696- }
699+ await this . attachListeners ( browser , url , pageCriClient , automation , options )
697700
698701 // return the launched browser process
699702 // with additional method to close the remote connection
0 commit comments