11import { URLSearchParams } from "url" ;
22import URI from "urijs" ;
33import { isBoolean , assign , isEmpty , set } from "lodash" ;
4- import { remote } from "@testplane/webdriverio" ;
5- import type { Capabilities } from "@testplane/wdio-types" ;
4+ import { WebDriver } from "@testplane/webdriver" ;
5+ import { sessionEnvironmentDetector } from "@testplane/wdio-utils" ;
6+ import { attach , remote } from "@testplane/webdriverio" ;
7+ import type { Capabilities , Options } from "@testplane/wdio-types" ;
68
79import { Browser , BrowserOpts } from "./browser" ;
810import signalHandler from "../signal-handler" ;
@@ -104,9 +106,60 @@ export class NewBrowser extends Browser {
104106 }
105107
106108 protected async _createSession ( ) : Promise < WebdriverIO . Browser > {
107- const sessionOpts = await this . _getSessionOpts ( ) ;
109+ if ( this . _isDevtools ( ) ) {
110+ return this . _getSessionOpts ( ) . then ( remote ) ;
111+ }
112+
113+ let sessionOptsException = null ;
114+ const sessionOptsPromise = this . _getSessionOpts ( ) // starts installing browsers + launching driver process...
115+ . catch ( exception => {
116+ sessionOptsException = exception ;
117+ } ) ;
118+
119+ const isUsingThirtPartyGrid = typeof this . _config . user === "string" && typeof this . _config . key === "string" ;
120+ let thirdPartyGridCustomOptions = { } ;
121+
122+ if ( isUsingThirtPartyGrid && ! this . _isLocalGridUrl ( ) ) {
123+ const gridUri = new URI ( this . _config . gridUrl ) ;
124+
125+ thirdPartyGridCustomOptions = await import ( "./third-party-grid" ) . then ( m =>
126+ m . detectBackend ( {
127+ port : gridUri . port ( ) ? parseInt ( gridUri . port ( ) , 10 ) : DEFAULT_PORT ,
128+ hostname : this . _getGridHost ( gridUri ) ,
129+ user : this . _config . user ,
130+ key : this . _config . key ,
131+ protocol : gridUri . protocol ( ) ,
132+ region : this . _config . region as Options . SauceRegions | undefined ,
133+ path : gridUri . path ( ) ,
134+ capabilities : this . _config . desiredCapabilities , // we dont need exact caps here
135+ } ) ,
136+ ) ;
137+ }
138+
139+ const sessionOpts = await sessionOptsPromise ;
140+
141+ if ( sessionOptsException ) {
142+ throw sessionOptsException ;
143+ }
108144
109- return remote ( sessionOpts ) ;
145+ const fullSessionOpts = Object . assign ( sessionOpts ! , thirdPartyGridCustomOptions ) ;
146+ const session = await WebDriver . newSession ( fullSessionOpts ) ;
147+
148+ const detectedSessionEnvFlags = sessionEnvironmentDetector ( {
149+ capabilities : session . capabilities ,
150+ requestedCapabilities : fullSessionOpts . capabilities ,
151+ } ) ;
152+
153+ return attach ( {
154+ sessionId : session . sessionId ,
155+ ...session . options ,
156+ ...this . _getSessionOptsFromConfig ( [ "transformRequest" , "transformResponse" ] ) ,
157+ ...detectedSessionEnvFlags ,
158+ ...this . _config . sessionEnvFlags ,
159+ options : session . options ,
160+ capabilities : session . capabilities ,
161+ requestedCapabilities : fullSessionOpts . capabilities as WebdriverIO . Capabilities ,
162+ } ) ;
110163 }
111164
112165 protected async _setPageLoadTimeout ( ) : Promise < void > {
@@ -133,6 +186,10 @@ export class NewBrowser extends Browser {
133186 return this . _config . gridUrl === LOCAL_GRID_URL || getInstance ( ) . local ;
134187 }
135188
189+ protected _isDevtools ( ) : boolean {
190+ return this . _config . automationProtocol === DEVTOOLS_PROTOCOL || getInstance ( ) . devtools ;
191+ }
192+
136193 protected async _getSessionOpts ( ) : Promise < Capabilities . WebdriverIOConfig > {
137194 const config = this . _config ;
138195
@@ -150,16 +207,14 @@ export class NewBrowser extends Browser {
150207
151208 const capabilities = await this . _extendCapabilities ( config ) ;
152209
153- const { devtools } = getInstance ( ) ;
154-
155210 const options = {
156211 protocol : gridUri . protocol ( ) ,
157212 hostname : this . _getGridHost ( gridUri ) ,
158213 port : gridUri . port ( ) ? parseInt ( gridUri . port ( ) , 10 ) : DEFAULT_PORT ,
159214 path : gridUri . path ( ) ,
160215 queryParams : this . _getQueryParams ( gridUri . query ( ) ) ,
161216 capabilities,
162- automationProtocol : devtools ? DEVTOOLS_PROTOCOL : config . automationProtocol ,
217+ automationProtocol : this . _isDevtools ( ) ? DEVTOOLS_PROTOCOL : config . automationProtocol ,
163218 connectionRetryTimeout : config . sessionRequestTimeout || config . httpTimeout ,
164219 connectionRetryCount : 3 ,
165220 baseUrl : config . baseUrl ,
0 commit comments