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 1 commit
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 @@ -35,15 +35,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 @@ -37,9 +37,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 @@ -29,13 +29,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 @@ -29,9 +29,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 @@ -29,9 +29,9 @@ export const options = {

export default function () {
const context = await browser.newContext();
context.newPage();
await context.newPage();
inancgumus marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
Original file line number Diff line number Diff line change
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 @@ -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 @@ -42,10 +42,10 @@ export const options = {

export default function () {
const context = await browser.newContext();
context.newPage();
await context.newPage();
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const options = {

export default function () {
const context = await browser.newContext();
context.setGeolocation({ latitude: 59.95, longitude: 30.31667 });
await context.setGeolocation({ latitude: 59.95, longitude: 30.31667 });
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export const options = {
export default async function () {
const context = await browser.newContext();

context.setOffline(true);
await context.setOffline(true);

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

try {
// Will not be able to load the page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async function () {
// Call waitForEvent with a predicate which will return true once at least
// one page has been created.
let counter = 0;
const promise = context.waitForEvent('page', {
const promise = await context.waitForEvent('page', {
predicate: (page) => {
if (++counter >= 1) {
return true;
Expand All @@ -58,7 +58,7 @@ export default async function () {
});

// Now we create a page.
const page = context.newPage();
const page = await context.newPage();

// Wait for the predicate to pass.
await promise;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export default function () {

const page1 = await browser.newPage(); // implicitly creates a new browserContext
const context = browser.context(); // underlying live browserContext associated with browser
const page2 = context.newPage(); // shares the browserContext with page1
const page2 = await context.newPage(); // shares the browserContext with page1

page1.close();
page2.close();
context.close();
await context.close();
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default async function () {
},
deviceScaleFactor: 3,
});
const page = context.newPage();
const page = await context.newPage();

try {
await page.goto('https://test.k6.io/');
Expand Down