diff --git a/types/k6/browser/index.d.ts b/types/k6/browser/index.d.ts index eaad25e6abaa13..b82c2a87ce3f01 100644 --- a/types/k6/browser/index.d.ts +++ b/types/k6/browser/index.d.ts @@ -4891,6 +4891,39 @@ export interface Page { */ on(event: "response", listener: (response: Response) => void): void; + /** + * Registers a handler function to listen for network requests that + * fail to reach the server (DNS errors, connection refused, timeouts, etc.). + * The handler will receive an instance of {@link Request}, which includes + * information about the failed request. + * + * **Usage** + * + * ```js + * page.on('requestfailed', request => { + * const failure = request.failure(); + * console.log(`Request failed: ${request.url()}`); + * console.log(` Error: ${failure ? failure.errorText : 'unknown'}`); + * }); + * ``` + */ + on(event: "requestfailed", listener: (request: Request) => void): void; + + /** + * Registers a handler function to listen for network requests that + * successfully complete (receive a response). The handler will receive an + * instance of {@link Request}, which includes information about the request. + * + * **Usage** + * + * ```js + * page.on('requestfinished', request => { + * console.log(`Request finished: ${request.method()} ${request.url()}`); + * }); + * ``` + */ + on(event: "requestfinished", listener: (request: Request) => void): void; + /** * Returns the page that opened the current page. The first page that is * navigated to will have a null opener. @@ -4990,6 +5023,37 @@ export interface Page { waitUntil?: "load" | "domcontentloaded" | "networkidle"; }): Promise; + /** + * Goes back to the previous page in the history. + * + * @example + * ```js + * await page.goto('https://example.com'); + * await page.goto('https://example.com/page2'); + * await page.goBack(); // Will navigate back to the first page + * ``` + * + * @param options Navigation options. + * @returns A promise that resolves to the response of the requested navigation when it happens. + * Returns null if there is no previous entry in the history. + */ + goBack(options?: NavigationOptions): Promise; + + /** + * Goes forward to the next page in the history. + * + * @example + * ```js + * await page.goBack(); // Navigate back first + * await page.goForward(); // Then navigate forward + * ``` + * + * @param options Navigation options. + * @returns A promise that resolves to the response of the requested navigation when it happens. + * Returns null if there is no next entry in the history. + */ + goForward(options?: NavigationOptions): Promise; + /** * Adds a route to the page to modify network requests made by that page. * @@ -6003,6 +6067,25 @@ export interface Request { * @returns request URL */ url(): string; + + /** + * Returns the failure info for a failed request, or null if the request succeeded. + * This method returns information about network failures such as DNS errors, + * connection refused, timeouts, etc. It does not return information for HTTP + * 4xx/5xx responses, which are successful network requests. + * @returns The failure information or null if the request succeeded. + */ + failure(): RequestFailure | null; +} + +/** + * RequestFailure contains information about a failed request. + */ +export interface RequestFailure { + /** + * The error text describing why the request failed. + */ + errorText: string; } /** diff --git a/types/k6/package.json b/types/k6/package.json index 8ea5bc79f0429c..3c61bef41e7a32 100644 --- a/types/k6/package.json +++ b/types/k6/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "@types/k6", - "version": "1.5.9999", + "version": "1.6.9999", "type": "module", "projects": [ "https://grafana.com/docs/k6/latest/" @@ -28,7 +28,7 @@ "./experimental/fs": "./experimental/fs/index.d.ts", "./experimental/redis": "./experimental/redis/index.d.ts", "./experimental/streams": "./experimental/streams/index.d.ts", - "./experimental/websockets": "./experimental/websockets/index.d.ts" + "./websockets": "./websockets/index.d.ts" }, "owners": [ { diff --git a/types/k6/test/browser.ts b/types/k6/test/browser.ts index bbd4efbdb677fc..820d1b108bde80 100644 --- a/types/k6/test/browser.ts +++ b/types/k6/test/browser.ts @@ -670,6 +670,68 @@ async function test() { response.url(); }); + // $ExpectType void + page.on("requestfailed", request => { + // $ExpectType Promise> + request.allHeaders(); + // $ExpectType Frame + request.frame(); + // $ExpectType Record + request.headers(); + // $ExpectType Promise<{ name: string; value: string; }[]> + request.headersArray(); + // $ExpectType Promise + request.headerValue("content-type"); + // $ExpectType boolean + request.isNavigationRequest(); + // $ExpectType string + request.method(); + // $ExpectType string | null + request.postData(); + // $ExpectType ArrayBuffer | null + request.postDataBuffer(); + // $ExpectType ResourceType + request.resourceType(); + // $ExpectType Promise + request.response(); + // $ExpectType Promise<{ body: number; headers: number; }> + request.size(); + // $ExpectType ResourceTiming + request.timing(); + // $ExpectType RequestFailure | null + request.failure(); + }); + + // $ExpectType void + page.on("requestfinished", request => { + // $ExpectType Promise> + request.allHeaders(); + // $ExpectType Frame + request.frame(); + // $ExpectType Record + request.headers(); + // $ExpectType Promise<{ name: string; value: string; }[]> + request.headersArray(); + // $ExpectType Promise + request.headerValue("content-type"); + // $ExpectType boolean + request.isNavigationRequest(); + // $ExpectType string + request.method(); + // $ExpectType string | null + request.postData(); + // $ExpectType ArrayBuffer | null + request.postDataBuffer(); + // $ExpectType ResourceType + request.resourceType(); + // $ExpectType Promise + request.response(); + // $ExpectType Promise<{ body: number; headers: number; }> + request.size(); + // $ExpectType ResourceTiming + request.timing(); + }); + // $ExpectType Promise page.opener(); @@ -782,6 +844,24 @@ async function test() { // $ExpectType Promise page.reload({ waitUntil: "domcontentloaded" }); + // $ExpectType Promise + page.goBack(); + // $ExpectType Promise + page.goBack({ timeout: 10000 }); + // $ExpectType Promise + page.goBack({ waitUntil: "domcontentloaded" }); + // $ExpectType Promise + page.goBack({ timeout: 10000, waitUntil: "load" }); + + // $ExpectType Promise + page.goForward(); + // $ExpectType Promise + page.goForward({ timeout: 10000 }); + // $ExpectType Promise + page.goForward({ waitUntil: "domcontentloaded" }); + // $ExpectType Promise + page.goForward({ timeout: 10000, waitUntil: "load" }); + // $ExpectType Promise page.screenshot(); // $ExpectType Promise diff --git a/types/k6/test/websockets.ts b/types/k6/test/websockets.ts index ada829c0b74365..01f6ea4243efbc 100644 --- a/types/k6/test/websockets.ts +++ b/types/k6/test/websockets.ts @@ -1,5 +1,5 @@ -import { Blob, CompressionAlgorithm, EventName, WebSocket } from "k6/experimental/websockets"; import { CookieJar } from "k6/http"; +import { Blob, CompressionAlgorithm, EventName, WebSocket } from "k6/websockets"; let str: string; let ab: ArrayBuffer; diff --git a/types/k6/tsconfig.json b/types/k6/tsconfig.json index 60b5479adfc7e2..d7f4394a3919a6 100644 --- a/types/k6/tsconfig.json +++ b/types/k6/tsconfig.json @@ -36,8 +36,8 @@ "experimental/csv/index.d.ts", "experimental/fs/index.d.ts", "experimental/redis/index.d.ts", - "experimental/websockets/index.d.ts", "experimental/streams/index.d.ts", + "websockets/index.d.ts", "test/browser.ts", "test/data.ts", diff --git a/types/k6/experimental/websockets/index.d.ts b/types/k6/websockets/index.d.ts similarity index 83% rename from types/k6/experimental/websockets/index.d.ts rename to types/k6/websockets/index.d.ts index ae4cf7dfca5d1d..9c4dcb8fadab2e 100644 --- a/types/k6/experimental/websockets/index.d.ts +++ b/types/k6/websockets/index.d.ts @@ -1,11 +1,10 @@ -import { CookieJar } from "../../http/index.js"; -import { ReadableStream } from "../streams/index.js"; +import { ReadableStream } from "../experimental/streams/index.js"; +import { CookieJar } from "../http/index.js"; /** - * This module provides an experimental implementation of the WebSocket API - * for k6. + * This module provides a WebSocket API for k6. * - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/ */ /** @@ -43,7 +42,7 @@ export class WebSocket { /** * The Websocket constructor returns a newly created WebSocket object. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/ * * @param url - The URL to which to connect; this should be the URL to which the WebSocket server will respond. * @param protocols - Either a single protocol string or an array of protocol strings. The param is reserved for future use and will be presently ignored. @@ -53,7 +52,7 @@ export class WebSocket { /** * Enqueues data to be transmitted to the server over the WebSocket connection. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-send/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-send/ * * @param data - the data to send to the server */ @@ -62,7 +61,7 @@ export class WebSocket { /** * Bind event names to event handlers to be executed when their * respective event is received by the server. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-addeventlistener/ * * @param event - the event to listen for * @param listener - the callback to invoke when the event is emitted @@ -71,7 +70,7 @@ export class WebSocket { /** * Closes the WebSocket connection or connection attempt, if any. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-close/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-close/ * * @param code - An integer WebSocket connection close code value indicating a reason for closure. * @param reason - A human-readable string WebSocket connection close reason. No longer than 123 bytes of UTF-8 text. @@ -80,13 +79,13 @@ export class WebSocket { /** * Sends a ping message over the WebSocket connection. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-ping/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-ping/ */ ping(): void; /** * Sets an event handler which is invoked when a message event happens. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onmessage/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onmessage/ * * @param event - the message event */ @@ -94,19 +93,19 @@ export class WebSocket { /** * Sets an event handler which is invoked when the WebSocket connection's opens. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onopen/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onopen/ */ onopen: () => void; /** * Sets an event handler which is invoked when the WebSocket connection's closes. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onclose/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onclose/ */ onclose: () => void; /** * Sets an event handler which is invoked when errors occur. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onerror/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onerror/ * * @param event - the error event */ @@ -114,13 +113,13 @@ export class WebSocket { /** * Sets an event handler which is invoked when a ping message is received. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onping/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onping/ */ onping: () => void; /** * Sets an event handler which is invoked when a pong message is received. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/websocket/websocket-onpong/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/websocket/websocket-onpong/ */ onpong: () => void; } @@ -147,7 +146,7 @@ export class Blob { /** * k6 specific WebSocket parameters. - * https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/params/ + * https://grafana.com/docs/k6/latest/javascript-api/k6-websockets/params/ */ export interface Params { /** Request headers. */