Skip to content

Commit 5da81d8

Browse files
authored
🤖 Merge PR DefinitelyTyped#66347 Fix k6 browser type definitions by @ankur22
* Remove white space * Fix browser definition to interface There was an error with the definition which didn't reflect how it was meant to be used -- the browser named export should be worked without having to new up a new Browser instance and it should allow access to its methods. * Fix evaluate and evaluateHandle definitions * Refactor class defs to interfaces and unexport We should work with interface types instead of classes, since working with a class infers that the type can be instantiated with the new operator. With the k6 browser module we currently do not need to new up an instance of any type that the module works with. We need to correctly reflect that the k6 browser module only exports browser and not the other types that were being exported. This also does a better a job of showing us how to get to some of the other types when working with browser e.g. page, response, request etc. * Remove export {} and export all types/interfaces There is a valid use case for wanting to work with the types that are declared, which is to be able to do the following: ```js async function doSomethingWithPage(page: Page): Promise<void> { ``` If we didn't export the types then we would have to work with the `any` keyword like so: ```js async function doSomethingWithPage(page: any): Promise<void> { ``` The change is to remove `export {}` and to export all types explicitly. Resolves: DefinitelyTyped#66347 (comment)
1 parent fcacb00 commit 5da81d8

File tree

2 files changed

+108
-112
lines changed

2 files changed

+108
-112
lines changed

types/k6/experimental/browser.d.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export type MouseMultiClickOptions = MouseClickOptions & {
152152
clickCount?: number;
153153
};
154154

