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

Update to Async BrowserContext #1590

Merged
merged 18 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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 @@ -251,7 +251,7 @@ export const options = {
export default async function () {
const iphoneX = devices['iPhone X'];
const context = await browser.newContext(iphoneX);
const page = context.newPage();
const page = await context.newPage();

try {
await page.goto('https://test.k6.io/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ description: 'Clears context cookies.'

# addCookies()

Adds a list of [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) into the [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie). All pages within this [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) will have these [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) set.
Adds a list of [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) into the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/). All pages within this [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/) will have these [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the BrowserContext to browser context change. I think it makes it easier for the user to see it used in one way, so that they can easily understand the relationship between all the components.

Why did you make the change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was easier to read in a sentence. We've been using this pattern in other places (before this PR), too. I've created an issue to keep everything consistent: #1596.


{{% admonition type="note" %}}

If a [cookie](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie)'s `url` property is not provided, both `domain` and `path` properties must be specified.

{{% /admonition %}}

### Returns

| Type | Description |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Promise<void>` | A Promise that fulfills when the [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) have been added to the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/). |

### Example

{{< code >}}
Expand All @@ -35,15 +41,15 @@ export const options = {

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

try {
const unixTimeSinceEpoch = Math.round(new Date() / 1000);
const day = 60 * 60 * 24;
const dayAfter = unixTimeSinceEpoch + day;
const dayBefore = unixTimeSinceEpoch - day;

context.addCookies([
await context.addCookies([
// this cookie expires at the end of the session
{
name: 'testcookie',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ description: 'Adds an init script.'

Adds a script which will be evaluated in one of the following scenarios:

- Whenever a [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) is created in the [browserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext) or is navigated.
- Whenever a child [frame](<[page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/frame/)>) is attached or navigated in any [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) in the [browserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). In this case, the script is evaluated in the context of the newly attached [frame](<[page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/frame/)>).
- Whenever a [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) is created in the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext) or is navigated.
- Whenever a child [frame](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/frame/) is attached or navigated in any [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) in the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). In this case, the script is evaluated in the context of the newly attached [frame](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/frame/).

The script is evaluated after the document is created but before any of its scripts are run. This is useful to amend the JavaScript environment, for example, to override `Math.random`.

### Returns

| Type | Description |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Promise<void>` | A Promise that fulfills when the script has been added to the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). |

### Example

{{< code >}}
Expand All @@ -37,9 +43,9 @@ export default async function () {
const browserContext = await browser.newContext();

// This will execute and override the existing implementation of Math.random.
browserContext.addInitScript('Math.random = function(){return 0}');
await browserContext.addInitScript('Math.random = function(){return 0}');

const page = browserContext.newPage();
const page = await browserContext.newPage();

// In this example we are going to set the content manually, so we first
// navigate to about:blank which will execute the init script, before setting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ description: 'Clears context cookies.'

# clearCookies()

Clears the `BrowserContext`'s cookies.
Clears the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext)'s cookies.

### Returns

| Type | Description |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Promise<void>` | A Promise that fulfills when the cookies have been cleared from the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). |

### Example

Expand All @@ -29,13 +35,15 @@ export const options = {

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

await page.goto('https://httpbin.org/cookies/set?testcookie=testcookievalue');
console.log(context.cookies().length); // prints: 1
let cookies = await context.cookies();
console.log(cookies.length); // prints: 1

context.clearCookies();
console.log(context.cookies().length); // prints: 0
await context.clearCookies();
cookies = await context.cookies();
console.log(cookies.length); // prints: 0
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ description: 'Clears all permission overrides for the BrowserContext.'

# clearPermissions()

Clears all permission overrides for the `BrowserContext`.
Clears all permission overrides for the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext).

### Returns

| Type | Description |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Promise<void>` | A Promise that fulfills when the permissions have been cleared from the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). |

### Example

Expand All @@ -29,9 +35,9 @@ export const options = {

export default function () {
const context = await browser.newContext();
context.grantPermissions(['clipboard-read']);
await context.grantPermissions(['clipboard-read']);
// do stuff ...
context.clearPermissions();
await context.clearPermissions();
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ description: 'Close the BrowserContext and all its pages.'

# close()

Close the `BrowserContext` and all its [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/)s. The `BrowserContext` is unusable after this call and a new one must be created. This is typically called to cleanup before ending the test.
Close the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext) and all its [pages](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/). The [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext) is unusable after this call and a new one must be created. This is typically called to cleanup before ending the test.

### Returns

| Type | Description |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Promise<void>` | A Promise that fulfills when the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext) and all its [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/)s have been closed. |

### Example

Expand All @@ -29,9 +35,9 @@ export const options = {

export default function () {
const context = await browser.newContext();
context.newPage();
await context.newPage();

context.close();
await context.close();
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ description: 'Retrieves context cookies.'

# cookies([urls])

Returns a list of [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) from the [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext) filtered by the provided `urls`. If no `urls` are provided, all cookies are returned.
Returns a list of [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) from the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext) filtered by the provided `urls`. If no `urls` are provided, all cookies are returned.

| Parameter | Type | Description |
| --------- | ----- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| urls | array | A string array of URLs to filter the [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) in the [BrowserContext](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). |
| urls | array | A string array of URLs to filter the [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie) in the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). |

### Returns

| Type | Description |
| ----- | --------------------------------------------------------------------------------------------------------------------------- |
| array | A list of [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie). |
| Type | Description |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Promise<Array<Cookie>>` | A Promise that fulfills with an array of [cookies](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext/cookie). |
inancgumus marked this conversation as resolved.
Show resolved Hide resolved

{{% admonition type="note" %}}

Expand Down Expand Up @@ -45,26 +45,26 @@ export const options = {

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

try {
// get cookies from the browser context
let cookies = context.cookies();
let cookies = await context.cookies();
console.log('initial cookies length:', cookies.length); // prints 0

// let's add more cookies to filter by urls
context.addCookies([
await context.addCookies([
{ name: 'foo', value: 'foovalue', sameSite: 'Strict', url: 'http://foo.com' },
{ name: 'bar', value: 'barvalue', sameSite: 'Lax', url: 'https://bar.com' },
{ name: 'baz', value: 'bazvalue', sameSite: 'Lax', url: 'https://baz.com' },
]);

// get all cookies
cookies = context.cookies();
cookies = await context.cookies();
console.log('filtered cookies length:', cookies.length); // prints 3

// get cookies filtered by urls
cookies = context.cookies('http://foo.com', 'https://baz.com');
cookies = await context.cookies('http://foo.com', 'https://baz.com');
console.log('filtered cookies length:', cookies.length); // prints 2

// the first filtered cookie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: 'Grants specified permissions to the BrowserContext.'

# grantPermissions(permissions[, options])

Grants specified permissions to the `BrowserContext`. Only grants corresponding permissions to the given origin if specified.
Grants specified permissions to the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext). Only grants corresponding permissions to the given origin if specified.

<TableWithNestedRows>

Expand All @@ -17,6 +17,12 @@ Grants specified permissions to the `BrowserContext`. Only grants corresponding

</TableWithNestedRows>

### Returns

| Type | Description |
| --------------- | --------------------------------------------------------------- |
| `Promise<void>` | A Promise that fulfills when the permissions have been granted. |

### Example

{{< code >}}
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ description: 'Creates a new page inside this BrowserContext.'

# newPage()

Uses the `BrowserContext` to create a new [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) and returns it.
Uses the [browser context](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/browsercontext) to create and return a new [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/).

### Returns

| Type | Description |
| ------ | ----------------------------------------------------------------------------------------------------------- |
| object | A new [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) object. |
| Type | Description |
| --------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `Promise<Page>` | A Promise that fulfills with a new [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) object. |

### Example

Expand All @@ -22,7 +22,7 @@ import { browser } from 'k6/experimental/browser';

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

try {
await page.goto('https://test.k6.io/browser.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Returns all open [Page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/

### Returns

| Type | Description |
| ----- | --------------- |
| array | All open pages. |
| Type | Description |
| ------------- | ------------------------------------------------------------------------------------------------------------------ |
| `Array<Page>` | An array of [page](https://grafana.com/docs/k6/<K6_VERSION>/javascript-api/k6-experimental/browser/page/) objects. |

### Example

Expand All @@ -42,10 +42,10 @@ export const options = {

export default function () {
const context = await browser.newContext();
context.newPage();
await context.newPage();
const pages = context.pages();
console.log(pages.length); // 1
context.close();
await context.close();
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const options = {

export default async function () {
const context = await browser.newContext();
const page = context.newPage();
const page = await context.newPage();
context.setDefaultNavigationTimeout(1000); // 1s

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const options = {
export default async function () {
const context = await browser.newContext();
context.setDefaultTimeout(1000); // 1s
const page = context.newPage();
const page = await context.newPage();
await page.click('h2'); // times out
}
```
Expand Down
Loading