From a15ca44b456c91e3519b6d394c7cebf4d96d4f38 Mon Sep 17 00:00:00 2001 From: Siarhei_Kliushnikau Date: Mon, 30 Sep 2024 18:41:21 +0200 Subject: [PATCH] added PLAYWRIGHT_JUNIT_PROPERTIES --- packages/playwright-core/src/utils/env.ts | 26 +++++++++++++++++++++- packages/playwright/src/reporters/junit.ts | 18 ++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/playwright-core/src/utils/env.ts b/packages/playwright-core/src/utils/env.ts index 2a4dd0bfd4..769465981c 100644 --- a/packages/playwright-core/src/utils/env.ts +++ b/packages/playwright-core/src/utils/env.ts @@ -17,7 +17,7 @@ export function getFromENV(name: string): string | undefined { let value = process.env[name]; value = value === undefined ? process.env[`npm_config_${name.toLowerCase()}`] : value; - value = value === undefined ? process.env[`npm_package_config_${name.toLowerCase()}`] : value; + value = value === undefined ? process.env[`npm_package_config_${name.toLowerCase()}`] : value; return value; } @@ -47,3 +47,27 @@ export function getPackageManagerExecCommand() { return 'pnpm exec'; return 'npx'; } + +export function getParsedFromEnv(key: string): Array<{name: string, value: string}> { + const envValue = getFromENV(key); + if (envValue) { + const commaSplittedValuesInEnv = [...new Set(envValue.split(','))].map(prop => { + let property = prop.split('='); + if (property.length === 2 && property[0] && property[1]) + return {name: property[0].trim(), value: property[1].trim()}; + }).filter(item => !!item) + const uniqueItems = []; + const mapWithUniqueName = new Map(); + for (const value of commaSplittedValuesInEnv) { + const key = Object.values(value)[0]; + if (!mapWithUniqueName.has(key)) { + mapWithUniqueName.set(key, true); + uniqueItems.push(value); + } + } + return uniqueItems; + } + return []; +} + + diff --git a/packages/playwright/src/reporters/junit.ts b/packages/playwright/src/reporters/junit.ts index befd652e4b..4c4a63cce1 100644 --- a/packages/playwright/src/reporters/junit.ts +++ b/packages/playwright/src/reporters/junit.ts @@ -18,7 +18,7 @@ import fs from 'fs'; import path from 'path'; import type { FullConfig, FullResult, Suite, TestCase } from '../../types/testReporter'; import { formatFailure, resolveOutputFile, stripAnsiEscapes } from './base'; -import { getAsBooleanFromENV } from 'playwright-core/lib/utils'; +import { getAsBooleanFromENV, getParsedFromEnv } from 'playwright-core/lib/utils'; import type { ReporterV2 } from './reporterV2'; type JUnitOptions = { @@ -40,12 +40,17 @@ class JUnitReporter implements ReporterV2 { private resolvedOutputFile: string | undefined; private stripANSIControlSequences = false; private includeProjectInTestName = false; + private propertiesFromEnv: Array<{ + name: string; + value: string; + }> = []; constructor(options: JUnitOptions) { this.stripANSIControlSequences = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_STRIP_ANSI', !!options.stripANSIControlSequences); this.includeProjectInTestName = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_INCLUDE_PROJECT_IN_TEST_NAME', !!options.includeProjectInTestName); this.configDir = options.configDir; this.resolvedOutputFile = resolveOutputFile('JUNIT', options)?.outputFile; + this.propertiesFromEnv = getParsedFromEnv('PLAYWRIGHT_JUNIT_PROPERTIES'); } version(): 'v2' { @@ -173,6 +178,17 @@ class JUnitReporter implements ReporterV2 { properties.children?.push(property); } + for (const propertyFromEnv of this.propertiesFromEnv) { + const property: XMLEntry = { + name: 'property', + attributes: { + name: propertyFromEnv.name, + value: propertyFromEnv.value + } + }; + properties.children?.push(property); + } + if (properties.children?.length) entry.children.push(properties);