Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions types/k6/browser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -4990,6 +5023,37 @@ export interface Page {
waitUntil?: "load" | "domcontentloaded" | "networkidle";
}): Promise<Response | null>;

/**
* 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<Response | null>;

/**
* 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<Response | null>;

/**
* Adds a route to the page to modify network requests made by that page.
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions types/k6/package.json
Original file line number Diff line number Diff line change
@@ -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/"
Expand All @@ -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": [
{
Expand Down
80 changes: 80 additions & 0 deletions types/k6/test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,68 @@ async function test() {
response.url();
});

// $ExpectType void
page.on("requestfailed", request => {
// $ExpectType Promise<Record<string, string>>
request.allHeaders();
// $ExpectType Frame
request.frame();
// $ExpectType Record<string, string>
request.headers();
// $ExpectType Promise<{ name: string; value: string; }[]>
request.headersArray();
// $ExpectType Promise<string | null>
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<Response | null>
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<Record<string, string>>
request.allHeaders();
// $ExpectType Frame
request.frame();
// $ExpectType Record<string, string>
request.headers();
// $ExpectType Promise<{ name: string; value: string; }[]>
request.headersArray();
// $ExpectType Promise<string | null>
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<Response | null>
request.response();
// $ExpectType Promise<{ body: number; headers: number; }>
request.size();
// $ExpectType ResourceTiming
request.timing();
});

// $ExpectType Promise<Page | null>
page.opener();

Expand Down Expand Up @@ -782,6 +844,24 @@ async function test() {
// $ExpectType Promise<Response | null>
page.reload({ waitUntil: "domcontentloaded" });

// $ExpectType Promise<Response | null>
page.goBack();
// $ExpectType Promise<Response | null>
page.goBack({ timeout: 10000 });
// $ExpectType Promise<Response | null>
page.goBack({ waitUntil: "domcontentloaded" });
// $ExpectType Promise<Response | null>
page.goBack({ timeout: 10000, waitUntil: "load" });

// $ExpectType Promise<Response | null>
page.goForward();
// $ExpectType Promise<Response | null>
page.goForward({ timeout: 10000 });
// $ExpectType Promise<Response | null>
page.goForward({ waitUntil: "domcontentloaded" });
// $ExpectType Promise<Response | null>
page.goForward({ timeout: 10000, waitUntil: "load" });

// $ExpectType Promise<ArrayBuffer>
page.screenshot();
// $ExpectType Promise<ArrayBuffer>
Expand Down
2 changes: 1 addition & 1 deletion types/k6/test/websockets.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion types/k6/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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/
*/

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -80,47 +79,47 @@ 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
*/
onmessage: (event?: MessageEvent) => void;

/**
* 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
*/
onerror: (event?: ErrorEvent) => void;

/**
* 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;
}
Expand All @@ -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. */
Expand Down