-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathcreateTest.ts
More file actions
432 lines (376 loc) · 14.1 KB
/
createTest.ts
File metadata and controls
432 lines (376 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import type { LogsInitConfiguration } from '@datadog/browser-logs'
import type { RemoteConfiguration } from '@datadog/browser-remote-config'
import type { RumInitConfiguration } from '@datadog/browser-rum-core'
import type { BrowserContext, Page } from '@playwright/test'
import { test, expect } from '@playwright/test'
import { addTag, addTestOptimizationTags } from '../helpers/tags'
import { getRunId } from '../../../envUtils'
import type { BrowserLog } from '../helpers/browser'
import { BrowserLogsManager, deleteAllCookies, getBrowserName, sendXhr } from '../helpers/browser'
import { DEFAULT_LOGS_CONFIGURATION, DEFAULT_RUM_CONFIGURATION } from '../helpers/configuration'
import { validateRumFormat } from '../helpers/validation'
import type { BrowserConfiguration } from '../../../browsers.conf'
import { IntakeRegistry } from './intakeRegistry'
import { flushEvents } from './flushEvents'
import type { Servers } from './httpServers'
import { getTestServers, waitForServersIdle } from './httpServers'
import type { CallerLocation, SetupFactory, SetupOptions } from './pageSetups'
import { html, DEFAULT_SETUPS, npmSetup, reactSetup } from './pageSetups'
import { createIntakeServerApp } from './serverApps/intake'
import { createMockServerApp } from './serverApps/mock'
import type { Extension } from './createExtension'
import type { Worker } from './createWorker'
import { isBrowserStack } from './environment'
export function createTest(title: string) {
return new TestBuilder(title, captureCallerLocation())
}
interface TestContext {
baseUrl: string
intakeRegistry: IntakeRegistry
servers: Servers
page: Page
browserContext: BrowserContext
browserName: 'chromium' | 'firefox' | 'webkit' | 'msedge'
getExtensionId: () => Promise<string>
withBrowserLogs: (cb: (logs: BrowserLog[]) => void) => void
flushBrowserLogs: () => void
flushEvents: () => Promise<void>
deleteAllCookies: () => Promise<void>
sendXhr: (url: string, headers?: string[][]) => Promise<string>
evaluateInWorker: (fn: () => void) => Promise<void>
}
type TestRunner = (testContext: TestContext) => Promise<void> | void
class TestBuilder {
private rumConfiguration: RumInitConfiguration | undefined = undefined
private alsoRunWithRumSlim = false
private logsConfiguration: LogsInitConfiguration | undefined = undefined
private remoteConfiguration?: RemoteConfiguration = undefined
private head = ''
private body = ''
private basePath = ''
private eventBridge = false
private setups: Array<{ factory: SetupFactory; name?: string }> = DEFAULT_SETUPS
private testFixture: typeof test = test
private extension: {
rumConfiguration?: RumInitConfiguration
logsConfiguration?: LogsInitConfiguration
} = {}
private worker: Worker | undefined
private hostName?: string
constructor(
private title: string,
private callerLocation: CallerLocation | undefined
) {}
withRum(rumInitConfiguration?: Partial<RumInitConfiguration>) {
this.rumConfiguration = { ...DEFAULT_RUM_CONFIGURATION, ...rumInitConfiguration }
return this
}
withRumSlim() {
this.alsoRunWithRumSlim = true
return this
}
withRumInit(rumInit: (initConfiguration: RumInitConfiguration) => void) {
this.rumInit = rumInit
return this
}
withLogsInit(logsInit: (initConfiguration: LogsInitConfiguration) => void) {
this.logsInit = logsInit
return this
}
withLogs(logsInitConfiguration?: Partial<LogsInitConfiguration>) {
this.logsConfiguration = { ...DEFAULT_LOGS_CONFIGURATION, ...logsInitConfiguration }
return this
}
withHead(head: string) {
this.head = head
return this
}
withBody(body: string) {
this.body = body
return this
}
withEventBridge() {
this.eventBridge = true
return this
}
withReactApp(appName: string) {
this.setups = [{ factory: (options, servers) => reactSetup(options, servers, appName) }]
return this
}
withBasePath(newBasePath: string) {
this.basePath = newBasePath
return this
}
withSetup(setup: SetupFactory) {
this.setups = [{ factory: setup }]
return this
}
withExtension(extension: Extension) {
this.testFixture = extension.fixture
this.extension.rumConfiguration = extension.rumConfiguration
this.extension.logsConfiguration = extension.logsConfiguration
return this
}
withRemoteConfiguration(remoteConfiguration: RemoteConfiguration) {
this.remoteConfiguration = remoteConfiguration
return this
}
withWorker(worker: Worker) {
this.worker = worker
const url = worker.importScripts ? '/sw.js?importScripts=true' : '/sw.js'
const options = worker.importScripts ? '{}' : '{ type: "module" }'
// Service workers require HTTPS or localhost due to browser security restrictions
this.hostName = 'localhost'
this.withBody(html`
<script>
if (!window.myServiceWorker && 'serviceWorker' in navigator) {
navigator.serviceWorker.register('${url}', ${options}).then((registration) => {
window.myServiceWorker = registration
})
}
</script>
`)
return this
}
withHostName(hostName: string) {
this.hostName = hostName
return this
}
run(runner: TestRunner) {
const setupOptions: SetupOptions = {
body: this.body,
head: this.head,
logs: this.logsConfiguration,
rum: this.rumConfiguration,
remoteConfiguration: this.remoteConfiguration,
rumInit: this.rumInit,
logsInit: this.logsInit,
useRumSlim: false,
eventBridge: this.eventBridge,
basePath: this.basePath,
context: {
run_id: getRunId(),
test_name: '<PLACEHOLDER>',
},
testFixture: this.testFixture,
extension: this.extension,
hostName: this.hostName,
worker: this.worker,
callerLocation: this.callerLocation,
}
if (this.alsoRunWithRumSlim) {
this.testFixture.describe(this.title, () => {
declareTestsForSetups('rum', this.setups, setupOptions, runner)
declareTestsForSetups(
'rum-slim',
this.setups.filter((setup) => setup.factory !== npmSetup && setup.factory !== reactSetup),
{ ...setupOptions, useRumSlim: true },
runner
)
})
} else {
declareTestsForSetups(this.title, this.setups, setupOptions, runner)
}
}
private rumInit: (configuration: RumInitConfiguration) => void = (configuration) => {
window.DD_RUM!.init(configuration)
}
private logsInit: (configuration: LogsInitConfiguration) => void = (configuration) => {
window.DD_LOGS!.init(configuration)
}
}
/**
* Captures the location of the caller's caller (i.e. the scenario file that called run()).
* This is used to override Playwright's default location detection so that test output
* shows the scenario file instead of createTest.ts.
*/
function captureCallerLocation(): CallerLocation | undefined {
const error = new Error()
const lines = error.stack?.split('\n')
if (!lines || lines.length < 4) {
return undefined
}
// Stack layout:
// [0] "Error"
// [1] " at captureCallerLocation (...)"
// [2] " at createTest (...)"
// [3] " at <scenario file> (...)"
const frame = lines[3]
const match = frame?.match(/\((.+):(\d+):(\d+)\)/) ?? frame?.match(/at (.+):(\d+):(\d+)/)
if (match) {
return { file: match[1], line: Number(match[2]), column: Number(match[3]) }
}
return undefined
}
function declareTestsForSetups(
title: string,
setups: Array<{ factory: SetupFactory; name?: string }>,
setupOptions: SetupOptions,
runner: TestRunner
) {
if (setups.length > 1) {
setupOptions.testFixture.describe(title, () => {
for (const { name, factory } of setups) {
declareTest(name!, setupOptions, factory, runner)
}
})
} else if (setups.length === 1) {
declareTest(title, setupOptions, setups[0].factory, runner)
} else {
console.warn('no setup available for', title)
}
}
/**
* Resolves the Playwright test function to use for declaring a test.
*
* When a callerLocation is available, accesses Playwright's internal TestTypeImpl via its
* private Symbol to call _createTest() directly with a custom location. This makes test
* output point to the scenario file instead of createTest.ts.
*/
function resolveTestFunction(setupOptions: SetupOptions): typeof test {
const testFn = setupOptions.testFixture ?? test
const testTypeSymbol = Object.getOwnPropertySymbols(testFn).find((s) => s.description === 'testType')
if (setupOptions.callerLocation && testTypeSymbol) {
const testTypeImpl = (testFn as any)[testTypeSymbol]
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
return testTypeImpl._createTest.bind(testTypeImpl, 'default', setupOptions.callerLocation)
}
return testFn
}
function declareTest(title: string, setupOptions: SetupOptions, factory: SetupFactory, runner: TestRunner) {
const testFixture = resolveTestFunction(setupOptions)
testFixture(title, async ({ page, context }: { page: Page; context: BrowserContext }) => {
const browserName = getBrowserName(test.info().project.name)
addTag('test.browserName', browserName)
addTestOptimizationTags(test.info().project.metadata as BrowserConfiguration)
test.skip(
!!setupOptions.hostName && setupOptions.hostName.endsWith('.localhost') && isBrowserStack,
// Skip those tests on BrowserStack because it doesn't support localhost subdomains. As a
// workaround we could use normal domains and use either:
// * the BrowserStack proxy capabilities -> not tried, but this sounds more complex because
// we also want to run tests outside of BrowserStack
// * the Playwright proxy capabilities -> tried and it seems to fail because of mismatch
// version between Playwright local and BrowserStack versions
// * a "ngrok-like" service -> not tried yet (it sounds more complex)
//
// See https://www.browserstack.com/support/faq/local-testing/local-exceptions/i-face-issues-while-testing-localhost-urls-or-private-servers-in-safari-on-macos-os-x-and-ios
'Localhost subdomains are not supported in BrowserStack'
)
const title = test.info().titlePath.join(' > ')
setupOptions.context.test_name = title
const servers = await getTestServers()
const browserLogs = new BrowserLogsManager()
const testContext = createTestContext(servers, page, context, browserLogs, browserName, setupOptions)
servers.intake.bindServerApp(createIntakeServerApp(testContext.intakeRegistry))
const setup = factory(setupOptions, servers)
servers.base.bindServerApp(
createMockServerApp(servers, setup, setupOptions.remoteConfiguration, setupOptions.worker)
)
servers.crossOrigin.bindServerApp(createMockServerApp(servers, setup))
await setUpTest(browserLogs, testContext)
try {
await runner(testContext)
tearDownPassedTest(testContext)
} finally {
await tearDownTest(testContext)
}
})
}
function createTestContext(
servers: Servers,
page: Page,
browserContext: BrowserContext,
browserLogsManager: BrowserLogsManager,
browserName: TestContext['browserName'],
{ basePath, hostName }: SetupOptions
): TestContext {
const baseUrl = new URL(basePath, servers.base.origin)
if (hostName) {
baseUrl.hostname = hostName
}
return {
baseUrl: baseUrl.href,
intakeRegistry: new IntakeRegistry(),
servers,
page,
browserContext,
browserName,
withBrowserLogs: (cb: (logs: BrowserLog[]) => void) => {
try {
cb(browserLogsManager.get())
} finally {
browserLogsManager.clear()
}
},
evaluateInWorker: async (fn: () => void) => {
await page.evaluate(async (code) => {
const { active, installing, waiting } = window.myServiceWorker
const worker = active ?? (await waitForActivation(installing ?? waiting!))
worker.postMessage({ __type: 'evaluate', code })
function waitForActivation(sw: ServiceWorker): Promise<ServiceWorker> {
return new Promise((resolve) => {
sw.addEventListener('statechange', () => {
if (sw.state === 'activated') {
resolve(sw)
}
})
})
}
}, `(${fn.toString()})()`)
},
flushBrowserLogs: () => browserLogsManager.clear(),
flushEvents: () => flushEvents(page),
deleteAllCookies: () => deleteAllCookies(browserContext),
sendXhr: (url: string, headers?: string[][]) => sendXhr(page, url, headers),
getExtensionId: async () => {
let [background] = browserContext.serviceWorkers()
if (!background) {
background = await browserContext.waitForEvent('serviceworker')
}
const extensionId = background.url().split('/')[2]
return extensionId || ''
},
}
}
async function setUpTest(browserLogsManager: BrowserLogsManager, { baseUrl, page, browserContext }: TestContext) {
browserContext.on('console', (msg) => {
browserLogsManager.add({
level: msg.type() as BrowserLog['level'],
message: msg.text(),
source: 'console',
timestamp: Date.now(),
})
})
browserContext.on('weberror', (webError) => {
browserLogsManager.add({
level: 'error',
message: webError.error().message,
source: 'console',
timestamp: Date.now(),
})
})
await page.goto(baseUrl)
await waitForServersIdle()
}
function tearDownPassedTest({ intakeRegistry, withBrowserLogs }: TestContext) {
expect(intakeRegistry.telemetryErrorEvents).toHaveLength(0)
expect(() => validateRumFormat(intakeRegistry.rumEvents)).not.toThrow()
withBrowserLogs((logs) => {
expect(logs.filter((log) => log.level === 'error')).toHaveLength(0)
})
}
async function tearDownTest({ flushEvents, deleteAllCookies }: TestContext) {
await flushEvents()
await deleteAllCookies()
if (test.info().status === 'passed' && test.info().retry > 0) {
addTag('test.flaky', 'true')
}
const skipReason = test.info().annotations.find((annotation) => annotation.type === 'skip')?.description
if (skipReason) {
addTag('test.skip', skipReason)
}
const fixmeReason = test.info().annotations.find((annotation) => annotation.type === 'fixme')?.description
if (fixmeReason) {
addTag('test.fixme', fixmeReason)
}
}