Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing browser request docs #1603

Merged
merged 24 commits into from
Jun 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,59 @@ weight: 11

# Request

The request that the browser performs can be retrieved from the [Response](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/response) when a navigation occurs.

{{< docs/shared source="k6" lookup="browser-module-wip.md" version="<K6_VERSION>" >}}

## Supported APIs

| Method | Playwright Relevant Distinctions |
| ---------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| <a href="https://playwright.dev/docs/api/class-request#request-all-headers" target="_blank" >request.allHeaders()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-frame" target="_blank" >request.frame()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-headers" target="_blank" >request.headers()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-headers-array" target="_blank" >request.headersArray()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-header-value" target="_blank" >request.headerValue(name)</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-is-navigation-request" target="_blank" >request.isNavigationRequest()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-method" target="_blank" >request.method()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-post-data" target="_blank" >request.postData()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-post-data-buffer" target="_blank" >request.postDataBuffer()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-redirected-from" target="_blank" >request.redirectedFrom()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-redirected-to" target="_blank" >request.redirectedTo()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-resource-type" target="_blank" >request.resourceType()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-response" target="_blank" >request.response()</a> | - |
| [request.size()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/size) | Unlike Playwright, this method returns an object containing the sizes of request headers and body. |
| <a href="https://playwright.dev/docs/api/class-request#request-timing" target="_blank" >request.timing()</a> | - |
| <a href="https://playwright.dev/docs/api/class-request#request-url" target="_blank" >request.url()</a> | - |
| Method | Playwright Relevant Distinctions |
| ------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
| [request.allHeaders()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/allheaders) {{< docs/bwipt id="965" >}} | Returns an object of headers associated to the request including headers added by the browser. |
| [request.frame()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/frame) | The [Frame](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/frame/) that initiated the request. |
| [request.headers()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/headers) | Returns an object of headers associated to the request. |
| [request.headersArray()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/headersarray) | An array with all the request HTTP headers. |
| [request.headerValue(name)](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/headervalue) | Returns the value of the header matching the name. The name is case insensitive. |
| [request.isNavigationRequest()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/isnavigationrequest) | Returns a boolean stating whether the request is for a navigation. |
| [request.method()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/method) | Request's method (GET, POST, etc.). |
| [request.postData()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/postdata) | Contains the request's post body, if any. |
| [request.postDataBuffer()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/postdatabuffer) | Request's post body in a binary form, if any. |
| [request.resourceType()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/resourcetype) | Contains the request's resource type as it was perceived by the rendering engine. |
| [request.response()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/response) | Returns the matching [Response](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/response) object. |
| [request.size()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/size) | Unlike Playwright, this method returns an object containing the sizes of request headers and body. |
| [request.timing()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/timing) | Returns resource timing information for given request. |
| [request.url()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/url) | URL of the request. |

### Example

{{< code >}}

```javascript
import { browser } from 'k6/experimental/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();

try {
const res = await page.goto('https://test.k6.io/');
const req = res.request();
} finally {
await page.close();
}
}
```

