From c74476267d303b8369de18de6a970d3396eeb72b Mon Sep 17 00:00:00 2001 From: Ilya Hancharyk Date: Fri, 2 Feb 2024 19:01:52 +0300 Subject: [PATCH] Add existing launchId support. Fix default values for launch mode and log level --- README.md | 50 +++++++++++++-------- src/__tests__/constructor.spec.ts | 20 ++++++--- src/__tests__/utils.spec.ts | 72 +++++++++++++++++++++---------- src/constants/index.ts | 1 + src/constants/launchModes.ts | 21 +++++++++ src/models/interfaces.ts | 13 ++++-- src/reporter.ts | 5 ++- src/utils.ts | 12 ++++-- 8 files changed, 140 insertions(+), 54 deletions(-) create mode 100644 src/constants/launchModes.ts diff --git a/README.md b/README.md index c39ffeb..a5bffdb 100644 --- a/README.md +++ b/README.md @@ -42,22 +42,38 @@ exports.config = { // ... }; ``` -| Parameter | Description | -|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| token | User's Report Portal token from which you want to send requests. It can be found on the profile page of this user. | -| endpoint | URL of your server. For example 'https://server:8080/api/v1'. | -| launch | Name of launch at creation. | -| project | The name of the project in which the launches will be created. | -| rerun | *Default: false.* Enable [rerun](https://github.com/reportportal/documentation/blob/master/src/md/src/DevGuides/rerun.md) | -| rerunOf | UUID of launch you want to rerun. If not specified, report portal will update the latest launch with the same name | -| mode | Launch mode. Allowable values *DEFAULT* (by default) or *DEBUG*. | -| attachPicturesToLogs | Automatically add screenshots | -| debug | This flag allows seeing the logs of the client-javascript. Useful for debugging. | -| cucumberNestedSteps | [Report your steps as logs](https://github.com/reportportal/agent-js-webdriverio#step-reporting-configuration) | -| skippedIssue | *Default: true.* ReportPortal provides feature to mark skipped tests as not 'To Investigate' items on WS side.
Parameter could be equal boolean values:
*TRUE* - skipped tests considered as issues and will be marked as 'To Investigate' on Report Portal.
*FALSE* - skipped tests will not be marked as 'To Investigate' on application. | -| isLaunchMergeRequired | *Default: false.* Allows to merge several run's into one launch at the end of the run. Needs additional setup. See [Manual merge launches](#manual-merge-launches). | -| reportSeleniumCommands | *Default: false.* Add selenium logs to each test case. | -| seleniumCommandsLogLevel | If set *reportSeleniumCommands* to *true*, you need to provide log level witch can be one of: *'trace', 'debug', 'info', 'warn', 'error', 'fatal'*. | + +The full list of available options presented below. + +| Option | Necessity | Default | Description | +|--------------------------|------------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| apiKey | Required | | User's ReportPortal token from which you want to send requests. It can be found on the profile page of this user. | +| endpoint | Required | | URL of your server. For example 'https://server:8080/api/v1'. | +| launch | Required | | Name of launch at creation. | +| project | Required | | The name of the project in which the launches will be created. | +| attributes | Optional | [] | Launch attributes. | +| description | Optional | '' | Launch description. | +| rerun | Optional | false | Enable [rerun](https://reportportal.io/docs/dev-guides/RerunDevelopersGuide) | +| rerunOf | Optional | Not set | UUID of launch you want to rerun. If not specified, ReportPortal will update the latest launch with the same name | +| mode | Optional | 'DEFAULT' | Results will be submitted to Launches page
*'DEBUG'* - Results will be submitted to Debug page. | +| skippedIssue | Optional | true | ReportPortal provides feature to mark skipped tests as not 'To Investigate'.
Option could be equal boolean values:
*true* - skipped tests considered as issues and will be marked as 'To Investigate' on ReportPortal.
*false* - skipped tests will not be marked as 'To Investigate' on application. | +| debug | Optional | false | This flag allows seeing the logs of the client-javascript. Useful for debugging. | +| launchId | Optional | Not set | The _ID_ of an already existing launch. The launch must be in 'IN_PROGRESS' status while the tests are running. Please note that if this _ID_ is provided, the launch will not be finished at the end of the run and must be finished separately. | +| restClientConfig | Optional | Not set | The object with `agent` property for configure [http(s)](https://nodejs.org/api/https.html#https_https_request_url_options_callback) client, may contain other client options eg. [`timeout`](https://github.com/reportportal/client-javascript#timeout-30000ms-on-axios-requests).
Visit [client-javascript](https://github.com/reportportal/client-javascript) for more details. | +| launchUuidPrint | Optional | false | Whether to print the current launch UUID. | +| launchUuidPrintOutput | Optional | 'STDOUT' | Launch UUID printing output. Possible values: 'STDOUT', 'STDERR'. Works only if `launchUuidPrint` set to `true`. | +| isLaunchMergeRequired | Optional | false | Allows to merge several run's into one launch at the end of the run. Needs additional setup. See [Manual merge launches](#manual-merge-launches). | +| attachPicturesToLogs | Optional | false | Automatically add screenshots | +| cucumberNestedSteps | Optional | false | [Report your steps as logs](https://github.com/reportportal/agent-js-webdriverio#step-reporting-configuration) | +| reportSeleniumCommands | Optional | false | Add selenium logs to each test case. | +| seleniumCommandsLogLevel | Optional | 'info' | If set *reportSeleniumCommands* to *true*, you need to provide log level witch can be one of: *'trace', 'debug', 'info', 'warn', 'error', 'fatal'*. | +| token | Deprecated | Not set | Use `apiKey` instead. | + +The following options can be overridden using ENVIRONMENT variables: + +| Option | ENV variable | +|-------------|-----------------| +| launchId | RP_LAUNCH_ID | ## Step reporting configuration (for Cucumber setup) @@ -71,7 +87,7 @@ You may change this behavior to report steps to the log level by enabling scenar - feature - TEST - scenario - STEP -- step - log item +- step - log item (nested step) To report your steps as logs, you need to pass an additional parameter to the agent config: `"cucumberNestedSteps": true` diff --git a/src/__tests__/constructor.spec.ts b/src/__tests__/constructor.spec.ts index 1323c9a..775141b 100644 --- a/src/__tests__/constructor.spec.ts +++ b/src/__tests__/constructor.spec.ts @@ -19,16 +19,24 @@ import { Reporter } from '../reporter'; import { options } from './mocks/optionsMock'; describe('reporter constructor', () => { - let reporter: Reporter; - beforeEach(() => { - reporter = new Reporter(options); - }); + const optionsWithDefaults = { + seleniumCommandsLogLevel: 'info', + ...options, + }; - it('should store configuration data', () => { - expect(reporter.options).toEqual(options); + it('should store configuration data with default values', () => { + const reporter = new Reporter(options); + expect(reporter.options).toEqual(optionsWithDefaults); }); it('isSynchronised should be FALSE', () => { + const reporter = new Reporter(options); expect(reporter.isSynchronised).toBeFalsy(); }); + + it('should override options defaults via user provided options', () => { + const reporterOptions = { ...options, seleniumCommandsLogLevel: 'debug' }; + const reporter = new Reporter(reporterOptions); + expect(reporter.options).toEqual(reporterOptions); + }); }); diff --git a/src/__tests__/utils.spec.ts b/src/__tests__/utils.spec.ts index 670fd11..304033b 100644 --- a/src/__tests__/utils.spec.ts +++ b/src/__tests__/utils.spec.ts @@ -28,6 +28,8 @@ import { parseTags, promiseErrorHandler, } from '../utils'; +import { LAUNCH_MODES } from '../constants'; +import { LaunchObj } from '../models'; import { options } from './mocks/optionsMock'; describe('utils', () => { @@ -41,15 +43,7 @@ describe('utils', () => { }); describe('getClientConfig', () => { - const { apiKey, endpoint, launch, project, attributes, description } = options; - const baseRes = { - apiKey, - endpoint, - launch, - project, - attributes, - description, - }; + const { logFile, ...baseRes } = options; it('should return base config', () => { expect(getClientConfig(options)).toEqual(baseRes); @@ -133,27 +127,59 @@ describe('utils', () => { }); describe('getStartLaunchObj', () => { + const { attributes: optionsAttributes, description, rerun, rerunOf } = options; + const startLaunchObject: LaunchObj = { + attributes: optionsAttributes, + description, + rerun, + rerunOf, + mode: LAUNCH_MODES.DEFAULT, + id: undefined, + }; const systemAttributes = getSystemAttributes(options); + const fullAttributes = options.attributes.concat(systemAttributes); - it('config with attributes', () => { - const { description, attributes, rerun, rerunOf, mode } = options; - const expectedRes = { - attributes: [...attributes, ...systemAttributes], - description, - rerun, - rerunOf, - mode, + it('should return start launch object with system attributes joined with provided', () => { + const expectedObject = { + ...startLaunchObject, + attributes: fullAttributes, + }; + + expect(getStartLaunchObj(options)).toEqual(expectedObject); + }); + + it('should return start launch object only with system attributes in case of no attributes provided', () => { + const { attributes, ...optionsWithoutAttributes } = options; + const expectedObject = { + ...startLaunchObject, + attributes: systemAttributes, }; - expect(getStartLaunchObj(options)).toEqual(expectedRes); + expect(getStartLaunchObj(optionsWithoutAttributes)).toEqual(expectedObject); }); - it('config without attributes', () => { - const newOptions = Object.assign(options, { attributes: undefined }); - const { description, rerun, rerunOf, mode } = newOptions; - const expectedRes = { attributes: systemAttributes, description, rerun, rerunOf, mode }; + it('should set launch id from options launchId property', () => { + const launchId = 'realLaunchId'; + const optionsWithLaunchId = { ...options, launchId: launchId }; + const expectedObject = { + ...startLaunchObject, + attributes: fullAttributes, + id: launchId, + }; + + expect(getStartLaunchObj(optionsWithLaunchId)).toEqual(expectedObject); + }); + + it('should set launch id from environment variable if exists', () => { + process.env.RP_LAUNCH_ID = 'realLaunchId'; + const expectedObject = { + ...startLaunchObject, + attributes: fullAttributes, + id: 'realLaunchId', + }; - expect(getStartLaunchObj(options)).toEqual(expectedRes); + expect(getStartLaunchObj(options)).toEqual(expectedObject); + delete process.env.RP_LAUNCH_ID; }); }); diff --git a/src/constants/index.ts b/src/constants/index.ts index 5e6a7d9..baa254c 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -19,4 +19,5 @@ export { CUCUMBER_TYPE, TYPES } from './testItemTypes'; export { RP_STATUSES } from './statuses'; export { LOG_LEVELS } from './logLevels'; export { FILE_TYPES } from './fileTypes'; +export { LAUNCH_MODES } from './launchModes'; export { BROWSER_PARAM } from './parameters'; diff --git a/src/constants/launchModes.ts b/src/constants/launchModes.ts new file mode 100644 index 0000000..10c21dc --- /dev/null +++ b/src/constants/launchModes.ts @@ -0,0 +1,21 @@ +/* + * Copyright 2024 EPAM Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +export enum LAUNCH_MODES { + DEFAULT = 'DEFAULT', + DEBUG = 'DEBUG', +} diff --git a/src/models/interfaces.ts b/src/models/interfaces.ts index 71baf05..fd795fc 100644 --- a/src/models/interfaces.ts +++ b/src/models/interfaces.ts @@ -1,5 +1,5 @@ /* - * Copyright 2021 EPAM Systems + * Copyright 2024 EPAM Systems * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,9 @@ * */ -import { FILE_TYPES, LOG_LEVELS, TYPES } from '../constants'; +import { FILE_TYPES, LOG_LEVELS, TYPES, LAUNCH_MODES } from '../constants'; + +type launchMode = LAUNCH_MODES.DEFAULT | LAUNCH_MODES.DEBUG; export interface ClientConfig { apiKey: string; @@ -25,9 +27,11 @@ export interface ClientConfig { description?: string; attributes?: Attribute[]; headers?: BaseObj; - mode?: 'DEFAULT' | 'DEBUG'; + mode?: launchMode; debug?: boolean; isLaunchMergeRequired?: boolean; + launchUuidPrint?: boolean; + launchUuidPrintOutput?: string; token?: string; } @@ -37,6 +41,7 @@ export interface Config extends ClientConfig { rerunOf?: string; seleniumCommandsLogLevel?: LOG_LEVELS; reportSeleniumCommands?: boolean; + launchId?: string; } export interface LaunchObj { @@ -44,7 +49,7 @@ export interface LaunchObj { startTime?: Date | number; description?: string; attributes?: Attribute[]; - mode?: 'DEFAULT' | 'DEBUG'; + mode?: launchMode; rerun?: boolean; rerunOf?: string; id?: string; diff --git a/src/reporter.ts b/src/reporter.ts index c3a5720..96d092f 100644 --- a/src/reporter.ts +++ b/src/reporter.ts @@ -60,7 +60,10 @@ export class Reporter extends WDIOReporter { const agentInfo = getAgentInfo(); const clientConfig = getClientConfig(options); - this.options = options; + this.options = { + seleniumCommandsLogLevel: 'info', + ...options, + }; this.syncReporting = false; this.client = new RPClient(clientConfig, agentInfo); this.storage = new Storage(); diff --git a/src/utils.ts b/src/utils.ts index c68e1a2..51a6af7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -21,6 +21,7 @@ import { Reporters } from '@wdio/types'; import { Tag } from '@wdio/reporter/build/types'; // @ts-ignore import { name as pjsonName, version as pjsonVersion } from '../package.json'; +import { LAUNCH_MODES } from './constants/launchModes'; import { Attribute, ClientConfig, LaunchObj, Suite } from './models'; export const promiseErrorHandler = (promise: Promise): void => { @@ -44,6 +45,8 @@ export const getClientConfig = (options: Partial): ClientConf headers, restClientConfig, isLaunchMergeRequired, + launchUuidPrint, + launchUuidPrintOutput, } = options; let apiKey = options.apiKey; @@ -68,7 +71,9 @@ export const getClientConfig = (options: Partial): ClientConf ...(debug && { debug }), ...(headers && { headers }), ...(restClientConfig && { restClientConfig }), - ...(isLaunchMergeRequired && { isLaunchMergeRequired }), + launchUuidPrint, + launchUuidPrintOutput, + isLaunchMergeRequired, }; }; @@ -104,14 +109,15 @@ export const getStartLaunchObj = ( launchObj: LaunchObj = {}, ): LaunchObj => { const systemAttributes = getSystemAttributes(config); - const { description, attributes, rerun, rerunOf, mode } = config; + const { description, attributes, rerun, rerunOf, mode, launchId } = config; return { description, attributes: [...(attributes || []), ...systemAttributes], rerun, rerunOf, - mode, + mode: mode || LAUNCH_MODES.DEFAULT, + id: process.env.RP_LAUNCH_ID || launchId, ...launchObj, }; };