@@ -13,6 +13,7 @@ import * as CriClient from './cri-client'
1313import * as protocol from './protocol'
1414import utils from './utils'
1515import { Browser } from './types'
16+ import errors from '../errors'
1617
1718// TODO: this is defined in `cypress-npm-api` but there is currently no way to get there
1819type CypressConfiguration = any
@@ -35,6 +36,9 @@ type ChromePreferences = {
3536 localState : object
3637}
3738
39+ const staticCdpPort = Number ( process . env . CYPRESS_REMOTE_DEBUGGING_PORT )
40+ const stdioTimeoutMs = Number ( process . env . CYPRESS_CDP_TARGET_TIMEOUT ) || 60000
41+
3842const pathToExtension = extension . getPathToExtension ( )
3943const pathToTheme = extension . getPathToTheme ( )
4044
@@ -171,9 +175,7 @@ const _writeChromePreferences = (userDir: string, originalPrefs: ChromePreferenc
171175}
172176
173177const getRemoteDebuggingPort = async ( ) => {
174- const port = Number ( process . env . CYPRESS_REMOTE_DEBUGGING_PORT )
175-
176- return port || utils . getPort ( )
178+ return staticCdpPort || utils . getPort ( )
177179}
178180
179181/**
@@ -241,19 +243,47 @@ const _disableRestorePagesPrompt = function (userDir) {
241243 . catch ( ( ) => { } )
242244}
243245
246+ const useStdioCdp = ( browser ) => {
247+ return (
248+ // CDP via stdio doesn't seem to work in browsers older than 72
249+ // @see https://github.com/cyrus-and/chrome-remote-interface/issues/381#issuecomment-445277683
250+ browser . majorVersion >= 72
251+ // allow users to force TCP by specifying a port in environment
252+ && ! staticCdpPort
253+ )
254+ }
255+
244256// After the browser has been opened, we can connect to
245257// its remote interface via a websocket.
246- const _connectToChromeRemoteInterface = function ( port , onError ) {
247- // @ts -ignore
248- la ( check . userPort ( port ) , 'expected port number to connect CRI to' , port )
258+ const _connectToChromeRemoteInterface = function ( browser , process , port , onError ) {
259+ const connectTcp = ( ) => {
260+ // @ts -ignore
261+ la ( check . userPort ( port ) , 'expected port number to connect CRI to' , port )
262+
263+ debug ( 'connecting to Chrome remote interface at random port %d' , port )
264+
265+ return protocol . getWsTargetFor ( port )
266+ . then ( ( wsUrl ) => {
267+ debug ( 'received wsUrl %s for port %d' , wsUrl , port )
268+
269+ return CriClient . create ( { target : wsUrl } , onError )
270+ } )
271+ }
272+
273+ if ( ! useStdioCdp ( browser ) ) {
274+ return connectTcp ( )
275+ }
276+
277+ return CriClient . create ( { process } , onError )
278+ . timeout ( stdioTimeoutMs )
279+ . catch ( Bluebird . TimeoutError , async ( ) => {
280+ errors . warning ( 'CDP_STDIO_TIMEOUT' , browser . displayName , stdioTimeoutMs )
249281
250- debug ( 'connecting to Chrome remote interface at random port %d' , port )
282+ const client = await connectTcp ( )
251283
252- return protocol . getWsTargetFor ( port )
253- . then ( ( wsUrl ) => {
254- debug ( 'received wsUrl %s for port %d' , wsUrl , port )
284+ errors . warning ( 'CDP_FALLBACK_SUCCEEDED' , browser . displayName )
255285
256- return CriClient . create ( wsUrl , onError )
286+ return client
257287 } )
258288}
259289
@@ -405,6 +435,10 @@ export = {
405435 args . push ( `--remote-debugging-port=${ port } ` )
406436 args . push ( '--remote-debugging-address=127.0.0.1' )
407437
438+ if ( useStdioCdp ( browser ) ) {
439+ args . push ( '--remote-debugging-pipe' )
440+ }
441+
408442 return args
409443 } ,
410444
@@ -460,14 +494,16 @@ export = {
460494 // first allows us to connect the remote interface,
461495 // start video recording and then
462496 // we will load the actual page
463- const launchedBrowser = await utils . launch ( browser , 'about:blank' , args )
497+ const launchedBrowser = await utils . launch ( browser , 'about:blank' , args , {
498+ pipeStdio : useStdioCdp ( browser ) ,
499+ } )
464500
465501 la ( launchedBrowser , 'did not get launched browser instance' )
466502
467503 // SECOND connect to the Chrome remote interface
468504 // and when the connection is ready
469505 // navigate to the actual url
470- const criClient = await this . _connectToChromeRemoteInterface ( port , options . onError )
506+ const criClient = await this . _connectToChromeRemoteInterface ( browser , launchedBrowser , port , options . onError )
471507
472508 la ( criClient , 'expected Chrome remote interface reference' , criClient )
473509
0 commit comments