added PLAYWRIGHT_JUNIT_PROPERTIES

This commit is contained in:
Siarhei_Kliushnikau 2024-09-30 18:41:21 +02:00
parent b3a82bef46
commit a15ca44b45
2 changed files with 42 additions and 2 deletions

View file

@ -47,3 +47,27 @@ export function getPackageManagerExecCommand() {
return 'pnpm exec'; return 'pnpm exec';
return 'npx'; 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 [];
}

View file

@ -18,7 +18,7 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import type { FullConfig, FullResult, Suite, TestCase } from '../../types/testReporter'; import type { FullConfig, FullResult, Suite, TestCase } from '../../types/testReporter';
import { formatFailure, resolveOutputFile, stripAnsiEscapes } from './base'; 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'; import type { ReporterV2 } from './reporterV2';
type JUnitOptions = { type JUnitOptions = {
@ -40,12 +40,17 @@ class JUnitReporter implements ReporterV2 {
private resolvedOutputFile: string | undefined; private resolvedOutputFile: string | undefined;
private stripANSIControlSequences = false; private stripANSIControlSequences = false;
private includeProjectInTestName = false; private includeProjectInTestName = false;
private propertiesFromEnv: Array<{
name: string;
value: string;
}> = [];
constructor(options: JUnitOptions) { constructor(options: JUnitOptions) {
this.stripANSIControlSequences = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_STRIP_ANSI', !!options.stripANSIControlSequences); this.stripANSIControlSequences = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_STRIP_ANSI', !!options.stripANSIControlSequences);
this.includeProjectInTestName = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_INCLUDE_PROJECT_IN_TEST_NAME', !!options.includeProjectInTestName); this.includeProjectInTestName = getAsBooleanFromENV('PLAYWRIGHT_JUNIT_INCLUDE_PROJECT_IN_TEST_NAME', !!options.includeProjectInTestName);
this.configDir = options.configDir; this.configDir = options.configDir;
this.resolvedOutputFile = resolveOutputFile('JUNIT', options)?.outputFile; this.resolvedOutputFile = resolveOutputFile('JUNIT', options)?.outputFile;
this.propertiesFromEnv = getParsedFromEnv('PLAYWRIGHT_JUNIT_PROPERTIES');
} }
version(): 'v2' { version(): 'v2' {
@ -173,6 +178,17 @@ class JUnitReporter implements ReporterV2 {
properties.children?.push(property); 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) if (properties.children?.length)
entry.children.push(properties); entry.children.push(properties);