Skip to content

Commit

Permalink
Merge pull request #77 from alexneo2003/76-ado-test-case-tag-with-prefix
Browse files Browse the repository at this point in the history
76 ado test case tag with prefix
  • Loading branch information
alexneo2003 authored Sep 19, 2024
2 parents cc73c65 + d5a94aa commit 9d89fe9
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
yarn-error.log
.vscode
.taskkey
.env
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# [1.11.0](https://github.com/alexneo2003/playwright-azure-reporter/compare/v1.11.0-beta.0...v1.11.0) (2024-09-19)



# [1.11.0-beta.0](https://github.com/alexneo2003/playwright-azure-reporter/compare/v1.10.1...v1.11.0-beta.0) (2024-09-17)



## [1.10.1](https://github.com/alexneo2003/playwright-azure-reporter/compare/v1.10.0...v1.10.1) (2024-08-09)


Expand Down
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const config: PlaywrightTestConfig = {
publishTestResultsMode: 'testRun',
uploadAttachments: true,
attachmentsType: ['screenshot', 'video', 'trace'],
testCaseIdMatcher: /@\[(\d+)\]/,
testRunConfig: {
owner: {
displayName: 'Alex Neo',
Expand Down Expand Up @@ -210,6 +211,48 @@ Reporter options (\* - required):
> **Note:** If you use `isExistingTestRun` mode, test run doesn't complete automatically. You should complete it manually.
- `testCaseIdMatcher` [string|RegExp|string[]|RegExp[]] - A string or a regular expression to match the name of the test case to extract the test case id. Default: `/\[([\d,\s]+)\]/`

#### Example Test Titles

- Test title: `Test case @tag1=123`

- `testCaseIdMatcher: /@tag1=(\d+)/`
- Extracted tags: `['123']`

- Test title: `Test case @TestCase=123 [@TestCase=456]`

- `testCaseIdMatcher: /@TestCase=(\d+)/`
- Extracted tags: `['123', '456']`

- Test title: `Test case test123 TEST456`
- `testCaseIdMatcher: [/[a-z]+(\d+)/, /[A-Z]+(\d+)/]`
- Extracted tags: `['123', '456']`
- Test title: `Test case @tag1=123 @tag2=456`
- `testCaseIdMatcher: ['@tag1=(\\d+)', '@tag2=(\\d+)']`
- Extracted tags: `['123', '456']`

#### Error Handling

If an invalid `testCaseIdMatcher` is provided, an error will be thrown. For example:

```typescript
reporter: [
['list'],
[
'@alex_neo/playwright-azure-reporter',
{
orgUrl: 'http://localhost:4000',
projectName: 'SampleProject',
planId: 4,
token: 'your-token',
isDisabled: false,
testCaseIdMatcher: 1234, // Invalid pattern
}
],
// This will throw an error: "Invalid testCaseIdMatcher. Must be a string or RegExp. Actual: 1234"
```
## Usefulness
- **AZURE_PW_TEST_RUN_ID** - Id of current test run. It will be set in environment variables after test run created. Can be accessed by `process.env.AZURE_PW_TEST_RUN_ID`. Pay attention what `publishTestResultsMode` configuration you use. If you use `testResult` mode - this variable will be set when test run created, at the start of tests execution, if you use `testRun` mode - this variable will be set when test run completed, at the end of tests execution.
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alex_neo/playwright-azure-reporter",
"version": "1.10.1",
"version": "1.11.0",
"description": "Playwright Azure Reporter",
"main": "./dist/playwright-azure-reporter.js",
"types": "./dist/playwright-azure-reporter.d.js",
Expand Down Expand Up @@ -38,10 +38,11 @@
},
"license": "ISC",
"dependencies": {
"azure-devops-node-api": "^12.0.0",
"azure-devops-node-api": "^14.0.2",
"azure-pipelines-task-lib": "^4.15.0",
"chalk": "4.1.2",
"debug": "^4.3.4",
"dotenv": "^16.4.5",
"mime": "^3.0.0"
},
"devDependencies": {
Expand Down
43 changes: 36 additions & 7 deletions src/playwright-azure-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as Test from 'azure-devops-node-api/TestApi';
import { setVariable } from 'azure-pipelines-task-lib';
import chalk from 'chalk';
import { existsSync, readFileSync } from 'fs';
import { isRegExp } from 'util/types';

import Logger from './logger';
import { createGuid, getExtensionFromContentType, getExtensionFromFilename, shortID } from './utils';
Expand Down Expand Up @@ -55,6 +56,7 @@ export interface AzureReporterOptions {
testPointMapper?: (testCase: TestCase, testPoints: TestPoint[]) => Promise<TestPoint[] | undefined>;
isExistingTestRun?: boolean;
testRunId?: number;
testCaseIdMatcher?: string | RegExp | Array<string | RegExp>;
}

interface TestResultsToTestRun {
Expand Down Expand Up @@ -142,6 +144,7 @@ class AzureDevOpsReporter implements Reporter {
private _publishTestResultsMode: TPublishTestResults = 'testResult';
private _testRunId: number | undefined;
private _isExistingTestRun = false;
private _testCaseIdMatcher: string | RegExp | Array<string | RegExp> = new RegExp(/\[([\d,\s]+)\]/, 'g');

public constructor(options: AzureReporterOptions) {
this._runIdPromise = new Promise<number | void>((resolve, reject) => {
Expand Down Expand Up @@ -259,6 +262,7 @@ class AzureDevOpsReporter implements Reporter {
this._testPointMapper = options.testPointMapper;
}
this._isExistingTestRun = options.isExistingTestRun || false;
this._testCaseIdMatcher = options.testCaseIdMatcher || new RegExp(/\[([\d,\s]+)\]/, 'g');
}

async onBegin(): Promise<void> {
Expand Down Expand Up @@ -396,10 +400,9 @@ class AzureDevOpsReporter implements Reporter {
}

private _anonymizeObject(obj: any, keys: string[]): any {
if (typeof obj !== 'object') return obj;
if (Array.isArray(obj)) {
return obj.map((item) => this._anonymizeObject(item, keys));
}
if (typeof obj !== 'object' || obj === null) return obj;
if (Array.isArray(obj)) return obj.map((item) => this._anonymizeObject(item, keys));
if (isRegExp(obj)) return obj.toString();
const result: any = {};
for (const key in obj) {
if (keys.includes(key)) {
Expand All @@ -412,21 +415,47 @@ class AzureDevOpsReporter implements Reporter {
}

private _extractMatches(text: string): string[] {
const regex = new RegExp(/\[([\d,\s]+)\]/, 'gm');
const matchesAll = text.matchAll(regex);
return [...matchesAll].map((match) => match[1]);
const reList = (Array.isArray(this._testCaseIdMatcher) ? this._testCaseIdMatcher : [this._testCaseIdMatcher]).map(
(re) => {
if (typeof re === 'string') {
return new RegExp(re, 'g');
} else if (!isRegExp(re)) {
throw new Error(`Invalid testCaseIdMatcher. Must be a string or RegExp. Actual: ${re}`);
}
return re;
}
);

this._logger?.debug(`Extracting matches from text: ${text}`);
this._logger?.debug(`Using matchers: ${reList}`);

const matchesResult: string[] = [];
for (const re of reList) {
this._logger?.debug(`Using matcher: ${re}`);
const matchesAll = text.matchAll(new RegExp(re, 'g'));
for (const match of matchesAll) {
this._logger?.debug(`[_extractMatches] Whole matches found: ${match}`);
if (match && match[1]) {
this._logger?.debug(`[_extractMatches] Matches found: ${match[1]}`);
matchesResult.push(match[1]);
}
}
}
return matchesResult;
}

private _getCaseIds(test: TestCase): string[] {
const result: string[] = [];
const matches = this._extractMatches(test.title);
this._logger?.debug(`[_getCaseIds] Matches found: ${matches}`);
matches.forEach((match) => {
const ids = match.split(',').map((id) => id.trim());
result.push(...ids);
});
if (test.tags) {
test.tags.forEach((tag) => {
const ids = this._extractMatches(tag);
this._logger?.debug(`[_getCaseIds] Matches found in tag: ${ids}`);
ids.forEach((id) => {
result.push(id);
});
Expand Down
11 changes: 11 additions & 0 deletions tests/reporter/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { defineConfig } from '@playwright/test';
import dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';

const envPath = path.resolve(__dirname, '../../.env');

if (fs.existsSync(envPath)) {
dotenv.config({ path: envPath });
} else {
console.warn(`.env file not found at ${envPath}, skipping dotenv configuration.`);
}

export default defineConfig({
testDir: __dirname,
Expand Down
Loading

0 comments on commit 9d89fe9

Please sign in to comment.