Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add existingTestRun publishing mode #53

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# [1.8.0](https://github.com/alexneo2003/playwright-azure-reporter/compare/v1.7.0...v1.8.0) (2023-11-21)


### Features

* set existing test run ID ([3c2bfb3](https://github.com/alexneo2003/playwright-azure-reporter/commit/3c2bfb31fad8427dd1206202d934812a78290b27))



# [1.7.0](https://github.com/alexneo2003/playwright-azure-reporter/compare/v1.7.0-beta.0...v1.7.0) (2023-11-21)


Expand Down
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# Playwright Azure Reporter

![GitHub](https://img.shields.io/github/license/alexneo2003/playwright-azure-reporter) ![npm (scoped)](https://img.shields.io/npm/v/@alex_neo/playwright-azure-reporter) ![npm](https://img.shields.io/npm/dw/@alex_neo/playwright-azure-reporter) ![npm](https://img.shields.io/npm/dt/@alex_neo/playwright-azure-reporter)

## A must read!

**Since version 1.5.0 reporter allows using configurationIds to publish results for different configurations e.g. different browsers**
**Necessarily defining `testRun.configurationIds` or/and `testPointMapper` function in reporter config, otherwise reporter will be publishing results for all configurations**


## How to integrate

Install package

```bash
npm install @alex_neo/playwright-azure-reporter
```
or

or

```bash
yarn add @alex_neo/playwright-azure-reporter
```
Expand Down Expand Up @@ -108,11 +112,11 @@ const config: PlaywrightTestConfig = {
displayName: 'Alex Neo',
},
comment: 'Playwright Test Run',
// the configuration ids of this test run, use
// the configuration ids of this test run, use
// https://dev.azure.com/{organization}/{project}/_apis/test/configurations to get the ids of your project.
// if multiple configuration ids are used in one run a testPointMapper should be used to pick the correct one,
// if multiple configuration ids are used in one run a testPointMapper should be used to pick the correct one,
// otherwise the results are pushed to all.
configurationIds: [ 1 ],
configurationIds: [1],
},
} as AzureReporterOptions,
],
Expand Down Expand Up @@ -146,6 +150,7 @@ Reporter options (\* - required):
- `testRunTitle` - Title of test run using to create new test run. Default: `Playwright Test Run`.
- `testRunConfig` - Extra data to pass when Test Run creating. Read [doc](https://learn.microsoft.com/en-us/rest/api/azure/devops/test/runs/create?view=azure-devops-rest-7.1&tabs=HTTP#request-body) from more information. Default: `empty`.
- `testPointMapper` - A callback to map the test runs to test configurations, e.g. by browser

```
testPointMapper: async (testCase: TestCase, testPoints: TestPoint[]) => {
switch(testCase.parent.project()?.use.browserName) {
Expand All @@ -160,11 +165,19 @@ Reporter options (\* - required):
}
}
```

- `publishTestResultsMode` - Mode of publishing test results. Default: `'testResult'`. Available options:
- `testResult` - Published results of tests, at the end of each test, parallel to test run..
- `testRun` - Published test results to test run, at the end of test run.
> **Note:** If you use `testRun` mode and using same test cases in different tests (yes i know it sounds funny), it will be overwritten with last test result.
- `isExistingTestRun` [true/false] - Published test results to the existing test run. In this mode test results only added to the existing test run without its creation and completion. Default: `false`.
> **Note:** If you use `isExistingTestRun` mode, `testRunId` should be specified.
- `testRunId` [number] - Id of test run. Used only for `existingTestRun` publishing mode. Also can be set by `AZURE_PW_TEST_RUN_ID` environment variable. Default: `undefined`.

> **Note:** If you set existing test run ID from reporter options and from environment variable - reporter options will be used

> **Note:** If you use `isExistingTestRun` mode, test run doesn't complete automatically. You should complete it manually.

## 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.
- **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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alex_neo/playwright-azure-reporter",
"version": "1.7.0",
"version": "1.8.0",
"description": "Playwright Azure Reporter",
"main": "./dist/playwright-azure-reporter.js",
"types": "./dist/playwright-azure-reporter.d.js",
Expand Down
32 changes: 30 additions & 2 deletions src/playwright-azure-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface AzureReporterOptions {
attachmentsType?: TAttachmentType | undefined;
testRunConfig?: TTestRunConfig;
testPointMapper?: (testCase: TestCase, testPoints: TestPoint[]) => Promise<TestPoint[] | undefined>;
isExistingTestRun?: boolean;
testRunId?: number;
}

interface TestResultsToTestRun {
Expand Down Expand Up @@ -137,6 +139,8 @@ class AzureDevOpsReporter implements Reporter {
maxRetries: 20,
} as IRequestOptions;
private _publishTestResultsMode: TPublishTestResults = 'testResult';
private _testRunId: number | undefined;
private _isExistingTestRun = false;

public constructor(options: AzureReporterOptions) {
this._runIdPromise = new Promise<number | void>((resolve, reject) => {
Expand Down Expand Up @@ -199,6 +203,17 @@ class AzureDevOpsReporter implements Reporter {
this._isDisabled = true;
return;
}
this._testRunId = options.testRunId || Number(process.env.AZURE_PW_TEST_RUN_ID) || undefined;
if (options?.isExistingTestRun && !this._testRunId) {
this._warning(
"'testRunId' or AZURE_PW_TEST_RUN_ID is not set for 'isExistingTestRun'=true mode. Reporting is disabled."
);
this._isDisabled = true;
return;
}
if (this._testRunId) {
process.env.AZURE_PW_TEST_RUN_ID = String(this._testRunId);
}
if (options?.uploadAttachments) {
if (!options?.attachmentsType) {
this._warning("'attachmentsType' is not set. Attachments Type will be set to 'screenshot' by default.");
Expand Down Expand Up @@ -234,13 +249,21 @@ class AzureDevOpsReporter implements Reporter {
if (options.testPointMapper) {
this._testPointMapper = options.testPointMapper;
}
this._isExistingTestRun = options.isExistingTestRun || false;

if (this._logging) {
debug.enable('azure');
}
}

async onBegin(): Promise<void> {
if (this._isDisabled) return;
if (this._isExistingTestRun) {
this._resolveRunId(this._testRunId!);
this._log(chalk.green(`Using existing run ${this._testRunId} to publish test results`));
this._log(chalk.green(`AZURE_PW_TEST_RUN_ID: ${process.env.AZURE_PW_TEST_RUN_ID}`));
return;
}
try {
this._testApi = await this._connection.getTestApi();

Expand Down Expand Up @@ -324,8 +347,12 @@ class AzureDevOpsReporter implements Reporter {
this._log(chalk.gray('No test results to publish'));
return;
} else {
const createRunResponse = await this._createRun(this._testRunTitle);
runId = createRunResponse?.id;
if (!this._isExistingTestRun) {
const createRunResponse = await this._createRun(this._testRunTitle);
runId = createRunResponse?.id;
} else {
runId = this._testRunId;
}
if (runId) {
this._resolveRunId(runId);
this._log(chalk.green(`Using run ${runId} to publish test results`));
Expand All @@ -346,6 +373,7 @@ class AzureDevOpsReporter implements Reporter {
return;
}

if (this._isExistingTestRun) return;
if (!this._testApi) this._testApi = await this._connection.getTestApi();
const runUpdatedResponse = await this._testApi.updateTestRun({ state: 'Completed' }, this._projectName, runId!);
this._log(chalk.green(`Run ${runId} - ${runUpdatedResponse.state}`));
Expand Down
Loading