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
72 changes: 72 additions & 0 deletions types/k6/browser/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference lib="dom" />

/**
* Represents event-specific properties. Refer to the events documentation for
* the lists of initial properties:
Expand All @@ -12,6 +14,7 @@
export type EvaluationArgument = object;

export type PageFunction<Arg, R> = string | ((arg: Unboxed<Arg>) => R);
export type PageFunctionOn<On, Arg2, R> = string | ((on: On, arg2: Unboxed<Arg2>) => R);

export type Unboxed<Arg> = Arg extends [infer A0, infer A1] ? [Unboxed<A0>, Unboxed<A1>]
: Arg extends [infer A0, infer A1, infer A2] ? [Unboxed<A0>, Unboxed<A1>, Unboxed<A2>]
Expand Down Expand Up @@ -2997,6 +3000,35 @@ export interface Locator {
*/
dblclick(options?: MouseMoveOptions & MouseMultiClickOptions): Promise<void>;

/**
* Evaluates the page function and returns its return value.
* This method passes this locator's matching element as the first argument to the page function.
*
* @param pageFunction Function to be evaluated in the page context.
* @param arg Optional argument to pass to `pageFunction`.
* @returns Return value of `pageFunction`.
*/
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
evaluate<R, E extends SVGElement | HTMLElement, Arg>(
pageFunction: PageFunctionOn<E, Arg, R>,
arg?: Arg,
): Promise<R>;

/**
* Evaluates the page function and returns its return value as a [JSHandle].
* This method passes this locator's matching element as the first argument to the page function.
* Unlike `evaluate`, `evaluateHandle` returns the value as a `JSHandle`
*
* @param pageFunction Function to be evaluated in the page context.
* @param arg Optional argument to pass to `pageFunction`.
* @returns JSHandle of the return value of `pageFunction`.
*/
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
evaluateHandle<R, E extends SVGElement | HTMLElement, Arg>(
pageFunction: PageFunctionOn<E, Arg, R>,
arg?: Arg,
): Promise<JSHandle<R>>;

/**
* Use this method to select an `input type="checkbox"`.
* @param options Options to use.
Expand Down Expand Up @@ -5424,6 +5456,16 @@ export interface Page {
},
): Promise<void>;

/**
* Removes all existing routes for the `url`.
*/
unroute(url: string | RegExp): Promise<void>;

/**
* Removes all routes created with page.route().
*/
unrouteAll(): Promise<void>;

/**
* Returns the page's URL.
*/
Expand Down Expand Up @@ -5621,6 +5663,36 @@ export interface Page {
},
): Promise<Response | null>;

/**
* Waits for the page to match against the URL for a Request object
*
* @example
* ```js
* const requestPromise = page.waitForRequest('https://example.com/resource');
* await page.goto('https://example.com/resource');
* const request = await requestPromise;
* ```
*
* @param request Request URL string or regex to match against Request object.
* @param options Options to use.
*/
waitForRequest(
request: string | RegExp,
options?: {
/**
* Maximum operation time in milliseconds. Defaults to `30` seconds.
* The default value can be changed via the
* browserContext.setDefaultNavigationTimeout(timeout),
* browserContext.setDefaultTimeout(timeout),
* page.setDefaultNavigationTimeout(timeout) or
* page.setDefaultTimeout(timeout) methods.
*
* Setting the value to `0` will disable the timeout.
*/
timeout?: number;
},
): Promise<Request | null>;

/**
* **NOTE** Use web assertions that assert visibility or a locator-based
* locator.waitFor([options]) instead.
Expand Down
2 changes: 1 addition & 1 deletion 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.3.9999",
"version": "1.4.9999",
"type": "module",
"projects": [
"https://grafana.com/docs/k6/latest/"
Expand Down
72 changes: 72 additions & 0 deletions types/k6/test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,15 @@ async function test() {
// $ExpectType Promise<Response | null>
page.waitForResponse("https://example.com", { timeout: 10000 });

// @ts-expect-error
page.waitForRequest();
// $ExpectType Promise<Request | null>
page.waitForRequest("https://example.com");
// $ExpectType Promise<Request | null>
page.waitForRequest(/.*\/api\/pizza$/);
// $ExpectType Promise<Request | null>
page.waitForRequest("https://example.com", { timeout: 10000 });

// @ts-expect-error
page.waitForSelector();
// $ExpectType Promise<ElementHandle>
Expand Down Expand Up @@ -1080,6 +1089,31 @@ async function test() {
// $ExpectType Promise<ElementHandle[]>
page.$$(selector);

// $ExpectType Promise<void>
page.route("https://example.com/logo.png", () => {});
// $ExpectType Promise<void>
page.route(/.*\/logo.png/i, () => {});
// @ts-expect-error
page.route();
// @ts-expect-error
page.route(123, () => {});
// @ts-expect-error
page.route("https://example.com/logo.png");

// $ExpectType Promise<void>
page.unroute("https://example.com/logo.png");
// $ExpectType Promise<void>
page.unroute(/.*\/logo.png/i);
// @ts-expect-error
page.unroute();
// @ts-expect-error
page.unroute(123);

// $ExpectType Promise<void>
page.unrouteAll();
// @ts-expect-error
page.unrouteAll("https://example.com/logo.png");

//
// Keyboard
//
Expand Down Expand Up @@ -1549,6 +1583,44 @@ async function test() {
// @ts-expect-error
locator.getByPlaceholder("name@example.com", { exact: "true" });

// @ts-expect-error
locator.evaluate();
// @ts-expect-error
locator.evaluate(1);
// @ExpectType Promise<void>
locator.evaluate("");
// @ExpectType Promise<void>
locator.evaluate(() => {});
// @ExpectType Promise<string>
locator.evaluate(() => {
"";
});
// @ExpectType Promise<string>
locator.evaluate((elem: HTMLElement, a: string) => {
a;
}, "");
// @ExpectType Promise<string[]>
locator.evaluate((el: HTMLElement, a: string[]) => a, [""]);

// @ts-expect-error
locator.evaluateHandle();
// @ts-expect-error
locator.evaluateHandle(1);
// @ExpectType Promise<JSHandle>
locator.evaluateHandle("");
// @ExpectType Promise<JSHandle>
locator.evaluateHandle(() => {});
// @ExpectType Promise<JSHandle>
locator.evaluateHandle(() => {
"";
});
// @ExpectType Promise<JSHandle>
locator.evaluateHandle((el: HTMLElement, a: string) => {
a;
}, "");
// @ExpectType Promise<JSHandle>
locator.evaluateHandle((el: HTMLElement, a: string[]) => a, [""]);

//
// JSHandle
//
Expand Down
2 changes: 0 additions & 2 deletions types/k6/test/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// open
// @ts-expect-error
open();
// @ts-expect-error
open(5);
Expand All @@ -8,7 +7,6 @@ const text: string = open("file.txt");
open(5, "b");
// @ts-expect-error
open("file.bin", 5);
// @ts-expect-error
open("file.bin", "notamode");
const arrayBuffer: ArrayBuffer = open("file.bin", "b");
// @ts-expect-error
Expand Down