Skip to content

Commit 2872038

Browse files
[test optimization] Playwright + RUM: wait for RUM data to flush (#6759)
1 parent f0c1e65 commit 2872038

File tree

5 files changed

+52
-22
lines changed

5 files changed

+52
-22
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const { test, expect } = require('@playwright/test')
4+
5+
test.beforeEach(async ({ page }) => {
6+
await page.goto(process.env.PW_BASE_URL)
7+
})
8+
9+
test.describe('playwright', () => {
10+
test('should have RUM active', async ({ page }) => {
11+
await expect(page.locator('.hello-world')).toHaveText([
12+
'Hello World'
13+
])
14+
await page.goto(`${process.env.PW_BASE_URL}/another-page`)
15+
await expect(page.locator('.hello-world')).toHaveText([
16+
'Hello World'
17+
])
18+
})
19+
})

integration-tests/playwright/playwright.spec.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,25 +1700,26 @@ versions.forEach((version) => {
17001700
receiver
17011701
.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', (payloads) => {
17021702
const events = payloads.flatMap(({ payload }) => payload.events)
1703-
const playwrightTest = events.find(event => event.type === 'test').content
1704-
if (isRedirecting) {
1705-
assert.notProperty(playwrightTest.meta, TEST_IS_RUM_ACTIVE)
1706-
assert.notProperty(playwrightTest.meta, TEST_BROWSER_VERSION)
1707-
} else {
1708-
assert.property(playwrightTest.meta, TEST_IS_RUM_ACTIVE, 'true')
1709-
assert.property(playwrightTest.meta, TEST_BROWSER_VERSION)
1710-
}
1711-
assert.include(playwrightTest.meta, {
1712-
[TEST_BROWSER_NAME]: 'chromium',
1713-
[TEST_TYPE]: 'browser'
1703+
const tests = events.filter(event => event.type === 'test').map(event => event.content)
1704+
tests.forEach(test => {
1705+
if (isRedirecting) {
1706+
// can't do assertions because playwright has been redirected
1707+
assert.propertyVal(test.meta, TEST_STATUS, 'fail')
1708+
assert.notProperty(test.meta, TEST_IS_RUM_ACTIVE)
1709+
assert.notProperty(test.meta, TEST_BROWSER_VERSION)
1710+
} else {
1711+
assert.propertyVal(test.meta, TEST_STATUS, 'pass')
1712+
assert.property(test.meta, TEST_IS_RUM_ACTIVE, 'true')
1713+
assert.property(test.meta, TEST_BROWSER_VERSION)
1714+
}
17141715
})
17151716
})
17161717

1717-
const runTest = (done, { isRedirecting }, extraEnvVars) => {
1718+
const runRumTest = async ({ isRedirecting }, extraEnvVars) => {
17181719
const testAssertionsPromise = getTestAssertions({ isRedirecting })
17191720

17201721
childProcess = exec(
1721-
'./node_modules/.bin/playwright test -c playwright.config.js active-test-span-rum-test.js',
1722+
'./node_modules/.bin/playwright test -c playwright.config.js',
17221723
{
17231724
cwd,
17241725
env: {
@@ -1731,17 +1732,18 @@ versions.forEach((version) => {
17311732
}
17321733
)
17331734

1734-
childProcess.on('exit', () => {
1735-
testAssertionsPromise.then(() => done()).catch(done)
1736-
})
1735+
await Promise.all([
1736+
once(childProcess, 'exit'),
1737+
testAssertionsPromise
1738+
])
17371739
}
17381740

1739-
it('can correlate tests and RUM sessions', (done) => {
1740-
runTest(done, { isRedirecting: false })
1741+
it('can correlate tests and RUM sessions', async () => {
1742+
await runRumTest({ isRedirecting: false })
17411743
})
17421744

1743-
it('do not crash when redirecting and RUM sessions are not active', (done) => {
1744-
runTest(done, { isRedirecting: true })
1745+
it('do not crash when redirecting and RUM sessions are not active', async () => {
1746+
await runRumTest({ isRedirecting: true })
17451747
})
17461748
})
17471749

packages/datadog-instrumentations/src/playwright.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const testSuiteToTestStatuses = new Map()
3838
const testSuiteToErrors = new Map()
3939
const testsToTestStatuses = new Map()
4040

41+
const RUM_FLUSH_WAIT_TIME = 1000
42+
4143
let applyRepeatEachIndex = null
4244

4345
let startedSuites = []
@@ -1134,16 +1136,19 @@ addHook({
11341136
})
11351137

11361138
if (isRumActive) {
1139+
// Give some time RUM to flush data, similar to what we do in selenium
1140+
await new Promise(resolve => setTimeout(resolve, RUM_FLUSH_WAIT_TIME))
11371141
const url = page.url()
11381142
if (url) {
11391143
const domain = new URL(url).hostname
11401144
await page.context().addCookies([{
11411145
name: 'datadog-ci-visibility-test-execution-id',
11421146
value: '',
11431147
domain,
1144-
expires: 0,
11451148
path: '/'
11461149
}])
1150+
} else {
1151+
log.error('RUM is active but page.url() is not available')
11471152
}
11481153
}
11491154
}

packages/datadog-plugin-playwright/src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const {
4747
TELEMETRY_EVENT_FINISHED
4848
} = require('../../dd-trace/src/ci-visibility/telemetry')
4949
const { appClosing: appClosingTelemetry } = require('../../dd-trace/src/telemetry')
50+
const log = require('../../dd-trace/src/log')
5051

5152
class PlaywrightPlugin extends CiPlugin {
5253
static id = 'playwright'
@@ -174,7 +175,10 @@ class PlaywrightPlugin extends CiPlugin {
174175
}) => {
175176
const store = storage('legacy').getStore()
176177
const span = store && store.span
177-
if (!span) return
178+
if (!span) {
179+
log.error('ci:playwright:test:page-goto: test span not found')
180+
return
181+
}
178182

179183
if (isRumActive) {
180184
span.setTag(TEST_IS_RUM_ACTIVE, 'true')

0 commit comments

Comments
 (0)