Skip to content

Commit

Permalink
[ci-visibility] Add browser name as test configuration in playwright (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez authored and tlhunter committed Feb 14, 2024
1 parent f068e70 commit 3fd8ab2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
5 changes: 4 additions & 1 deletion integration-tests/playwright/playwright.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const {
TEST_STATUS,
TEST_SOURCE_START,
TEST_TYPE,
TEST_SOURCE_FILE
TEST_SOURCE_FILE,
TEST_CONFIGURATION_BROWSER_NAME
} = require('../../packages/dd-trace/src/plugins/util/test')

const versions = ['1.18.0', 'latest']
Expand Down Expand Up @@ -110,6 +111,8 @@ versions.forEach((version) => {
// Can read DD_TAGS
assert.propertyVal(testEvent.content.meta, 'test.customtag', 'customvalue')
assert.propertyVal(testEvent.content.meta, 'test.customtag2', 'customvalue2')
// Adds the browser used
assert.propertyVal(testEvent.content.meta, TEST_CONFIGURATION_BROWSER_NAME, 'chromium')
})

stepEvents.forEach(stepEvent => {
Expand Down
50 changes: 41 additions & 9 deletions packages/datadog-instrumentations/src/playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,41 @@ function getRootDir (playwrightRunner) {
if (playwrightRunner._configDir) {
return playwrightRunner._configDir
}
if (playwrightRunner._config && playwrightRunner._config.config) {
return playwrightRunner._config.config.rootDir
if (playwrightRunner._config) {
return playwrightRunner._config.config?.rootDir || process.cwd()
}
return process.cwd()
}

function testBeginHandler (test) {
const { _requireFile: testSuiteAbsolutePath, title: testName, _type, location: { line: testSourceLine } } = test
function getProjectsFromRunner (runner) {
const config = getPlaywrightConfig(runner)
return config.projects?.map(({ project }) => project)
}

function getProjectsFromDispatcher (dispatcher) {
const newConfig = dispatcher._config?.config?.projects
if (newConfig) {
return newConfig
}
// old
return dispatcher._loader?.fullConfig()?.projects
}

function getBrowserNameFromProjects (projects, projectId) {
if (!projects) {
return null
}
return projects.find(project =>
project.__projectId === projectId || project._id === projectId
)?.name
}

function testBeginHandler (test, browserName) {
const {
_requireFile: testSuiteAbsolutePath,
title: testName, _type,
location: { line: testSourceLine }
} = test

if (_type === 'beforeAll' || _type === 'afterAll') {
return
Expand All @@ -100,7 +127,7 @@ function testBeginHandler (test) {
const testAsyncResource = new AsyncResource('bound-anonymous-fn')
testToAr.set(test, testAsyncResource)
testAsyncResource.runInAsyncScope(() => {
testStartCh.publish({ testName, testSuiteAbsolutePath, testSourceLine })
testStartCh.publish({ testName, testSuiteAbsolutePath, testSourceLine, browserName })
})
}

Expand Down Expand Up @@ -166,11 +193,12 @@ function dispatcherHook (dispatcherExport) {
shimmer.wrap(dispatcherExport.Dispatcher.prototype, '_createWorker', createWorker => function () {
const dispatcher = this
const worker = createWorker.apply(this, arguments)

worker.process.on('message', ({ method, params }) => {
if (method === 'testBegin') {
const { test } = dispatcher._testById.get(params.testId)
testBeginHandler(test)
const projects = getProjectsFromDispatcher(dispatcher)
const browser = getBrowserNameFromProjects(projects, test._projectId)
testBeginHandler(test, browser)
} else if (method === 'testEnd') {
const { test } = dispatcher._testById.get(params.testId)

Expand Down Expand Up @@ -203,7 +231,9 @@ function dispatcherHookNew (dispatcherExport, runWrapper) {

worker.on('testBegin', ({ testId }) => {
const test = getTestByTestId(dispatcher, testId)
testBeginHandler(test)
const projects = getProjectsFromDispatcher(dispatcher)
const browser = getBrowserNameFromProjects(projects, test._projectId)
testBeginHandler(test, browser)
})
worker.on('testEnd', ({ testId, status, errors, annotations }) => {
const test = getTestByTestId(dispatcher, testId)
Expand All @@ -226,6 +256,7 @@ function runnerHook (runnerExport, playwrightVersion) {
testSessionAsyncResource.runInAsyncScope(() => {
testSessionStartCh.publish({ command, frameworkVersion: playwrightVersion, rootDir })
})
const projects = getProjectsFromRunner(this)

const runAllTestsReturn = await runAllTests.apply(this, arguments)

Expand All @@ -234,7 +265,8 @@ function runnerHook (runnerExport, playwrightVersion) {
// there were tests that did not go through `testBegin` or `testEnd`,
// because they were skipped
tests.forEach(test => {
testBeginHandler(test)
const browser = getBrowserNameFromProjects(projects, test._projectId)
testBeginHandler(test, browser)
testEndHandler(test, [], 'skip')
})
})
Expand Down
12 changes: 8 additions & 4 deletions packages/datadog-plugin-playwright/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const {
getTestSuiteCommonTags,
TEST_SOURCE_START,
TEST_CODE_OWNERS,
TEST_SOURCE_FILE
TEST_SOURCE_FILE,
TEST_CONFIGURATION_BROWSER_NAME
} = require('../../dd-trace/src/plugins/util/test')
const { RESOURCE_NAME } = require('../../../ext/tags')
const { COMPONENT } = require('../../dd-trace/src/constants')
Expand Down Expand Up @@ -77,11 +78,11 @@ class PlaywrightPlugin extends CiPlugin {
this.telemetry.ciVisEvent(TELEMETRY_EVENT_FINISHED, 'suite')
})

this.addSub('ci:playwright:test:start', ({ testName, testSuiteAbsolutePath, testSourceLine }) => {
this.addSub('ci:playwright:test:start', ({ testName, testSuiteAbsolutePath, testSourceLine, browserName }) => {
const store = storage.getStore()
const testSuite = getTestSuitePath(testSuiteAbsolutePath, this.rootDir)
const testSourceFile = getTestSuitePath(testSuiteAbsolutePath, this.repositoryRoot)
const span = this.startTestSpan(testName, testSuite, testSourceFile, testSourceLine)
const span = this.startTestSpan(testName, testSuite, testSourceFile, testSourceLine, browserName)

this.enter(span, store)
})
Expand Down Expand Up @@ -128,7 +129,7 @@ class PlaywrightPlugin extends CiPlugin {
})
}

startTestSpan (testName, testSuite, testSourceFile, testSourceLine) {
startTestSpan (testName, testSuite, testSourceFile, testSourceLine, browserName) {
const testSuiteSpan = this._testSuites.get(testSuite)

const extraTags = {
Expand All @@ -137,6 +138,9 @@ class PlaywrightPlugin extends CiPlugin {
if (testSourceFile) {
extraTags[TEST_SOURCE_FILE] = testSourceFile || testSuite
}
if (browserName) {
extraTags[TEST_CONFIGURATION_BROWSER_NAME] = browserName
}

return super.startTestSpan(testName, testSuite, testSuiteSpan, extraTags)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/dd-trace/src/plugins/util/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const TEST_MODULE_ID = 'test_module_id'
const TEST_SUITE_ID = 'test_suite_id'
const TEST_TOOLCHAIN = 'test.toolchain'
const TEST_SKIPPED_BY_ITR = 'test.skipped_by_itr'
// Browser used in browser test. Namespaced by test.configuration because it affects the fingerprint
const TEST_CONFIGURATION_BROWSER_NAME = 'test.configuration.browser_name'
// Early flake detection
const TEST_IS_NEW = 'test.is_new'
const TEST_EARLY_FLAKE_IS_RETRY = 'test.early_flake.is_retry'
const TEST_EARLY_FLAKE_IS_ENABLED = 'test.early_flake.is_enabled'
Expand Down Expand Up @@ -90,6 +93,7 @@ module.exports = {
JEST_WORKER_COVERAGE_PAYLOAD_CODE,
TEST_SOURCE_START,
TEST_SKIPPED_BY_ITR,
TEST_CONFIGURATION_BROWSER_NAME,
TEST_IS_NEW,
TEST_EARLY_FLAKE_IS_RETRY,
TEST_EARLY_FLAKE_IS_ENABLED,
Expand Down

0 comments on commit 3fd8ab2

Please sign in to comment.