From fb216019b296b1b691ecf484573dd7d128cb61c7 Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Sun, 11 Feb 2024 09:24:30 +0100 Subject: [PATCH] Add stopAsFailure (#2076) --- lib/core/engine/command/measure.js | 19 +++++++++++++++++-- types/core/engine/command/measure.d.ts | 13 ++++++++++++- types/core/engine/command/measure.d.ts.map | 2 +- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/core/engine/command/measure.js b/lib/core/engine/command/measure.js index 08d0b640f..ca9ed5f33 100644 --- a/lib/core/engine/command/measure.js +++ b/lib/core/engine/command/measure.js @@ -372,6 +372,21 @@ export class Measure { } } + /** + * Stop the current measurement and mark it as a failure. This stop function will not measure anything on a page. This is useful if you need to stop a measurement in a (try) catch and you + * know something has failed. + * + * @async + * @param {string} failureMessage - The message about the failure. This will end up on the HTML report for sitespeed.io so give it a good message so you know what's gone wrong. + * @param {string} optionalURL - The URL of the page that you wanted to test. If you don't know the URL (you clicked on a link etc) skip it. + * @returns {Promise} A promise that resolves when the stop process has completed. + * @since 21.2.0 + */ + async stopAsFailure(failureMessage, optionalURL) { + this.areWeMeasuring = false; + this._failure(failureMessage); + return this.stop(optionalURL, false); + } /** * Stops the measurement process, collects metrics, and handles any post-measurement tasks. * It finalizes the URL being tested, manages any URL-specific metadata, stops any ongoing video recordings, @@ -382,7 +397,7 @@ export class Measure { * @throws {Error} Throws an error if there are issues in stopping the measurement or collecting data. * @returns {Promise} A promise that resolves with the collected metrics data. */ - async stop(testedStartUrl) { + async stop(testedStartUrl, collectMeasurements = true) { log.debug('Stop measuring'); // If we don't have a URL (tested using clicking on link etc) get the URL from the browser let url = @@ -448,7 +463,7 @@ export class Measure { await this.tcpDump.stop(); } - return this.collect(url); + return collectMeasurements === false ? undefined : this.collect(url); } /** diff --git a/types/core/engine/command/measure.d.ts b/types/core/engine/command/measure.d.ts index 347faba97..66a4043d9 100644 --- a/types/core/engine/command/measure.d.ts +++ b/types/core/engine/command/measure.d.ts @@ -155,6 +155,17 @@ export class Measure { * @returns {Promise} A promise that resolves when the start process is complete, or rejects if there are errors. */ start(urlOrAlias: string, optionalAlias?: string): Promise; + /** + * Stop the current measurement and mark it as a failure. This stop function will not measure anything on a page. This is useful if you need to stop a measurement in a (try) catch and you + * know something has failed. + * + * @async + * @param {string} failureMessage - The message about the failure. This will end up on the HTML report for sitespeed.io so give it a good message so you know what's gone wrong. + * @param {string} optionalURL - The URL of the page that you wanted to test. If you don't know the URL (you clicked on a link etc) skip it. + * @returns {Promise} A promise that resolves when the stop process has completed. + * @since 21.2.0 + */ + stopAsFailure(failureMessage: string, optionalURL: string): Promise; /** * Stops the measurement process, collects metrics, and handles any post-measurement tasks. * It finalizes the URL being tested, manages any URL-specific metadata, stops any ongoing video recordings, @@ -165,7 +176,7 @@ export class Measure { * @throws {Error} Throws an error if there are issues in stopping the measurement or collecting data. * @returns {Promise} A promise that resolves with the collected metrics data. */ - stop(testedStartUrl: string): Promise; + stop(testedStartUrl: string, collectMeasurements?: boolean): Promise; /** * Adds a custom metric to the current measurement result. * This method should be called after a measurement has started and before it has stopped. diff --git a/types/core/engine/command/measure.d.ts.map b/types/core/engine/command/measure.d.ts.map index 368ab4136..203c7b430 100644 --- a/types/core/engine/command/measure.d.ts.map +++ b/types/core/engine/command/measure.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"measure.d.ts","sourceRoot":"","sources":["../../../../lib/core/engine/command/measure.js"],"names":[],"mappings":"AA6BA;;;;;;GAMG;AACH;IACE,sRA4GC;IA5FC;;OAEG;IACH,gBAAsB;IACtB;;OAEG;IACH,0BAA0C;IAC1C;;OAEG;IACH,cAAkB;IAClB;;OAEG;IACH,eAAoB;IACpB;;OAEG;IACH,uBAAoC;IACpC;;OAEG;IACH,gBAAsB;IACtB;;OAEG;IACH,0BAA0C;IAC1C;;OAEG;IACH,uBAAoC;IACpC;;OAEG;IACH,oBAAyD;IACzD;;OAEG;IACH,wBAAsC;IACtC;;OAEG;IACH,eAAoB;IACpB;;OAEG;IACH,0BAA0C;IAC1C;;OAEG;IACH,+BAAoD;IACpD;;OAEG;IACH,uBAAoC;IACpC;;OAEG;IACH,gBAAsB;IACtB;;OAEG;IACH,8BAA8B;IAC9B;;OAEG;IACH,6BAA6B;IAC7B;;OAEG;IACH,uBAA2B;IAC3B;;OAEG;IACH,mBAAoB;IACpB;;OAEG;IACH,gBAA6D;IAC7D;;OAEG;IACH,2BAAsE;IACtE;;OAEG;IACH,uBAA8D;IAC9D;;OAEG;IACH,2BAAqE;IAGvE;;;OAGG;IACH,oBAsBC;IArBC,aAAuE;IAuBzE;;;OAGG;IACH,mBAKC;IAED;;;OAGG;IACH,eAsBC;IAED;;;OAGG;IACH,iBAQC;IAED;;;;;;;;;;;;OAYG;IACH,kBAuBC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,kBALW,MAAM,kBACN,MAAM,GAEJ,QAAQ,IAAI,CAAC,CAwFzB;IAED;;;;;;;;;OASG;IACH,qBAJW,MAAM,gBAkEhB;IAED;;;;;;;OAOG;IACH,UAJW,MAAM,oBAYhB;IAED;;;;;;;OAOG;IACH,6BAOC;IAED;;;OAGG;IACH,gBA+KC;CACF;sBAlpBqB,yBAAyB"} \ No newline at end of file +{"version":3,"file":"measure.d.ts","sourceRoot":"","sources":["../../../../lib/core/engine/command/measure.js"],"names":[],"mappings":"AA6BA;;;;;;GAMG;AACH;IACE,sRA4GC;IA5FC;;OAEG;IACH,gBAAsB;IACtB;;OAEG;IACH,0BAA0C;IAC1C;;OAEG;IACH,cAAkB;IAClB;;OAEG;IACH,eAAoB;IACpB;;OAEG;IACH,uBAAoC;IACpC;;OAEG;IACH,gBAAsB;IACtB;;OAEG;IACH,0BAA0C;IAC1C;;OAEG;IACH,uBAAoC;IACpC;;OAEG;IACH,oBAAyD;IACzD;;OAEG;IACH,wBAAsC;IACtC;;OAEG;IACH,eAAoB;IACpB;;OAEG;IACH,0BAA0C;IAC1C;;OAEG;IACH,+BAAoD;IACpD;;OAEG;IACH,uBAAoC;IACpC;;OAEG;IACH,gBAAsB;IACtB;;OAEG;IACH,8BAA8B;IAC9B;;OAEG;IACH,6BAA6B;IAC7B;;OAEG;IACH,uBAA2B;IAC3B;;OAEG;IACH,mBAAoB;IACpB;;OAEG;IACH,gBAA6D;IAC7D;;OAEG;IACH,2BAAsE;IACtE;;OAEG;IACH,uBAA8D;IAC9D;;OAEG;IACH,2BAAqE;IAGvE;;;OAGG;IACH,oBAsBC;IArBC,aAAuE;IAuBzE;;;OAGG;IACH,mBAKC;IAED;;;OAGG;IACH,eAsBC;IAED;;;OAGG;IACH,iBAQC;IAED;;;;;;;;;;;;OAYG;IACH,kBAuBC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,kBALW,MAAM,kBACN,MAAM,GAEJ,QAAQ,IAAI,CAAC,CAwFzB;IAED;;;;;;;;;OASG;IACH,8BALW,MAAM,eACL,MAAM,gBAQjB;IACD;;;;;;;;;OASG;IACH,qBAJW,MAAM,+CAuEhB;IAED;;;;;;;OAOG;IACH,UAJW,MAAM,oBAYhB;IAED;;;;;;;OAOG;IACH,6BAOC;IAED;;;OAGG;IACH,gBA4KC;CACF;sBAnqBqB,yBAAyB"} \ No newline at end of file