{{< /code >}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: 'allHeaders()'
description: 'Browser module: Request.allHeaders method'
---

# allHeaders()

{{% admonition type="caution" %}}

This method has a **known issue**. For details, refer to [#965](https://github.com/grafana/xk6-browser/issues/965).
ankur22 marked this conversation as resolved.
Show resolved Hide resolved

{{% /admonition %}}

An object of key value pairs made up of HTTP headers associated with the request and the ones that the browser adds (such as cookies). All header names are lower-case.

### Returns

| Type | Description |
| --------------------------------- | ------------------------------------------------------------------------ |
| `Promise<Record<string, string>>` | A promise that resolves to an object of key value pairs for each header. |

### Example

{{< code >}}

```javascript
import { browser } from 'k6/experimental/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();

try {
const res = await page.goto('https://test.k6.io/');
const req = res.request();

const allHeaders = await req.allHeaders();
console.log(`allHeaders: ${JSON.stringify(allHeaders)}`); // allHeaders: {"user-agent":"Mozilla/5.0...}
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
} finally {
await page.close();
}
}
```

{{< /code >}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: 'frame()'
description: 'Browser module: Request.frame method'
---

# frame()

The [Frame](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/frame/) that initiated the request.

### Returns

| Type | Description |
| ----------------------------------------------------------------------------------------------- | --------------------------------------- |
| [Frame](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/frame/) | The `Frame` that initiated the request. |

### Example

{{< code >}}

```javascript
import { browser } from 'k6/experimental/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();

try {
const res = await page.goto('https://test.k6.io/');
const req = res.request();

const frame = req.frame();
// ...
} finally {
await page.close();
}
}
```

{{< /code >}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: 'headers()'
description: 'Browser module: Request.headers method'
---

# headers()

An object of key value pairs made up of HTTP headers associated with the request.

### Returns

| Type | Description |
| ------------------------ | -------------------------------- |
| `Record<string, string>` | Key value pairs for each header. |

### Example

{{< code >}}

```javascript
import { browser } from 'k6/experimental/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();

try {
const res = await page.goto('https://test.k6.io/');
const req = res.request();

const headers = req.headers();
console.log(`headers: ${JSON.stringify(headers)}`); // headers: {"user-agent":"Mozilla/5.0...}
} finally {
await page.close();
}
}
```

{{< /code >}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: 'headersArray()'
description: 'Browser module: Request.headersArray method'
---

# headersArray()

An array with all the request HTTP headers. Unlike [request.allHeaders()](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/request/allheaders), header names are not lower-cased. Headers with multiple entries, such as `Set-Cookie`, appear in the array multiple times.
ankur22 marked this conversation as resolved.
Show resolved Hide resolved

### Returns

| Type | Description |
| ------------------------------------------------ | -------------------------------------------------------------------- |
| `Promise<Array<{ name: string; value: string }>` | A promise that resolves to an array of all the request HTTP headers. |

### Example

{{< code >}}

```javascript
import { browser } from 'k6/experimental/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();

try {
const res = await page.goto('https://test.k6.io/');
const req = res.request();

const headersArray = await req.headersArray();
console.log(`headersArray: ${JSON.stringify(headersArray)}`); // headersArray: [{"name":"Accept-Language","value"...}]
} finally {
await page.close();
}
}
```

{{< /code >}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: 'headerValue(name)'
description: 'Browser module: Request.headerValue method'
---

# headerValue(name)

Returns the value of the header matching the name. The name is case insensitive.

### Returns

| Type | Description |
| ------------------------- | --------------------------------------------------------------------------------------- |
| `Promise<string \| null>` | A promise that resolves to the value of the header matching the name, otherwise `null`. |

### Example

{{< code >}}

```javascript
import { browser } from 'k6/experimental/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();

try {
const res = await page.goto('https://test.k6.io/');
const req = res.request();

const headerValue = await req.headerValue('accept-language');
console.log(`headerValue: ${headerValue}`); // headerValue: en-US
} finally {
await page.close();
}
}
```

{{< /code >}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: 'isNavigationRequest()'
description: 'Browser module: Request.isNavigationRequest method'
---

# isNavigationRequest()

Returns a boolean stating whether the request is for a navigation.

### Returns

| Type | Description |
| ------- | -------------------------------------------------------- |
| boolean | Boolean stating whether the request is for a navigation. |

### Example

{{< code >}}

```javascript
import { browser } from 'k6/experimental/browser';

export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};

export default async function () {
const page = await browser.newPage();

try {
const res = await page.goto('https://test.k6.io/');
const req = res.request();

const isNavReq = req.isNavigationRequest();
console.log(`isNavReq: ${isNavReq}`); // isNavReq: true
} finally {
await page.close();
}
}
```

{{< /code >}}
Loading