Skip to content

Commit

Permalink
EPMRPP-78272 || Move new reportingApi to promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzmitry Kosarau authored and Dzmitry Kosarau committed Oct 14, 2023
1 parent 9ec793d commit f1b584f
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 161 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore all md files
*.md
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Added
- Add ability to provide `attributes`,`status`,`description`,`testCaseId` for test using `testInfo.attach`
- `ReportingApi` from `@reportportal/agent-js-playwright/promises` methods (***addAttributes, setDescription, setTestCaseId, setStatus***, and all methods for setting custom statuses for test or suite) now using ***testInfo.attach*** method to attach custom data to test case.

## [5.1.4] - 2023-10-05
## Changed
Expand Down
137 changes: 73 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,69 +102,6 @@ test('basic test', async ({ page }, testInfo) => {
});
```

Also, we can attach `attributes`,`status`,`description`,`testCaseId`

`Description`
```typescript
import { test, expect } from '@playwright/test';
import { RPTestInfo } from '@reportportal/agent-js-playwright'

test('basic test', async ({ page }, testInfo) => {
await testInfo.attach(RPTestInfo.description, { body: 'Description', contentType: 'plain/text' });

expect(true).toBe(true);
});
```
Description provided this way will be merged with description provided by `ReportingAPI`.

`Attributes`
```typescript
import { test, expect } from '@playwright/test';
import { RPTestInfo } from '@reportportal/agent-js-playwright'

test('basic test', async ({ page }, testInfo) => {
await testInfo.attach(RPTestInfo.status, { body: JSON.stringify([
{
key: 'testKey',
value: 'testValue',
},
{
value: 'testValueTwo',
}
]),
contentType: 'application/json' });

expect(true).toBe(true);
});
```
Attributes provided this way will be merged with Attributes provided by `ReportingAPI`.

`Status`
```typescript
import { test, expect } from '@playwright/test';
import { RPTestInfo } from '@reportportal/agent-js-playwright'

test('basic test', async ({ page }, testInfo) => {
await testInfo.attach(RPTestInfo.status, { body: 'passed', contentType:'text/plain'})

expect(true).toBe(true);
});
```
Status provided this way will be replaced by status provided by `ReportingAPI`. You can provide as many statuses as you want, but only the last will be applied.

`testCaseId`
```typescript
import { test, expect } from '@playwright/test';
import { RPTestInfo } from '@reportportal/agent-js-playwright'

test('basic test', async ({ page }, testInfo) => {
await testInfo.attach(RPTestInfo.testCaseId, { body: 'testCaseId', contentType:'text/plain'})

expect(true).toBe(true);
});
```
TestCaseId provided this way will be replaced by testCaseId provided by `ReportingAPI`.

*Note:* attachment path can be provided instead of body.

As an alternative to this approach the [`ReportingAPI`](#log) methods can be used.
Expand Down Expand Up @@ -193,9 +130,11 @@ As an alternative to this approach the [`ReportingAPI`](#log) methods can be use

This reporter provides Reporting API to use it directly in tests to send some additional data to the report.

To start using the `ReportingApi` in tests, just import it from `'@reportportal/agent-js-playwright'`:
To start using the `ReportingApi` in tests, just import it from `'@reportportal/agent-js-playwright'` or `'@reportportal/agent-js-playwright/promises'`:
```javascript
import { ReportingApi } from '@reportportal/agent-js-playwright';
// or
import { ReportingApi } from '@reportportal/agent-js-playwright/promises'
```

#### Reporting API methods
Expand Down Expand Up @@ -225,6 +164,27 @@ test('should have the correct attributes', () => {
});
```

`ReportingApi.addAttributes(attributes: Array<Attribute>, suite?: string):Promise<void>;`<br/>
**required**: `attributes`<br/>
**optional**: `suite`<br/>
Example:
```javascript
import { ReportingApi } from '@reportportal/agent-js-playwright/promises'

test('should have the correct attributes', async () => {
await ReportingApi.addAttributes([
{
key: 'testKey',
value: 'testValue',
},
{
value: 'testValueTwo',
},
]);
expect(true).toBe(true);
});
```

