Skip to content

Commit f24d815

Browse files
[test-optimization] [SDTEST-1163] Playwright active test span (#4843)
1 parent b984e63 commit f24d815

File tree

12 files changed

+463
-63
lines changed

12 files changed

+463
-63
lines changed

ci/init.js

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const isJestWorker = !!process.env.JEST_WORKER_ID
77
const isCucumberWorker = !!process.env.CUCUMBER_WORKER_ID
88
const isMochaWorker = !!process.env.MOCHA_WORKER_ID
99

10+
const isPlaywrightWorker = !!process.env.DD_PLAYWRIGHT_WORKER
11+
1012
const packageManagers = [
1113
'npm',
1214
'yarn',
@@ -67,6 +69,12 @@ if (isMochaWorker) {
6769
}
6870
}
6971

72+
if (isPlaywrightWorker) {
73+
options.experimental = {
74+
exporter: 'playwright_worker'
75+
}
76+
}
77+
7078
if (shouldInit) {
7179
tracer.init(options)
7280
tracer.use('fs', false)

ext/exporters.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ declare const exporters: {
55
AGENT_PROXY: 'agent_proxy',
66
JEST_WORKER: 'jest_worker',
77
CUCUMBER_WORKER: 'cucumber_worker',
8-
MOCHA_WORKER: 'mocha_worker'
8+
MOCHA_WORKER: 'mocha_worker',
9+
PLAYWRIGHT_WORKER: 'playwright_worker'
910
}
1011

1112
export = exporters

ext/exporters.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports = {
66
AGENT_PROXY: 'agent_proxy',
77
JEST_WORKER: 'jest_worker',
88
CUCUMBER_WORKER: 'cucumber_worker',
9-
MOCHA_WORKER: 'mocha_worker'
9+
MOCHA_WORKER: 'mocha_worker',
10+
PLAYWRIGHT_WORKER: 'playwright_worker'
1011
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { test, expect } = require('@playwright/test')
2+
const tracer = require('dd-trace')
3+
4+
test.beforeEach(async ({ page }) => {
5+
await page.goto(process.env.PW_BASE_URL)
6+
})
7+
8+
test.describe('playwright', () => {
9+
test('should be able to grab the active test span and add a custom span', async ({ page }) => {
10+
const customSpan = tracer.startSpan('my custom span', {
11+
childOf: tracer.scope().active()
12+
})
13+
14+
customSpan.addTags({
15+
'test.really_custom_tag': 'this is really custom'
16+
})
17+
18+
customSpan.finish()
19+
20+
await expect(page.locator('.hello-world')).toHaveText([
21+
'Hello World'
22+
])
23+
})
24+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { test, expect } = require('@playwright/test')
2+
const tracer = require('dd-trace')
3+
4+
test.beforeEach(async ({ page }) => {
5+
await page.goto(process.env.PW_BASE_URL)
6+
})
7+
8+
test.describe('playwright', () => {
9+
test('should be able to grab the active test span', async ({ page }) => {
10+
const testSpan = tracer.scope().active()
11+
12+
testSpan.addTags({
13+
'test.custom_tag': 'this is custom'
14+
})
15+
16+
await expect(page.locator('.hello-world')).toHaveText([
17+
'Hello World'
18+
])
19+
})
20+
})

integration-tests/playwright/playwright.spec.js

+68
Original file line numberDiff line numberDiff line change
@@ -1350,5 +1350,73 @@ versions.forEach((version) => {
13501350
})
13511351
})
13521352
})
1353+
1354+
if (version === 'latest') {
1355+
context('active test span', () => {
1356+
it('can grab the test span and add tags', (done) => {
1357+
const receiverPromise = receiver
1358+
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', (payloads) => {
1359+
const events = payloads.flatMap(({ payload }) => payload.events)
1360+
1361+
const test = events.find(event => event.type === 'test').content
1362+
1363+
assert.equal(test.meta['test.custom_tag'], 'this is custom')
1364+
})
1365+
1366+
childProcess = exec(
1367+
'./node_modules/.bin/playwright test -c playwright.config.js active-test-span-tags-test.js',
1368+
{
1369+
cwd,
1370+
env: {
1371+
...getCiVisAgentlessConfig(receiver.port),
1372+
PW_BASE_URL: `http://localhost:${webAppPort}`,
1373+
TEST_DIR: './ci-visibility/playwright-tests-active-test-span'
1374+
},
1375+
stdio: 'pipe'
1376+
}
1377+
)
1378+
1379+
childProcess.on('exit', () => {
1380+
receiverPromise.then(() => done()).catch(done)
1381+
})
1382+
})
1383+
1384+
it('can grab the test span and add spans', (done) => {
1385+
const receiverPromise = receiver
1386+
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', (payloads) => {
1387+
const events = payloads.flatMap(({ payload }) => payload.events)
1388+
1389+
const test = events.find(event => event.type === 'test').content
1390+
const spans = events.filter(event => event.type === 'span').map(event => event.content)
1391+
1392+
const customSpan = spans.find(span => span.name === 'my custom span')
1393+
1394+
assert.exists(customSpan)
1395+
assert.equal(customSpan.meta['test.really_custom_tag'], 'this is really custom')
1396+
1397+
// custom span is children of active test span
1398+
assert.equal(customSpan.trace_id.toString(), test.trace_id.toString())
1399+
assert.equal(customSpan.parent_id.toString(), test.span_id.toString())
1400+
})
1401+
1402+
childProcess = exec(
1403+
'./node_modules/.bin/playwright test -c playwright.config.js active-test-span-custom-span-test.js',
1404+
{
1405+
cwd,
1406+
env: {
1407+
...getCiVisAgentlessConfig(receiver.port),
1408+
PW_BASE_URL: `http://localhost:${webAppPort}`,
1409+
TEST_DIR: './ci-visibility/playwright-tests-active-test-span'
1410+
},
1411+
stdio: 'pipe'
1412+
}
1413+
)
1414+
1415+
childProcess.on('exit', () => {
1416+
receiverPromise.then(() => done()).catch(done)
1417+
})
1418+
})
1419+
})
1420+
}
13531421
})
13541422
})

0 commit comments

Comments
 (0)