@@ -18,11 +18,9 @@ import utils from './utils'
18
18
import type { Browser } from './types'
19
19
import { BrowserCriClient } from './browser-cri-client'
20
20
import type { LaunchedBrowser } from '@packages/launcher/lib/browsers'
21
- import type { CRIWrapper } from './cri-client'
21
+ import type { CriClient } from './cri-client'
22
22
import 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'
26
24
27
25
const debug = debugModule ( 'cypress:server:browsers:chrome' )
28
26
@@ -123,7 +121,7 @@ const DEFAULT_ARGS = [
123
121
'--disable-dev-shm-usage' ,
124
122
]
125
123
126
- let browserCriClient
124
+ let browserCriClient : BrowserCriClient | undefined
127
125
128
126
/**
129
127
* Reads all known preference files (CHROME_PREFERENCE_PATHS) from disk and retur
@@ -320,15 +318,15 @@ const _handleDownloads = async function (client, dir, automation) {
320
318
let frameTree
321
319
let gettingFrameTree
322
320
323
- const onReconnect = ( client : CRIWrapper . Client ) => {
321
+ const onReconnect = ( client : CriClient ) => {
324
322
// if the client disconnects (e.g. due to a computer sleeping), update
325
323
// the frame tree on reconnect in cases there were changes while
326
324
// the client was disconnected
327
325
return _updateFrameTree ( client , 'onReconnect' ) ( )
328
326
}
329
327
330
328
// 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 ( ) => {
332
330
debug ( `update frame tree for ${ eventName } ` )
333
331
334
332
gettingFrameTree = new Promise < void > ( async ( resolve ) => {
@@ -433,8 +431,8 @@ const _handlePausedRequests = async (client) => {
433
431
} )
434
432
}
435
433
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 )
438
436
439
437
return automation . use ( cdpAutomation )
440
438
}
@@ -490,7 +488,7 @@ export = {
490
488
return extensionDest
491
489
} ,
492
490
493
- _getArgs ( browser : Browser , options : CypressConfiguration , port : string ) {
491
+ _getArgs ( browser : Browser , options : BrowserLaunchOpts , port : string ) {
494
492
const args = ( [ ] as string [ ] ) . concat ( DEFAULT_ARGS )
495
493
496
494
if ( os . platform ( ) === 'linux' ) {
@@ -551,43 +549,60 @@ export = {
551
549
return args
552
550
} ,
553
551
554
- async connectToNewSpec ( browser : Browser , options : CypressConfiguration = { } , automation : Automation ) {
552
+ async connectToNewSpec ( browser : Browser , options : BrowserNewTabOpts , automation : Automation ) {
555
553
debug ( 'connecting to new chrome tab in existing instance with url and debugging port' , { url : options . url } )
556
554
557
555
const browserCriClient = this . _getBrowserCriClient ( )
556
+
557
+ if ( ! browserCriClient ) throw new Error ( 'Missing browserCriClient in connectToNewSpec' )
558
+
558
559
const pageCriClient = browserCriClient . currentlyAttachedTarget
559
560
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
+
560
586
await this . _setAutomation ( pageCriClient , automation , browserCriClient . resetBrowserTargets , options )
561
587
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
563
588
await pageCriClient . send ( 'Page.enable' )
564
589
565
- await options . onInitializeNewBrowserTab ( )
590
+ await options . onInitializeNewBrowserTab ?. ( )
566
591
567
592
await Promise . all ( [
568
593
this . _maybeRecordVideo ( pageCriClient , options , browser . majorVersion ) ,
569
594
this . _handleDownloads ( pageCriClient , options . downloadsFolder , automation ) ,
570
595
] )
571
596
572
- await this . _navigateUsingCRI ( pageCriClient , options . url )
597
+ await this . _navigateUsingCRI ( pageCriClient , url )
573
598
574
599
if ( options . experimentalSessionAndOrigin ) {
575
600
await this . _handlePausedRequests ( pageCriClient )
576
601
_listenForFrameTreeChanges ( pageCriClient )
577
602
}
578
603
} ,
579
604
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 > {
591
606
const { isTextTerminal } = options
592
607
593
608
const userDir = utils . getProfileDir ( browser , isTextTerminal )
@@ -646,6 +661,8 @@ export = {
646
661
// SECOND connect to the Chrome remote interface
647
662
// and when the connection is ready
648
663
// navigate to the actual url
664
+ if ( ! options . onError ) throw new Error ( 'Missing onError in chrome#open' )
665
+
649
666
browserCriClient = await BrowserCriClient . create ( port , browser . displayName , options . onError , onReconnect )
650
667
651
668
la ( browserCriClient , 'expected Chrome remote interface reference' , browserCriClient )
@@ -669,7 +686,7 @@ export = {
669
686
debug ( 'closing remote interface client' )
670
687
671
688
// Do nothing on failure here since we're shutting down anyway
672
- browserCriClient . close ( ) . catch ( )
689
+ browserCriClient ? .close ( ) . catch ( )
673
690
browserCriClient = undefined
674
691
675
692
debug ( 'closing chrome' )
@@ -679,21 +696,7 @@ export = {
679
696
680
697
const pageCriClient = await browserCriClient . attachToTargetUrl ( 'about:blank' )
681
698
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 )
697
700
698
701
// return the launched browser process
699
702
// with additional method to close the remote connection
0 commit comments