From b5a8704da3cac61aaa93786936aaf02427783c6b Mon Sep 17 00:00:00 2001 From: soulgalore Date: Sun, 11 Feb 2024 11:21:07 +0100 Subject: [PATCH] docs: stopAsError --- .../sitespeed.io/scripting/Measure.html | 2 +- .../sitespeed.io/scripting/data/search.json | 2 +- .../scripting/tutorial-06-Error-handling.html | 15 +++++++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/documentation/sitespeed.io/scripting/Measure.html b/docs/documentation/sitespeed.io/scripting/Measure.html index 7d472b0d28..2ad305df34 100644 --- a/docs/documentation/sitespeed.io/scripting/Measure.html +++ b/docs/documentation/sitespeed.io/scripting/Measure.html @@ -5,4 +5,4 @@ await commands.measure.start(); await commands.click.byLinkTextAndWait('Documentation'); // Remember to stop the measurements if you do not provide a URL -await commands.measure.stop();

(async) stop(testedStartUrl) → {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, and initiates the data collection process.
Parameters:
NameTypeDescription
testedStartUrlstringThe URL that was initially tested. If not provided, it will be obtained from the browser.
Throws:
Throws an error if there are issues in stopping the measurement or collecting data.
Type
Error
Returns:
A promise that resolves with the collected metrics data.
Type: 
Promise
\ No newline at end of file +await commands.measure.stop();

(async) stop(testedStartUrl) → {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, and initiates the data collection process.
Parameters:
NameTypeDescription
testedStartUrlstringThe URL that was initially tested. If not provided, it will be obtained from the browser.
Throws:
Throws an error if there are issues in stopping the measurement or collecting data.
Type
Error
Returns:
A promise that resolves with the collected metrics data.
Type: 
Promise

(async) stopAsError(errorMessage, optionalURL) → {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.
Parameters:
NameTypeDescription
errorMessagestringThe message about the error. This will end up on the HTML report for sitespeed.io so give it a good message so you know what's gone wrong.
optionalURLstringThe URL of the page that you wanted to test. If you don't know the URL (you clicked on a link etc) skip it.
Since
  • 21.2.0
Returns:
A promise that resolves when the stop process has completed.
Type: 
Promise
\ No newline at end of file diff --git a/docs/documentation/sitespeed.io/scripting/data/search.json b/docs/documentation/sitespeed.io/scripting/data/search.json index c265d64dd5..d52303e83f 100644 --- a/docs/documentation/sitespeed.io/scripting/data/search.json +++ b/docs/documentation/sitespeed.io/scripting/data/search.json @@ -1 +1 @@ -{"list":[{"title":"Actions","link":"Actions"},{"title":"Actions#getActions","link":"getActions","description":"Retrieves the current action sequence builder.\nThe actions builder can be used to chain multiple browser actions."},{"title":"AddText","link":"AddText"},{"title":"AddText#byClassName","link":"byClassName","description":"Adds text to an element identified by its class name."},{"title":"AddText#byId","link":"byId","description":"Adds text to an element identified by its ID."},{"title":"AddText#byName","link":"byName","description":"Adds text to an element identified by its name attribute."},{"title":"AddText#bySelector","link":"bySelector","description":"Adds text to an element identified by its CSS selector."},{"title":"AddText#byXpath","link":"byXpath","description":"Adds text to an element identified by its XPath."},{"title":"AndroidCommand","link":"AndroidCommand"},{"title":"AndroidCommand#shell","link":"shell","description":"Runs a shell command on the connected Android device.\nThis method requires the Android device to be properly configured."},{"title":"AndroidCommand#shellAsRoot","link":"shellAsRoot","description":"Runs a shell command on the connected Android device as the root user.\nThis method requires the Android device to be properly configured and that you\nrooted the device."},{"title":"Cache","link":"Cache"},{"title":"Cache#clear","link":"clear","description":"Clears the browser cache. This includes both cache and cookies.\n\nFor Firefox, it uses the extensionServer setup with specific options.\nFor Chrome and Edge, it uses the Chrome DevTools Protocol (CDP) commands.\nIf the browser is not supported, logs an error message."},{"title":"Cache#clearKeepCookies","link":"clearKeepCookies","description":"Clears the browser cache while keeping the cookies.\n\nFor Firefox, it uses the extensionServer setup with specific options.\nFor Chrome and Edge, it uses the Chrome DevTools Protocol (CDP) command to clear the cache.\nIf the browser is not supported, logs an error message."},{"title":"ChromeDevelopmentToolsProtocol","link":"ChromeDevelopmentToolsProtocol"},{"title":"ChromeDevelopmentToolsProtocol#getRawClient","link":"getRawClient","description":"Retrieves the raw client for the DevTools Protocol."},{"title":"ChromeDevelopmentToolsProtocol#on","link":"on","description":"Sets up an event listener for a specific DevTools Protocol event."},{"title":"ChromeDevelopmentToolsProtocol#send","link":"send","description":"Sends a command to the DevTools Protocol."},{"title":"ChromeDevelopmentToolsProtocol#sendAndGet","link":"sendAndGet","description":"Sends a command to the DevTools Protocol and returns the result."},{"title":"ChromeTrace","link":"ChromeTrace"},{"title":"ChromeTrace#start","link":"start","description":"Starts the Chrome trace collection."},{"title":"ChromeTrace#stop","link":"stop","description":"Stops the Chrome trace collection, processes the collected data, and attaches it to the result object."},{"title":"Click","link":"Click"},{"title":"Click#byClassName","link":"byClassName","description":"Clicks on an element identified by its class name."},{"title":"Click#byClassNameAndWait","link":"byClassNameAndWait","description":"Clicks on an element identified by its class name and waits for the page complete check to finish."},{"title":"Click#byId","link":"byId","description":"Clicks on an element located by its ID."},{"title":"Click#byIdAndWait","link":"byIdAndWait","description":"Click on link located by the ID attribute. Uses document.getElementById() to find the element. And wait for page complete check to finish."},{"title":"Click#byJs","link":"byJs","description":"Clicks on an element located by evaluating a JavaScript expression."},{"title":"Click#byJsAndWait","link":"byJsAndWait","description":"Clicks on an element located by evaluating a JavaScript expression and waits for the page complete check to finish."},{"title":"Click#byLinkText","link":"byLinkText","description":"Clicks on a link whose visible text matches the given string."},{"title":"Click#byLinkTextAndWait","link":"byLinkTextAndWait","description":"Clicks on a link whose visible text matches the given string and waits for the page complete check to finish."},{"title":"Click#byName","link":"byName","description":"Clicks on an element located by its name attribute."},{"title":"Click#byPartialLinkText","link":"byPartialLinkText","description":"Clicks on a link whose visible text contains the given substring."},{"title":"Click#byPartialLinkTextAndWait","link":"byPartialLinkTextAndWait","description":"Clicks on a link whose visible text contains the given substring and waits for the page complete check to finish."},{"title":"Click#bySelector","link":"bySelector","description":"Clicks on an element located by its CSS selector."},{"title":"Click#bySelectorAndWait","link":"bySelectorAndWait","description":"Clicks on an element located by its CSS selector and waits for the page complete check to finish."},{"title":"Click#byXpath","link":"byXpath","description":"Clicks on an element that matches a given XPath selector."},{"title":"Click#byXpathAndWait","link":"byXpathAndWait","description":"Clicks on an element that matches a given XPath selector and waits for the page complete check to finish."},{"title":"ClickAndHold","link":"ClickAndHold"},{"title":"ClickAndHold#atCursor","link":"atCursor","description":"Clicks and holds at the current cursor position."},{"title":"ClickAndHold#atPosition","link":"atPosition","description":"Clicks and holds at the specified screen coordinates."},{"title":"ClickAndHold#bySelector","link":"bySelector","description":"Clicks and holds on an element that matches a given CSS selector."},{"title":"ClickAndHold#byXpath","link":"byXpath","description":"Clicks and holds on an element that matches a given XPath selector."},{"title":"ClickAndHold#releaseAtPosition","link":"releaseAtPosition","description":"Releases the mouse button at the specified screen coordinates."},{"title":"ClickAndHold#releaseAtSelector","link":"releaseAtSelector","description":"Releases the mouse button on an element matching the specified CSS selector."},{"title":"ClickAndHold#releaseAtXpath","link":"releaseAtXpath","description":"Releases the mouse button on an element matching the specified XPath."},{"title":"Commands","link":"Commands"},{"title":"Commands#action","link":"action","description":"Selenium's action sequence functionality."},{"title":"Commands#addText","link":"addText","description":"Provides functionality to add text to elements on a web page using various selectors."},{"title":"Commands#android","link":"android","description":"Provides commands for interacting with an Android device."},{"title":"Commands#cache","link":"cache","description":"Manages the browser's cache."},{"title":"Commands#cdp","link":"cdp","description":"Use the Chrome DevTools Protocol, available in Chrome and Edge."},{"title":"Commands#click","link":"click","description":"Provides functionality to perform click actions on elements in a web page using various selectors."},{"title":"Commands#debug","link":"debug","description":"Provides debugging capabilities within a browser automation script.\nIt allows setting breakpoints to pause script execution and inspect the current state."},{"title":"Commands#element","link":"element","description":"Get Selenium's WebElements."},{"title":"Commands#error","link":"error","description":"Add a text that will be an error attached to the current page."},{"title":"Commands#js","link":"js","description":"Executes JavaScript in the browser context."},{"title":"Commands#markAsFailure","link":"markAsFailure","description":"Mark this run as an failure. Add a message that explains the failure."},{"title":"Commands#measure","link":"measure","description":"Provides functionality for measuring a navigation."},{"title":"Commands#meta","link":"meta","description":"Adds metadata to the user journey."},{"title":"Commands#mouse","link":"mouse","description":"Interact with the page using the mouse."},{"title":"Commands#mouse.clickAndHold","link":"clickAndHold","description":"Provides functionality to click and hold elements on a web page using different strategies."},{"title":"Commands#mouse.contextClick","link":"contextClick","description":"Perform a context click (right-click) on elements in a web page."},{"title":"Commands#mouse.doubleClick","link":"doubleClick","description":"Provides functionality to perform a double-click action on elements in a web page."},{"title":"Commands#mouse.moveTo","link":"moveTo","description":"Move the mouse cursor to elements or specific positions on a web page."},{"title":"Commands#mouse.singleClick","link":"singleClick","description":"Provides functionality to perform a single click action on elements or at specific positions in a web page."},{"title":"Commands#navigate","link":"navigate","description":"Navigates to a specified URL and handles additional setup for a page visit."},{"title":"Commands#navigation","link":"navigation","description":"Provides functionality to control browser navigation such as back, forward, and refresh actions."},{"title":"Commands#screenshot","link":"screenshot","description":"Takes and manages screenshots."},{"title":"Commands#scroll","link":"scroll","description":"Provides functionality to control page scrolling in the browser."},{"title":"Commands#select","link":"select","description":"Interact with a select element."},{"title":"Commands#set","link":"set","description":"Sets values on HTML elements in the page."},{"title":"Commands#stopWatch","link":"stopWatch","description":"Stopwatch utility for measuring time intervals."},{"title":"Commands#switch","link":"switch","description":"Switches context to different frames, windows, or tabs in the browser."},{"title":"Commands#trace","link":"trace","description":"Manages Chrome trace functionality, enabling custom profiling and trace collection in Chrome."},{"title":"Commands#wait","link":"wait","description":"Provides functionality to wait for different conditions in the browser."},{"title":"ContextClick","link":"ContextClick"},{"title":"ContextClick#atCursor","link":"atCursor","description":"Performs a context click (right-click) at the current cursor position."},{"title":"ContextClick#bySelector","link":"bySelector","description":"Performs a context click (right-click) on an element that matches a given CSS selector."},{"title":"ContextClick#byXpath","link":"byXpath","description":"Performs a context click (right-click) on an element that matches a given XPath selector."},{"title":"Debug","link":"Debug"},{"title":"Debug#breakpoint","link":"breakpoint","description":"Adds a breakpoint to the script. The browser will pause at the breakpoint, waiting for user input to continue.\nThis is useful for debugging and inspecting the browser state at a specific point in the script."},{"title":"DoubleClick","link":"DoubleClick"},{"title":"DoubleClick#atCursor","link":"atCursor","description":"Performs a mouse double-click at the current cursor position."},{"title":"DoubleClick#bySelector","link":"bySelector","description":"Performs a mouse double-click on an element matching a given CSS selector."},{"title":"DoubleClick#byXpath","link":"byXpath","description":"Performs a mouse double-click on an element matching a given XPath selector."},{"title":"Element","link":"Element"},{"title":"Element#getByClassName","link":"getByClassName","description":"Finds an element by its class name."},{"title":"Element#getByCss","link":"getByCss","description":"Finds an element by its CSS selector."},{"title":"Element#getById","link":"getById","description":"Finds an element by its ID."},{"title":"Element#getByName","link":"getByName","description":"Finds an element by its name attribute."},{"title":"Element#getByXpath","link":"getByXpath","description":"Finds an element by its XPath."},{"title":"GeckoProfiler","link":"GeckoProfiler"},{"title":"GeckoProfiler#start","link":"start","description":"Starts the Gecko Profiler."},{"title":"GeckoProfiler#stop","link":"stop","description":"Stops the Gecko Profiler and processes the collected data."},{"title":"JavaScript","link":"JavaScript"},{"title":"JavaScript#run","link":"run","description":"Executes a JavaScript script."},{"title":"JavaScript#runAndWait","link":"runAndWait","description":"Executes a JavaScript script and waits for the page complete check to finish."},{"title":"JavaScript#runPrivileged","link":"runPrivileged","description":"Executes synchronous privileged JavaScript."},{"title":"JavaScript#runPrivilegedAndWait","link":"runPrivilegedAndWait","description":"Executes synchronous privileged JavaScript and waits for the page complete check to finish."},{"title":"JavaScript#runPrivilegedAsync","link":"runPrivilegedAsync","description":"Executes asynchronous privileged JavaScript."},{"title":"Measure","link":"Measure"},{"title":"Measure#add","link":"add","description":"Adds a custom metric to the current measurement result.\nThis method should be called after a measurement has started and before it has stopped."},{"title":"Measure#addObject","link":"addObject","description":"Adds multiple custom metrics to the current measurement result.\nThis method accepts an object containing multiple key-value pairs representing different metrics.\nSimilar to `add`, it should be used within an active measurement cycle."},{"title":"Measure#start","link":"start","description":"Starts the measurement process for a given URL or an alias.\n\nIt supports starting measurements by either directly providing a URL or using an alias.\nIf a URL is provided, it navigates to that URL and performs the measurement.\nIf an alias is provided, or no URL is available, it sets up the environment for a user-driven navigation."},{"title":"Measure#stop","link":"stop","description":"Stops the measurement process, collects metrics, and handles any post-measurement tasks.\nIt finalizes the URL being tested, manages any URL-specific metadata, stops any ongoing video recordings,\nand initiates the data collection process."},{"title":"Meta","link":"Meta"},{"title":"Meta#setDescription","link":"setDescription","description":"Sets the description for the user journey."},{"title":"Meta#setTitle","link":"setTitle","description":"Sets the title for the user journey."},{"title":"MouseMove","link":"MouseMove"},{"title":"MouseMove#byOffset","link":"byOffset","description":"Moves the mouse cursor by an offset from its current position."},{"title":"MouseMove#bySelector","link":"bySelector","description":"Moves the mouse cursor to an element that matches a given CSS selector."},{"title":"MouseMove#byXpath","link":"byXpath","description":"Moves the mouse cursor to an element that matches a given XPath selector."},{"title":"MouseMove#toPosition","link":"toPosition","description":"Moves the mouse cursor to a specific position on the screen."},{"title":"Navigation","link":"Navigation"},{"title":"Navigation#back","link":"back","description":"Navigates backward in the browser's history."},{"title":"Navigation#forward","link":"forward","description":"Navigates forward in the browser's history."},{"title":"Navigation#refresh","link":"refresh","description":"Refreshes the current page."},{"title":"Screenshot","link":"Screenshot"},{"title":"Screenshot#take","link":"take","description":"Takes a screenshot and saves it using the screenshot manager."},{"title":"Scroll","link":"Scroll"},{"title":"Scroll#byLines","link":"byLines","description":"Scrolls the page by the specified number of lines. This method is only supported in Firefox."},{"title":"Scroll#byPages","link":"byPages","description":"Scrolls the page by the specified number of pages."},{"title":"Scroll#byPixels","link":"byPixels","description":"Scrolls the page by the specified number of pixels."},{"title":"Scroll#toBottom","link":"toBottom","description":"Scrolls to the bottom of the page, scrolling page by page with a delay between each scroll."},{"title":"Select","link":"Select"},{"title":"Select#deselectById","link":"deselectById","description":"Deselects all options in a `` element by its ID."},{"title":"Select#getValuesById","link":"getValuesById","description":"Retrieves all option values in a `` element by its ID and the index of the option."},{"title":"Select#selectByIdAndValue","link":"selectByIdAndValue","description":"Selects an option in a `` element by its name and the index of the option."},{"title":"Select#selectByNameAndValue","link":"selectByNameAndValue","description":"Selects an option in a `` element by its ID."},{"title":"Select#getSelectedValueById","link":"getSelectedValueById","description":"Retrieves the value of the selected option in a `` element by its ID."},{"title":"Select#selectByIdAndIndex","link":"selectByIdAndIndex","description":"Selects an option in a `` element by its ID and the value of the option."},{"title":"Select#selectByNameAndIndex","link":"selectByNameAndIndex","description":"Selects an option in a `` element by its name and the value of the option."},{"title":"Set","link":"Set"},{"title":"Set#innerHtml","link":"innerHtml","description":"Sets the innerHTML of an element using a CSS selector."},{"title":"Set#innerHtmlById","link":"innerHtmlById","description":"Sets the innerHTML of an element using its ID."},{"title":"Set#innerText","link":"innerText","description":"Sets the innerText of an element using a CSS selector."},{"title":"Set#innerTextById","link":"innerTextById","description":"Sets the innerText of an element using its ID."},{"title":"Set#value","link":"value","description":"Sets the value of an element using a CSS selector."},{"title":"Set#valueById","link":"valueById","description":"Sets the value of an element using its ID."},{"title":"SingleClick","link":"SingleClick"},{"title":"SingleClick#atCursor","link":"atCursor","description":"Performs a single mouse click at the current cursor position."},{"title":"SingleClick#atCursorAndWait","link":"atCursorAndWait","description":"Performs a single mouse click at the current cursor position and waits on the\npage complete check."},{"title":"SingleClick#byLinkText","link":"byLinkText","description":"Clicks on a link whose visible text matches the given string."},{"title":"SingleClick#byLinkTextAndWait","link":"byLinkTextAndWait","description":"Clicks on a link whose visible text matches the given string and waits on the opage complete check."},{"title":"SingleClick#byPartialLinkText","link":"byPartialLinkText","description":"Clicks on a link whose visible text contains the given substring."},{"title":"SingleClick#byPartialLinkTextAndWait","link":"byPartialLinkTextAndWait","description":"Clicks on a link whose visible text contains the given substring and waits on the\npage complete checl."},{"title":"SingleClick#bySelector","link":"bySelector","description":"Performs a single mouse click on an element matching a given CSS selector."},{"title":"SingleClick#bySelectorAndWait","link":"bySelectorAndWait","description":"Performs a single mouse click on an element matching a given CSS selector and waits on the page complete check."},{"title":"SingleClick#byXpath","link":"byXpath","description":"Performs a single mouse click on an element matching a given XPath selector."},{"title":"SingleClick#byXpathAndWait","link":"byXpathAndWait","description":"Performs a single mouse click on an element matching a given XPath selector and wait for page complete check."},{"title":"StopWatch","link":"StopWatch"},{"title":"StopWatch#getName","link":"getName","description":"Gets the name of the stopwatch."},{"title":"StopWatch#start","link":"start","description":"Starts the stopwatch."},{"title":"StopWatch#stop","link":"stop","description":"Stops the stopwatch."},{"title":"StopWatch#stopAndAdd","link":"stopAndAdd","description":"Stops the stopwatch and automatically adds the measured time to the\nlast measured page. Logs an error if no page has been measured."},{"title":"Switch","link":"Switch"},{"title":"Switch#toFrame","link":"toFrame","description":"Switches to a frame identified by its ID."},{"title":"Switch#toFrameBySelector","link":"toFrameBySelector","description":"Switches to a frame identified by a CSS selector."},{"title":"Switch#toFrameByXpath","link":"toFrameByXpath","description":"Switches to a frame identified by an XPath."},{"title":"Switch#toNewTab","link":"toNewTab","description":"Opens a new tab and optionally navigates to a URL."},{"title":"Switch#toNewWindow","link":"toNewWindow","description":"Opens a new window and optionally navigates to a URL."},{"title":"Switch#toParentFrame","link":"toParentFrame","description":"Switches to the parent frame of the current frame."},{"title":"Switch#toWindow","link":"toWindow","description":"Switches to a window identified by its name."},{"title":"Wait","link":"Wait"},{"title":"Wait#byCondition","link":"byCondition","description":"Waits for a JavaScript condition to return a truthy value within a maximum time."},{"title":"Wait#byId","link":"byId","description":"Waits for an element with a specific ID to appear within a maximum time."},{"title":"Wait#byPageToComplete","link":"byPageToComplete","description":"Waits for the page to finish loading."},{"title":"Wait#bySelector","link":"bySelector","description":"Waits for an element located by a CSS selector to appear within a maximum time."},{"title":"Wait#byTime","link":"byTime","description":"Waits for a specified amount of time."},{"title":"Wait#byXpath","link":"byXpath","description":"Waits for an element located by XPath to appear within a maximum time."}]} \ No newline at end of file diff --git a/docs/documentation/sitespeed.io/scripting/tutorial-06-Error-handling.html b/docs/documentation/sitespeed.io/scripting/tutorial-06-Error-handling.html index f99d0eb6d5..02353a40fc 100644 --- a/docs/documentation/sitespeed.io/scripting/tutorial-06-Error-handling.html +++ b/docs/documentation/sitespeed.io/scripting/tutorial-06-Error-handling.html @@ -1,15 +1,22 @@ Tutorial: Error handling
On this page

Error handling

You can try/catch failing commands that throw errors. If an error is not caught in your script, it will be caught in sitespeed.io and the error will be logged and reported in the HTML and to your data storage (Graphite/InfluxDb) under the key browsertime.statistics.errors.

If you do catch the error, you should make sure you report it yourself with the error function, so you can see that in the HTML. This is needed for all errors except navigating/measuring a URL. They will automatically be reported (since they are always important).

Here's an example of catching a URL that don't work and still continue to test another one. Remember since a navigation fails, this will be reported automatically and you don't need to do anything.

/**
+    
On this page

Error handling

You can try/catch failing commands that throw errors. If an error is not caught in your script, it will be caught in sitespeed.io and the error will be logged and reported in the HTML and to your data storage (Graphite/InfluxDb) under the key browsertime.statistics.errors.

If you do catch the error, you should make sure you report it yourself with the error function, so you can see that in the HTML report. This is needed for all errors except navigating/measuring a URL. They will automatically be reported (since they are always important).

If you measuring a page in a user journey and it fails, you can stop your measurements as a error and not collect any metrics. This works from Browsertime 21.2.0 and sitespeed.io 33.0.0.

/**
  * @param {import('browsertime').BrowsertimeContext} context
  * @param {import('browsertime').BrowsertimeCommands} commands
  */
 export default async function (context, commands) {
   await commands.measure.start('https://www.sitespeed.io');
+
   try {
-    await commands.measure.start('https://nonworking.url/');
-  } catch (e) {}
-  return commands.measure.start('https://www.sitespeed.io/documentation/');
+    await commands.measure.start('Documentation_page');
+    await commands.click.byLinkTextAndWait('Documentationsssss');
+    return commands.measure.stop();
+    
+  } catch (e) {
+    // Oops that link do not exist and will throw an exception
+    return commands.measure.stopAsError('Could not click on the link');
+  }
+
 };
 

You can also create your own errors. The error will be reported in the HTML and sent to Graphite/InfluxDB. If you report an error, the exit code from sitespeed.io will be > 0.

/**
  * @param {import('browsertime').BrowsertimeContext} context