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
2 changes: 0 additions & 2 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -3143,7 +3143,6 @@
/types/hash-stream/ @BendingBender
/types/hash-sum/ @DanielRosenwasser
/types/hasher/ @flyfishMT
/types/hashmap/ @rafalwrzeszcz @outring @dodomorandi
/types/hashring/ @medns
/types/hashset/ @gerich-home
/types/hashtable/ @gerich-home
Expand Down Expand Up @@ -3258,7 +3257,6 @@
/types/http-aws-es/ @marcogrcr
/types/http-build-query/ @alexu740
/types/http-cache-semantics/ @BendingBender
/types/http-codes/ @mhegazy
/types/http-context/ @mtraynham
/types/http-errors/ @tkrotoff @BendingBender @bjohansebas
/types/http-headers/ @BendingBender
Expand Down
60 changes: 60 additions & 0 deletions types/k6/browser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,66 @@ export interface NewBrowserContextOptions {
*/
export const browser: Browser;

/**
* The `chromium` named export allows attaching to an already running
* Chromium-based browser over the Chrome DevTools Protocol (CDP), instead of
* relying on the browser instance that k6 launches and manages for you.
*/
export const chromium: BrowserType;

/**
* `BrowserType` is the entry point to connect to an existing Chromium-based
* browser.
*/
export interface BrowserType {
/**
* Attaches to an already running Chromium-based browser over the Chrome
* DevTools Protocol (CDP), and returns the corresponding `Browser`
* instance.
*
* The returned `Browser` is closed automatically at the end of the
* iteration. Use {@link ConnectedBrowser.close | close} to release the
* connection earlier.
*
* **Usage**
*
* ```js
* const browser = await chromium.connectOverCDP('ws://localhost:9222/devtools/browser/<id>');
* const page = await browser.newPage();
*
* try {
* await page.goto('https://quickpizza.grafana.com/');
* } finally {
* await page.close();
* await browser.close();
* }
* ```
*
* @param wsEndpoint The WebSocket endpoint of the browser to attach to,
* e.g. 'ws://localhost:9222/devtools/browser/<id>'.
*/
connectOverCDP(wsEndpoint: string): Promise<ConnectedBrowser>;
}

/**
* `ConnectedBrowser` represents a web browser instance that k6 attached to over
* the Chrome DevTools Protocol (CDP), rather than launched itself.
*
* On top of the `Browser` API, it exposes a `close` method, since the
* connection is user managed and can be released on demand.
*/
export interface ConnectedBrowser extends Browser {
/**
* Closes the connection to the browser, and all of its `BrowserContext`s
* and `Page`s.
*
* Calling this method is optional: the connection is closed automatically
* at the end of the iteration. Closing an already closed `Browser` is a
* no-op.
*/
close(): Promise<void>;
}

/**
* `Browser` represents the main web browser instance.
*/
Expand Down
3 changes: 3 additions & 0 deletions types/k6/options/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export interface Options {
/** Cloud options */
cloud?: CloudOptions;

/** handleSummary() function timeout. */
handleSummaryTimeout?: string;

/** Static hostname mapping. */
hosts?: { [name: string]: string };

Expand Down
6 changes: 5 additions & 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": "2.0.9999",
"version": "2.2.9999",
"type": "module",
"projects": [
"https://grafana.com/docs/k6/latest/"
Expand Down Expand Up @@ -61,6 +61,10 @@
{
"name": "Iván Szkiba",
"githubUsername": "szkiba"
},
{
"name": "Jan Hildebrandt",
"githubUsername": "janHildebrandt98"
}
]
}
26 changes: 25 additions & 1 deletion types/k6/test/browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { browser, devices } from "k6/browser";
import { browser, chromium, devices } from "k6/browser";

const url = "http://example.com";
const selector = "a[href=\"http://example.com\"]";
Expand Down Expand Up @@ -121,6 +121,30 @@ async function test() {
// $ExpectType string
browser.version();

//
// Connect to an existing browser over CDP
//
// @ts-expect-error
chromium.connectOverCDP();
// $ExpectType Promise<ConnectedBrowser>
chromium.connectOverCDP("ws://localhost:9222/devtools/browser/1234");

const connectedBrowser = await chromium.connectOverCDP("ws://localhost:9222/devtools/browser/1234");

// $ExpectType Promise<void>
connectedBrowser.close();
// $ExpectType Promise<Page>
connectedBrowser.newPage();
// $ExpectType Promise<BrowserContext>
connectedBrowser.newContext({ viewport: { width: 1280, height: 720 } });
// $ExpectType boolean
connectedBrowser.isConnected();
// $ExpectType string
connectedBrowser.version();

// @ts-expect-error
browser.close();

//
// Create a browserContext
//
Expand Down