##### setTestCaseId
Set test case id to the current test ([About test case id](https://reportportal.io/docs/Test-case-ID%3Ewhat-is-it-test-case-id)). Should be called inside of corresponding test.<br/>
`ReportingApi.setTestCaseId(id: string, suite?: string);`<br/>
Expand All @@ -239,6 +199,19 @@ test('should have the correct testCaseId', () => {
});
```

`ReportingApi.setTestCaseId(id: string, suite?: string):Promise<void>;`<br/>
**required**: `id`<br/>
**optional**: `suite`<br/>
Example:
```javascript
import { ReportingApi } from '@reportportal/agent-js-playwright/promises'

test('should have the correct testCaseId', async () => {
await ReportingApi.setTestCaseId('itemTestCaseId');
expect(true).toBe(true);
});
```

##### log
Send logs to report portal for the current test. Should be called inside of corresponding test.<br/>
`ReportingApi.log(level: LOG_LEVELS, message: string, file?: Attachment, suite?: string);`<br/>
Expand Down Expand Up @@ -346,6 +319,19 @@ test('should have status FAILED', () => {
});
```

`ReportingApi.setStatus(status: string, suite?: string):Promise<void>;`<br/>
**required**: `status`<br/>
**optional**: `suite`<br/>
```javascript
import { ReportingApi } from '@reportportal/agent-js-playwright/promises'

test('should have status FAILED', async () => {
await ReportingApi.setStatus('failed');

expect(true).toBe(true);
});
```

##### setStatusFailed, setStatusPassed, setStatusSkipped, setStatusStopped, setStatusInterrupted, setStatusCancelled
Assign corresponding status to the current test item. Should be called inside of corresponding test.<br/>
`ReportingApi.setStatusFailed(suite?: string);`<br/>
Expand All @@ -365,6 +351,29 @@ test('should call ReportingApi to set statuses', () => {
ReportingAPI.setStatusInterrupted();
ReportingAPI.setStatusCancelled();
});

```
`ReportingApi.setStatusFailed(suite?: string):Promise<void>;`<br/>
`ReportingApi.setStatusPassed(suite?: string):Promise<void>;`<br/>
`ReportingApi.setStatusSkipped(suite?: string):Promise<void>;`<br/>
`ReportingApi.setStatusStopped(suite?: string):Promise<void>;`<br/>
`ReportingApi.setStatusInterrupted(suite?: string):Promise<void>;`<br/>
`ReportingApi.setStatusCancelled(suite?: string):Promise<void>;`<br/>
**optional**: `suite`<br/>
Example:
```javascript
import { ReportingApi } from '@reportportal/agent-js-playwright/promises'

test('should call ReportingApi to set statuses', async () => {
await Promise.all[
ReportingAPI.setStatusFailed();
ReportingAPI.setStatusPassed(),
ReportingAPI.setStatusSkipped(),
ReportingAPI.setStatusStopped(),
ReportingAPI.setStatusInterrupted(),
ReportingAPI.setStatusCancelled()
]
});
```

##### setLaunchStatus
Expand Down
20 changes: 18 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@
"name": "@reportportal/agent-js-playwright",
"version": "5.1.4",
"description": "Agent to integrate Playwright with ReportPortal.",
"main": "build/index.js",
"types": "build/index.d.ts",
"exports": {
".": {
"node": "./build/index.js",
"import": "./build/index.js",
"require": "./build/index.js",
"default": "./build/index.js",
"types": "./build/index.d.ts"
},
"./promises": {
"node": "./build/promises/index.js",
"default": "./build/promises/index.js",
"require": "./build/promises/index.js",
"import": "./build/promises/index.js",
"types": "./build/promises/index.d.ts"
}
},
"types": "./build/index.d.ts",
"main": "./build/index.js",
"scripts": {
"build": "npm run clean && tsc",
"clean": "rimraf ./build",
Expand Down
75 changes: 51 additions & 24 deletions src/__tests__/reporter/finishTestItemReporting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,30 @@ describe('finish test reporting', () => {
description: 'description',
};

// @ts-ignore
await reporter.onTestEnd({ ...testCase, outcome: () => 'expected' }, result);
await reporter.onTestEnd(
{
...testCase,
outcome: () => 'expected',
results: [
// @ts-ignore
{
attachments: [
{
name: RPTestInfo.attributes,
body: Buffer.from(JSON.stringify([{ key: 'key', value: 'value' }])),
contentType: 'application/json',
},
{
name: RPTestInfo.description,
body: Buffer.from('description'),
contentType: 'text/plain',
},
],
},
],
},
result,
);

expect(reporter.client.finishTestItem).toHaveBeenCalledTimes(3);
expect(reporter.client.finishTestItem).toHaveBeenNthCalledWith(
Expand All @@ -126,7 +148,30 @@ describe('finish test reporting', () => {
issue: { issueType: 'NOT_ISSUE' },
};
// @ts-ignore
await reporter.onTestEnd({ ...testCase, outcome: () => 'skipped' }, result);
await reporter.onTestEnd(
{
...testCase,
outcome: () => 'skipped',
results: [
// @ts-ignore
{
attachments: [
{
name: RPTestInfo.attributes,
body: Buffer.from(JSON.stringify([{ key: 'key', value: 'value' }])),
contentType: 'application/json',
},
{
name: RPTestInfo.description,
body: Buffer.from('description'),
contentType: 'text/plain',
},
],
},
],
},
result,
);

expect(reporter.client.finishTestItem).toHaveBeenCalledTimes(3);
expect(reporter.client.finishTestItem).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -195,22 +240,6 @@ describe('finish test reporting', () => {
test('client.finishTestItem should call reporter.client.finishTestItem with correct values', async () => {
const result = { status: 'passed' };

reporter.testItems = new Map([
[
`${testCase.id}`,
{
id: 'testItemId',
name: 'name',
description: 'savedTestItemDescription',
status: STATUSES.PASSED,
attributes: [
{ value: 'savedTestItemAttrValue' },
{ key: 'savedTestItemAttrKey', value: 'savedTestItemAttrValue', system: false },
],
},
],
]);

await reporter.onTestEnd(
{
...testCase,
Expand Down Expand Up @@ -263,20 +292,18 @@ describe('finish test reporting', () => {

const finishStepObject: FinishTestItemObjType = {
endTime: reporter.client.helpers.now(),
status: STATUSES.PASSED,
status: STATUSES.INTERRUPTED,
attributes: [
{ key: 'key1', value: 'value1', system: false },
{ key: 'key2', value: 'value2', system: false },
{ value: 'savedTestItemAttrValue' },
{ key: 'savedTestItemAttrKey', value: 'savedTestItemAttrValue', system: false },
],
description: 'savedTestItemDescription\nDescription',
description: 'Description',
testCaseId: 'testCaseId',
};

expect(reporter.client.finishTestItem).toHaveBeenNthCalledWith(
1,
'testItemId',
'tempTestItemId',
finishStepObject,
);
});
Expand Down
25 changes: 0 additions & 25 deletions src/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
isErrorLog,
calculateRpStatus,
getAdditionalInfo,
getDescription,
} from '../utils';
import fs from 'fs';
import path from 'path';
Expand Down Expand Up @@ -531,28 +530,4 @@ describe('testing utils', () => {
expect(additionalInfo).toEqual(expectedResult);
});
});

describe('getDescription', () => {
test('It should return one description joined by \n', () => {
const description1 = 'description1';
const description2 = 'description2';

const expectedResult = 'description1\ndescription2';

const description = getDescription(description1, description2);

expect(expectedResult).toBe(description);
});

test('It should return one description joined by \n in case when empty string was provided', () => {
const description1 = 'description1';
const description2 = '';

const expectedResult = 'description1';

const description = getDescription(description1, description2);

expect(expectedResult).toBe(description);
});
});
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RPReporter } from './reporter';
export { ReportingApi } from './reportingApi';
export { LOG_LEVELS, STATUSES } from './constants';
export { RPTestInfo } from './constants';
export { ReportPortalConfig } from './models';

export default RPReporter;
1 change: 1 addition & 0 deletions src/promises/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ReportingApi } from './reportingApi';
Loading

0 comments on commit f1b584f

Please sign in to comment.