155-
export interface MouseDownUpOptions {
155+
export interface MouseDownUpOptions {
156156
/**
157157
* The mouse button to use during the action.
158158
* Defaults to `left`.
@@ -530,10 +530,15 @@ export interface NewBrowserContextOptions {
530530
}
531531

532532
/**
533-
* The `Browser` class is the entry point for all your tests, it interacts
534-
* with the actual web browser via Chrome DevTools Protocol (CDP).
533+
* The `browser` named export is the entry point for all your tests,
534+
* it interacts with the actual web browser via Chrome DevTools Protocol (CDP).
535535
*/
536-
export class Browser {
536+
export const browser: Browser;
537+
538+
/**
539+
* `Browser` represents the main web browser instance.
540+
*/
541+
export interface Browser {
537542
/**
538543
* Returns the current `BrowserContext`. There is a 1-to-1 mapping between
539544
* `Browser` and `BrowserContext`. If no `BrowserContext` has been
@@ -585,7 +590,7 @@ export class Browser {
585590
* `BrowserContext` provides a way to operate multiple independent sessions, with
586591
* separate pages, cache, and cookies.
587592
*/
588-
export class BrowserContext {
593+
export interface BrowserContext {
589594
/**
590595
* Returns the `Browser` instance that this `BrowserContext` belongs to.
591596
*/
@@ -763,7 +768,7 @@ export class BrowserContext {
763768
/**
764769
* ElementHandle represents an in-page DOM element.
765770
*/
766-
export class ElementHandle extends JSHandle {
771+
export interface ElementHandle extends JSHandle {
767772
/**
768773
* Finds an element matching the specified selector in the `ElementHandle`'s subtree.
769774
* @param selector A selector to query element for.
@@ -959,7 +964,7 @@ export class ElementHandle extends JSHandle {
959964
/**
960965
* Frame represents the frame within a page. A page is made up of hierarchy of frames.
961966
*/
962-
export class Frame {
967+
export interface Frame {
963968
/**
964969
* Finds an element matching the specified selector within the `Frame`.
965970
* @param selector A selector to query element for.
@@ -1306,7 +1311,7 @@ export class Frame {
13061311
/**
13071312
* JSHandle represents an in-page JavaScript object.
13081313
*/
1309-
export class JSHandle<T = any> {
1314+
export interface JSHandle<T = any> {
13101315
/**
13111316
* Returns either `null` or the object handle itself, if the object handle is
13121317
* an instance of `ElementHandle`.
@@ -1326,7 +1331,7 @@ export class JSHandle<T = any> {
13261331
* @param args The arguments to pass to the page function.
13271332
* @returns The return value of `pageFunction`.
13281333
*/
1329-
evaluate<R, Arg>(pageFunction: PageFunction<R, Arg>, ...args: any): any;
1334+
evaluate<R, Arg>(pageFunction: PageFunction<Arg, R>, arg?: Arg): R;
13301335

13311336
/**
13321337
* Evaluates the page function and returns a `JSHandle`.
@@ -1336,7 +1341,7 @@ export class JSHandle<T = any> {
13361341
* @param args The arguments to pass to the page function.
13371342
* @returns A JSHandle of the return value of `pageFunction`.
13381343
*/
1339-
evaluateHandle<R, Arg>(pageFunction: PageFunction<R, Arg>, ...args: any): JSHandle<R>;
1344+
evaluateHandle<R, Arg>(pageFunction: PageFunction<Arg, R>, arg?: Arg): JSHandle<R>;
13401345

13411346
/**
13421347
* Fethes a map with own property names of of the `JSHandle` with their values as
@@ -1355,7 +1360,7 @@ export class JSHandle<T = any> {
13551360
/**
13561361
* Keyboard provides an API for managing a virtual keyboard.
13571362
*/
1358-
export class Keyboard {
1363+
export interface Keyboard {
13591364
/**
13601365
* Sends a key down message to a session target.
13611366
* A superset of the key values can be found [here](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values).
@@ -1406,7 +1411,7 @@ export class Keyboard {
14061411
* - Makes it easier to work with dynamic web pages and SPAs built with Svelte,
14071412
* React, Vue, etc.
14081413
*/
1409-
export class Locator {
1414+
export interface Locator {
14101415
/**
14111416
* Mouse click on the chosen element.
14121417
* @param options Options to use.
@@ -1580,7 +1585,7 @@ export class Locator {
15801585
/**
15811586
* Mouse provides an API for managing a virtual mouse.
15821587
*/
1583-
export class Mouse {
1588+
export interface Mouse {
15841589
/**
15851590
* Shortcut for `mouse.move(x, y)`, `mouse.down()`, `mouse.up()`.
15861591
* @param x The x position.
@@ -1623,7 +1628,7 @@ export class Mouse {
16231628
* Page provides methods to interact with a single tab in a running web browser
16241629
* instance. One instance of the browser can have many page instances.
16251630
*/
1626-
export class Page {
1631+
export interface Page {
16271632
/**
16281633
* Activates the browser tab so that it comes into focus and actions can be
16291634
* performed against it.
@@ -3125,9 +3130,9 @@ export class Page {
31253130
}
31263131

31273132
/**
3128-
* Request class represents requests which are sent by a page.
3133+
* Request represents requests which are sent by a page.
31293134
*/
3130-
export class Request {
3135+
export interface Request {
31313136
/**
31323137
* An object with HTTP headers associated with the request. All header names are
31333138
* lower-case.
@@ -3223,9 +3228,9 @@ export class Request {
32233228
}
32243229

32253230
/**
3226-
* Response class represents responses which are received by page.
3231+
* Response represents responses which are received by page.
32273232
*/
3228-
export class Response {
3233+
export interface Response {
32293234
/**
32303235
* An object with HTTP headers associated with the response. All header names are
32313236
* lower-case.
@@ -3339,7 +3344,7 @@ export class Response {
33393344
* operates in main-frame CSS pixels relative to the top-left corner of the
33403345
* viewport.
33413346
*/
3342-
export class Touchscreen {
3347+
export interface Touchscreen {
33433348
/**
33443349
* Taps on the specified position (`x`,`y`), which internally dispatches a `touchstart` and `touchend` event.
33453350
* @param x The x position.
@@ -3349,9 +3354,9 @@ export class Touchscreen {
33493354
}
33503355

33513356
/**
3352-
* The Worker class represents a [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
3357+
* The Worker represents a [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
33533358
*/
3354-
export class Worker {
3359+
export interface Worker {
33553360
/**
33563361
* Get the URL of the web worker.
33573362
* @return The URL of the web worker.

0 commit comments

Comments
 (0)