Skip to content

Commit

Permalink
feature: added support parameters on collection and folder levels
Browse files Browse the repository at this point in the history
  • Loading branch information
gibiw committed Oct 3, 2024
1 parent b26ef31 commit 97e73ec
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
6 changes: 6 additions & 0 deletions qase-newman/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# [email protected]

## What's new

Added support parameters from data files in Newman on collection and folder levels.

# [email protected]

## What's new
Expand Down
60 changes: 60 additions & 0 deletions qase-newman/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,66 @@ pm.test("Response has correct name", function() {
});
```

You also can specify parameters on collection or folder level. In this case, all tests in collection or folder will have
these parameters. If test has own parameters, they will be merged with collection or folder parameters.

```json
{
"info": {
"_postman_id": "collection_id",
"name": "Collection Name",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Folder Name",
"item": [
{
"name": "Test Name",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"pm.test('Status code is 200', function () {",
" pm.response.to.have.status(200);",
"})"
]
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://api.example.com",
"host": [
"api",
"example",
"com"
]
}
},
"response": []
}
],
"event": [
{
"listen": "test",
"script": {
"exec": [
"// qase.parameters: userId, user.name"
],
"type": "text/javascript"
}
}
]
}
]
}
```

### Expected Behavior

When you run the tests, the following behavior is expected:
Expand Down
2 changes: 1 addition & 1 deletion qase-newman/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "newman-reporter-qase",
"version": "2.0.2",
"version": "2.0.3",
"description": "Qase TMS Newman Reporter",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
28 changes: 16 additions & 12 deletions qase-newman/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EventEmitter } from 'events';
import { configSchema } from './configSchema';
import semver from 'semver';
import { NewmanRunExecution, NewmanRunOptions } from 'newman';
import { EventList, PropertyBase, PropertyBaseDefinition } from 'postman-collection';
import { EventList, Item, PropertyBase, PropertyBaseDefinition } from 'postman-collection';
import {
ConfigType,
QaseReporter,
Expand Down Expand Up @@ -57,27 +57,31 @@ export class NewmanQaseReporter {
}

/**
* @param {EventList} eventList
* @param {Item} item
* @returns {string[]}
* @private
*/
private static getParameters(eventList: EventList) {
private static getParameters(item: Item): string[] {
const params: string[] = [];

eventList.each((event) => {
item.events.each((event) => {
if (event.listen === 'test' && event.script.exec) {
event.script.exec.forEach((line) => {
const match = line.match(NewmanQaseReporter.qaseParamRegExp);

if (match) {
const parameters: string[] = match[1]?.split(/\s*,\s*/) ?? [];

params.push(...parameters);
}
});
}
});

const parent = item.parent();
if (parent && 'events' in parent) {
params.push(...NewmanQaseReporter.getParameters(parent as Item));
}

return params;
}

Expand Down Expand Up @@ -246,7 +250,7 @@ export class NewmanQaseReporter {
pendingResult.execution.duration = now - timer;
}

pendingResult.params = this.prepareParameters(item.events, exec.cursor.iteration);
pendingResult.params = this.prepareParameters(item, exec.cursor.iteration);

void this.reporter.addTestResult(pendingResult);
}
Expand Down Expand Up @@ -303,18 +307,18 @@ export class NewmanQaseReporter {
}

/**
* @param {EventList} events
* @param {Item} item
* @param {number} iteration
* @returns {Record<string, string>}
* @private
*/
private prepareParameters(events: EventList, iteration: number): Record<string, string> {
private prepareParameters(item: Item, iteration: number): Record<string, string> {
if (this.parameters.length === 0) {
return {};
}

const availableParameters = this.parameters[iteration] ?? {};
const params = NewmanQaseReporter.getParameters(events);
const params = NewmanQaseReporter.getParameters(item);

if (params.length === 0) {
if (this.autoCollectParams) {
Expand All @@ -325,9 +329,9 @@ export class NewmanQaseReporter {
}

return params.reduce<Record<string, string>>((filteredParams, param) => {
const value = availableParameters[param];
const value = availableParameters[param.toLowerCase()];
if (value) {
filteredParams[param] = value;
filteredParams[param.toLowerCase()] = value;
}
return filteredParams;
}, {});
Expand Down Expand Up @@ -368,7 +372,7 @@ export class NewmanQaseReporter {
if (this.isRecord(value)) {
Object.assign(record, this.convertToRecord(value, newKey));
} else {
record[newKey] = String(value);
record[newKey.toLowerCase()] = String(value);
}
}
}
Expand Down

0 comments on commit 97e73ec

Please sign in to comment.