@@ -222,6 +222,10 @@ function createChromedriverCaps(opts, deviceId, webViewDetails) {
222222 delete opts . chromeOptions . Arguments ;
223223 }
224224
225+ if ( opts . webSocketUrl && _ . isNil ( caps . chromeOptions . webSocketUrl ) ) {
226+ caps . chromeOptions . webSocketUrl = opts . webSocketUrl ;
227+ }
228+
225229 this . log . debug (
226230 'Precalculated Chromedriver capabilities: ' + JSON . stringify ( caps . chromeOptions , null , 2 ) ,
227231 ) ;
@@ -763,26 +767,52 @@ export async function setupNewChromedriver(opts, curDeviceId, context) {
763767 this . log . debug (
764768 `Before starting chromedriver, androidPackage is '${ caps . chromeOptions . androidPackage } '` ,
765769 ) ;
766- await chromedriver . start ( caps ) ;
770+ const sessionCaps = await chromedriver . start ( caps ) ;
771+ updateBidiProxyUrl . bind ( this ) ( sessionCaps , context ) ;
767772 return chromedriver ;
768773}
769774
770775/**
771776 * @this {import('../../driver').AndroidDriver}
772777 * @template {Chromedriver} T
773778 * @param {T } chromedriver
779+ * @param {string } context
774780 * @returns {Promise<T> }
775781 */
776- export async function setupExistingChromedriver ( chromedriver ) {
782+ export async function setupExistingChromedriver ( chromedriver , context ) {
777783 // check the status by sending a simple window-based command to ChromeDriver
778784 // if there is an error, we want to recreate the ChromeDriver session
779- if ( ! ( await chromedriver . hasWorkingWebview ( ) ) ) {
785+ if ( await chromedriver . hasWorkingWebview ( ) ) {
786+ const cachedBidiProxyUrl = this . _bidiProxyUrlCache . get ( context ) ;
787+ if ( cachedBidiProxyUrl ) {
788+ updateBidiProxyUrl . bind ( this ) ( { webSocketUrl : cachedBidiProxyUrl } , context ) ;
789+ }
790+ } else {
780791 this . log . debug ( 'ChromeDriver is not associated with a window. Re-initializing the session.' ) ;
781- await chromedriver . restart ( ) ;
792+ const sessionCaps = await chromedriver . restart ( ) ;
793+ updateBidiProxyUrl . bind ( this ) ( sessionCaps , context ) ;
782794 }
783795 return chromedriver ;
784796}
785797
798+ /**
799+ * @this {import('../../driver').AndroidDriver}
800+ * @param {Record<string, any> } sessionCaps
801+ * @param {string } context
802+ * @returns {void }
803+ */
804+ function updateBidiProxyUrl ( sessionCaps , context ) {
805+ if ( ! this . opts ?. chromedriverForwardBiDi || ! sessionCaps ?. webSocketUrl ) {
806+ return ;
807+ }
808+
809+ this . _bidiProxyUrlCache . set ( context , sessionCaps . webSocketUrl ) ;
810+ if ( this . _bidiProxyUrl !== sessionCaps . webSocketUrl ) {
811+ this . _bidiProxyUrl = sessionCaps . webSocketUrl ;
812+ this . log . debug ( `Updated Bidi Proxy URL to ${ this . _bidiProxyUrl } ` ) ;
813+ }
814+ }
815+
786816/**
787817 * @this {import('../../driver').AndroidDriver}
788818 * @returns {boolean }
0 commit comments