@@ -4,15 +4,14 @@ import path from 'node:path'
44
55import logger from '@wdio/logger'
66import { SevereServiceError } from 'webdriverio'
7- import type { Capabilities , Options } from '@wdio/types'
7+ import type { Services , Reporters , Capabilities , Options } from '@wdio/types'
88import type { WebDriverCommands } from '@wdio/protocols'
9- import type { Services , Reporters } from '@wdio/types'
109
1110import { SessionCapturer } from './session.js'
1211import { TestReporter } from './reporter.js'
1312import { DevToolsAppLauncher } from './launcher.js'
1413import { getBrowserObject } from './utils.ts'
15- import { type TraceLog , TraceType } from './types.ts'
14+ import { type TraceLog , type ExtendedCapabilities , TraceType } from './types.ts'
1615
1716export const launcher = DevToolsAppLauncher
1817
@@ -24,6 +23,7 @@ const log = logger('@wdio/devtools-service')
2423export function setupForDevtools ( opts : Options . WebdriverIO ) {
2524 let browserCaptured = false
2625 const service = new DevToolsHookService ( )
26+ service . captureType = TraceType . Standalone
2727 service . beforeSession ( null as never , opts . capabilities as Capabilities . RemoteCapability )
2828
2929 /**
@@ -69,8 +69,21 @@ export default class DevToolsHookService implements Services.ServiceInstance {
6969 #sessionCapturer = new SessionCapturer ( )
7070 #browser: WebdriverIO . Browser | undefined
7171
72- before ( _ : never , __ : never , browser : WebdriverIO . Browser ) {
72+ /**
73+ * allows to define the type of data being captured to hint the
74+ * devtools app which data to expect
75+ */
76+ captureType = TraceType . Testrunner
77+
78+ before ( caps : Capabilities . RemoteCapability , __ : never , browser : WebdriverIO . Browser ) {
7379 this . #browser = browser
80+
81+ console . log ( '\n\n\nWUTTT' , caps )
82+ const w3cCaps = caps as Capabilities . W3CCapabilities
83+ const c = w3cCaps . alwaysMatch
84+ ? w3cCaps . alwaysMatch as ExtendedCapabilities
85+ : caps as ExtendedCapabilities
86+ this . #sessionCapturer = new SessionCapturer ( c [ 'wdio:devtoolsOptions' ] )
7487 }
7588
7689 beforeSession ( config : Options . WebdriverIO | Options . Testrunner , capabilities : Capabilities . RemoteCapability ) {
@@ -103,7 +116,7 @@ export default class DevToolsHookService implements Services.ServiceInstance {
103116 */
104117 class DevToolsReporter extends TestReporter {
105118 constructor ( options : Reporters . Options ) {
106- super ( options )
119+ super ( options , self . #sessionCapturer )
107120 self . #testReporters. push ( this )
108121 }
109122 }
@@ -119,6 +132,20 @@ export default class DevToolsHookService implements Services.ServiceInstance {
119132 }
120133
121134 afterCommand ( command : keyof WebDriverCommands , args : any [ ] , result : any , error : Error ) {
135+ if ( this . #browser && command === 'navigateTo' ) {
136+ /**
137+ * propagate session metadata at the beginning of the session
138+ */
139+ browser . execute ( ( ) => window . wdioTraceCollector . getMetadata ( ) )
140+ . then ( ( metadata ) => this . #sessionCapturer. sendUpstream ( 'metadata' , {
141+ ...metadata ,
142+ type : this . captureType ,
143+ options : browser . options ,
144+ capabilities : browser . capabilities
145+ } )
146+ )
147+ }
148+
122149 return this . #sessionCapturer. afterCommand ( browser , command , args , result , error )
123150 }
124151
@@ -127,7 +154,7 @@ export default class DevToolsHookService implements Services.ServiceInstance {
127154 * we can use it to write all trace information to a file
128155 */
129156 async after ( ) {
130- if ( ! this . #browser) {
157+ if ( ! this . #browser || this . #sessionCapturer . isReportingUpstream ) {
131158 return
132159 }
133160 const outputDir = this . #browser. options . outputDir || process . cwd ( )
@@ -137,7 +164,7 @@ export default class DevToolsHookService implements Services.ServiceInstance {
137164 logs : this . #sessionCapturer. traceLogs ,
138165 consoleLogs : this . #sessionCapturer. consoleLogs ,
139166 metadata : {
140- type : TraceType . Standalone ,
167+ type : this . captureType ,
141168 ...this . #sessionCapturer. metadata ! ,
142169 options,
143170 capabilities
0 commit comments