From c50e77f7b6c8c0b16c856c28cf004cac9cca251d Mon Sep 17 00:00:00 2001 From: Dmitrii Gridnev Date: Thu, 3 Oct 2024 16:00:39 +0200 Subject: [PATCH] feature: added support parameters on collection and folder levels --- qase-newman/changelog.md | 6 ++++ qase-newman/docs/usage.md | 60 +++++++++++++++++++++++++++++++++++++ qase-newman/package.json | 2 +- qase-newman/src/reporter.ts | 28 +++++++++-------- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/qase-newman/changelog.md b/qase-newman/changelog.md index 75766c68..c31d5c2e 100644 --- a/qase-newman/changelog.md +++ b/qase-newman/changelog.md @@ -1,3 +1,9 @@ +# qase-newman@2.0.3 + +## What's new + +Added support parameters from data files in Newman on collection and folder levels. + # qase-newman@2.0.2 ## What's new diff --git a/qase-newman/docs/usage.md b/qase-newman/docs/usage.md index a5b85172..715eb02e 100644 --- a/qase-newman/docs/usage.md +++ b/qase-newman/docs/usage.md @@ -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: diff --git a/qase-newman/package.json b/qase-newman/package.json index b0e60e82..f0602e77 100644 --- a/qase-newman/package.json +++ b/qase-newman/package.json @@ -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", diff --git a/qase-newman/src/reporter.ts b/qase-newman/src/reporter.ts index 4bc44326..bb350cf6 100644 --- a/qase-newman/src/reporter.ts +++ b/qase-newman/src/reporter.ts @@ -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, @@ -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; } @@ -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); } @@ -303,18 +307,18 @@ export class NewmanQaseReporter { } /** - * @param {EventList} events + * @param {Item} item * @param {number} iteration * @returns {Record} * @private */ - private prepareParameters(events: EventList, iteration: number): Record { + private prepareParameters(item: Item, iteration: number): Record { 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) { @@ -325,9 +329,9 @@ export class NewmanQaseReporter { } return params.reduce>((filteredParams, param) => { - const value = availableParameters[param]; + const value = availableParameters[param.toLowerCase()]; if (value) { - filteredParams[param] = value; + filteredParams[param.toLowerCase()] = value; } return filteredParams; }, {}); @@ -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); } } }