Skip to content

Commit f36cd24

Browse files
Let auditors command fail if tracing failed (webdriverio#4416)
* let auditors command fail if tracing failed closes webdriverio#4085 * revert changes in examples
1 parent 047b77a commit f36cd24

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

packages/wdio-devtools-service/src/auditor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ export default class Auditor {
6767
* an Auditor instance is created for every trace so provide an updateCommands
6868
* function to receive the latest performance metrics with the browser instance
6969
*/
70-
updateCommands (browser) {
70+
updateCommands (browser, customFn) {
7171
const commands = Object.getOwnPropertyNames(Object.getPrototypeOf(this)).filter(
7272
fnName => fnName !== 'constructor' && fnName !== 'updateCommands' && !fnName.startsWith('_'))
73-
commands.forEach(fnName => browser.addCommand(fnName, ::this[fnName]))
73+
commands.forEach(fnName => browser.addCommand(fnName, customFn || ::this[fnName]))
7474
}
7575

7676
async getMainThreadWorkBreakdown () {

packages/wdio-devtools-service/src/gatherer/trace.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export default class TraceGatherer extends EventEmitter {
9999
this.waitForCPUIdleEvent.cancel()
100100
this.frameId = '"unsuccessful loaded frame"'
101101
this.finishTracing()
102+
this.emit('tracingError', new Error(`Page with url "${msgObj.frame.url}" failed to load`))
102103
return clearTimeout(this.clickTraceTimeout)
103104
}
104105

@@ -249,6 +250,7 @@ export default class TraceGatherer extends EventEmitter {
249250
this.finishTracing()
250251
} catch (err) {
251252
log.error(`Error capturing tracing logs: ${err.stack}`)
253+
this.emit('tracingError', err)
252254
return this.finishTracing()
253255
}
254256
}
@@ -273,6 +275,7 @@ export default class TraceGatherer extends EventEmitter {
273275
delete this.frameId
274276
delete this.loaderId
275277
delete this.pageUrl
278+
this.failingFrameLoadIds = []
276279
this.waitForNetworkIdleEvent.cancel()
277280
this.waitForCPUIdleEvent.cancel()
278281
this.emit('tracingFinished')

packages/wdio-devtools-service/src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default class DevToolsService {
8383
* allow user to work with Puppeteer directly
8484
*/
8585
global.browser.addCommand('getPuppeteer',
86-
() => this.devtoolsDriver)
86+
/* istanbul ignore next */ () => this.devtoolsDriver)
8787
}
8888

8989
async beforeCommand (commandName, params) {
@@ -113,6 +113,13 @@ export default class DevToolsService {
113113
auditor.updateCommands(global.browser)
114114
})
115115

116+
this.traceGatherer.once('tracingError', (err) => {
117+
const auditor = new Auditor()
118+
auditor.updateCommands(global.browser, /* istanbul ignore next */ () => {
119+
throw new Error(`Couldn't capture performance due to: ${err.message}`)
120+
})
121+
})
122+
116123
return new Promise((resolve) => {
117124
log.info(`Wait until tracing for command ${commandName} finishes`)
118125

packages/wdio-devtools-service/tests/gatherer/trace.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ test('onFrameNavigated: should cancel trace if page load failed', () => {
139139
traceGatherer.waitForNetworkIdleEvent = { cancel: jest.fn() }
140140
traceGatherer.waitForCPUIdleEvent = { cancel: jest.fn() }
141141
traceGatherer.onFrameNavigated({ frame })
142-
expect(traceGatherer.emit).toHaveBeenCalledTimes(0)
142+
expect(traceGatherer.emit).toHaveBeenCalledWith('tracingError', expect.any(Error))
143143
expect(traceGatherer.finishTracing).toHaveBeenCalledTimes(1)
144144
expect(traceGatherer.waitForNetworkIdleEvent.cancel).toHaveBeenCalledTimes(1)
145145
expect(traceGatherer.waitForCPUIdleEvent.cancel).toHaveBeenCalledTimes(1)
@@ -189,7 +189,7 @@ test('completeTracing: in failure case', async () => {
189189
pageMock.tracing.stop.mockReturnValue(Promise.reject(new Error('boom')))
190190
await traceGatherer.completeTracing()
191191
expect(traceGatherer.finishTracing).toHaveBeenCalledTimes(1)
192-
expect(traceGatherer.emit).toHaveBeenCalledTimes(0)
192+
expect(traceGatherer.emit).toBeCalledWith('tracingError', expect.any(Error))
193193
})
194194

195195
test('onLoadEventFired', async () => {

packages/wdio-devtools-service/tests/service.test.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ test('afterCommand', () => {
175175
expect(service.traceGatherer.once).toBeCalledTimes(0)
176176

177177
service.afterCommand('navigateTo')
178-
expect(service.traceGatherer.once).toBeCalledTimes(2)
178+
expect(service.traceGatherer.once).toBeCalledTimes(3)
179179

180180
service.afterCommand('url')
181-
expect(service.traceGatherer.once).toBeCalledTimes(4)
181+
expect(service.traceGatherer.once).toBeCalledTimes(6)
182182

183183
service.afterCommand('click')
184-
expect(service.traceGatherer.once).toBeCalledTimes(6)
184+
expect(service.traceGatherer.once).toBeCalledTimes(9)
185185
})
186186

187187
test('afterCommand: should create a new auditor instance and should update the browser commands', () => {
@@ -198,6 +198,20 @@ test('afterCommand: should create a new auditor instance and should update the b
198198
delete global.browser
199199
})
200200

201+
test('afterCommand: should update browser commands even if failed', () => {
202+
const service = new DevToolsService()
203+
service.traceGatherer = new EventEmitter()
204+
service.traceGatherer.isTracing = true
205+
service.devtoolsGatherer = { getLogs: jest.fn() }
206+
global.browser = 'some browser'
207+
service.afterCommand('url')
208+
service.traceGatherer.emit('tracingError', new Error('boom'))
209+
210+
const auditor = new Auditor()
211+
expect(auditor.updateCommands).toBeCalledWith('some browser', expect.any(Function))
212+
delete global.browser
213+
})
214+
201215
test('afterCommand: should continue with command after tracingFinished was emitted', async () => {
202216
const service = new DevToolsService()
203217
service.traceGatherer = new EventEmitter()

0 commit comments

Comments
 (0)