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

Async BrowserContext #1328

Merged
merged 16 commits into from
May 13, 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
118 changes: 78 additions & 40 deletions browser/browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,89 @@ import (
func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolint:funlen,gocognit,cyclop
rt := vu.Runtime()
return mapping{
"addCookies": bc.AddCookies,
"addInitScript": func(script goja.Value) error {
if !gojaValueExists(script) {
return nil
}
"addCookies": func(cookies []*common.Cookie) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.AddCookies(cookies) //nolint:wrapcheck
})
},
"addInitScript": func(script goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
if !gojaValueExists(script) {
return nil, nil
}

source := ""
switch script.ExportType() {
case reflect.TypeOf(string("")):
source = script.String()
case reflect.TypeOf(goja.Object{}):
opts := script.ToObject(rt)
for _, k := range opts.Keys() {
if k == "content" {
source = opts.Get(k).String()
source := ""
switch script.ExportType() {
case reflect.TypeOf(string("")):
source = script.String()
case reflect.TypeOf(goja.Object{}):
opts := script.ToObject(rt)
for _, k := range opts.Keys() {
if k == "content" {
source = opts.Get(k).String()
}
}
default:
_, isCallable := goja.AssertFunction(script)
if !isCallable {
source = fmt.Sprintf("(%s);", script.ToString().String())
} else {
source = fmt.Sprintf("(%s)(...args);", script.ToString().String())
}
}
default:
_, isCallable := goja.AssertFunction(script)
if !isCallable {
source = fmt.Sprintf("(%s);", script.ToString().String())
} else {
source = fmt.Sprintf("(%s)(...args);", script.ToString().String())
}
}

return bc.AddInitScript(source) //nolint:wrapcheck
return nil, bc.AddInitScript(source) //nolint:wrapcheck
})
},
"browser": func() mapping {
// the browser is grabbed from VU.
return mapBrowser(vu)
},
"clearCookies": bc.ClearCookies,
"clearPermissions": bc.ClearPermissions,
"close": bc.Close,
"cookies": bc.Cookies,
"grantPermissions": func(permissions []string, opts goja.Value) error {
pOpts := common.NewGrantPermissionsOptions()
pOpts.Parse(vu.Context(), opts)
"clearCookies": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.ClearCookies() //nolint:wrapcheck
})
},
"clearPermissions": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.ClearPermissions() //nolint:wrapcheck
})
},
"close": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.Close() //nolint:wrapcheck
})
},
"cookies": func(urls ...string) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return bc.Cookies(urls...) //nolint:wrapcheck
})
},
"grantPermissions": func(permissions []string, opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
popts := common.NewGrantPermissionsOptions()
popts.Parse(vu.Context(), opts)

return bc.GrantPermissions(permissions, pOpts) //nolint:wrapcheck
return nil, bc.GrantPermissions(permissions, popts) //nolint:wrapcheck
})
},
"setDefaultNavigationTimeout": bc.SetDefaultNavigationTimeout,
"setDefaultTimeout": bc.SetDefaultTimeout,
"setGeolocation": bc.SetGeolocation,
"setHTTPCredentials": bc.SetHTTPCredentials, //nolint:staticcheck
"setOffline": bc.SetOffline,
"setGeolocation": func(geolocation goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.SetGeolocation(geolocation) //nolint:wrapcheck
})
},
"setHTTPCredentials": func(httpCredentials goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.SetHTTPCredentials(httpCredentials) //nolint:staticcheck,wrapcheck
})
},
"setOffline": func(offline bool) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, bc.SetOffline(offline) //nolint:wrapcheck
})
},
"waitForEvent": func(event string, optsOrPredicate goja.Value) (*goja.Promise, error) {
ctx := vu.Context()
popts := common.NewWaitForEventOptions(
Expand Down Expand Up @@ -124,12 +160,14 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin

return rt.ToValue(mpages).ToObject(rt)
},
"newPage": func() (mapping, error) {
page, err := bc.NewPage()
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapPage(vu, page), nil
"newPage": func() *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
page, err := bc.NewPage()
if err != nil {
return nil, err //nolint:wrapcheck
}
return mapPage(vu, page), nil
})
},
}
}
2 changes: 1 addition & 1 deletion examples/colorscheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async function() {
// valid values are "light", "dark" or "no-preference"
colorScheme: preferredColorScheme,
});
const page = context.newPage();
const page = await context.newPage();

try {
await page.goto(
Expand Down
40 changes: 20 additions & 20 deletions examples/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default async function () {

try {
// get cookies from the browser context
check(context.cookies().length, {
check((await context.cookies()).length, {
'initial number of cookies should be zero': n => n === 0,
});

Expand All @@ -32,7 +32,7 @@ export default async function () {
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 Expand Up @@ -62,7 +62,7 @@ export default async function () {
expires: dayBefore
}
]);
let cookies = context.cookies();
let cookies = await context.cookies();
check(cookies.length, {
'number of cookies should be 2': n => n === 2,
});
Expand All @@ -82,27 +82,27 @@ export default async function () {
});

// let's add more cookies to filter by urls.
context.addCookies([
await context.addCookies([
{
name: 'foo',
value: '42',
sameSite: 'Strict',
url: 'http://foo.com'
name: "foo",
value: "42",
sameSite: "Strict",
url: "http://foo.com",
},
{
name: 'bar',
value: '43',
sameSite: 'Lax',
url: 'https://bar.com'
name: "bar",
value: "43",
sameSite: "Lax",
url: "https://bar.com",
},
{
name: 'baz',
value: '44',
sameSite: 'Lax',
url: 'https://baz.com'
}
name: "baz",
value: "44",
sameSite: "Lax",
url: "https://baz.com",
},
]);
cookies = context.cookies('http://foo.com', 'https://baz.com');
cookies = await context.cookies("http://foo.com", "https://baz.com");
check(cookies.length, {
'number of filtered cookies should be 2': n => n === 2,
});
Expand All @@ -116,8 +116,8 @@ export default async function () {
});

// clear cookies
context.clearCookies();
cookies = context.cookies();
await context.clearCookies();
cookies = await context.cookies();
check(cookies.length, {
'number of cookies should be zero': n => n === 0,
});
Expand Down
2 changes: 1 addition & 1 deletion examples/device_emulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async function() {
// See https://github.com/grafana/k6/issues/2296
const options = Object.assign({ locale: 'es-ES' }, device);
const context = await browser.newContext(options);
const page = context.newPage();
const page = await context.newPage();

try {
await page.goto('https://k6.io/', { waitUntil: 'networkidle' });
Expand Down
2 changes: 1 addition & 1 deletion examples/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const options = {

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/', { waitUntil: 'networkidle' });
Expand Down
2 changes: 1 addition & 1 deletion examples/elementstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const options = {

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

// Inject page content
page.setContent(`
Expand Down
2 changes: 1 addition & 1 deletion examples/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const options = {

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/", { waitUntil: "load" });
Expand Down
4 changes: 2 additions & 2 deletions examples/fillform.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const options = {

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

try {
// Goto front page, find login link and click it
Expand All @@ -43,7 +43,7 @@ export default async function() {
});

// Check whether we receive cookies from the logged site.
check(context.cookies(), {
check(await context.cookies(), {
'session cookie is set': cookies => {
const sessionID = cookies.find(c => c.name == 'sid')
return typeof sessionID !== 'undefined'
Expand Down
2 changes: 1 addition & 1 deletion examples/getattribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const options = {

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

try {
await page.goto('https://googlechromelabs.github.io/dark-mode-toggle/demo/', {
Expand Down
4 changes: 2 additions & 2 deletions examples/grant_permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export default async function() {
permissions: ["camera", "microphone"],
});

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

try {
await page.goto('https://test.k6.io/');
page.screenshot({ path: `example-chromium.png` });
context.clearPermissions();
await context.clearPermissions();
} finally {
page.close();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const options = {

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

try {
const res = await page.goto('http://test.k6.io/', { waitUntil: 'load' });
Expand Down
2 changes: 1 addition & 1 deletion examples/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const options = {

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/flip_coin.php", {
Expand Down
2 changes: 1 addition & 1 deletion examples/locator_pom.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Bet {

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

const bet = new Bet(page);
try {
Expand Down
2 changes: 1 addition & 1 deletion examples/querying.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const options = {

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/');
Expand Down
2 changes: 1 addition & 1 deletion examples/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const options = {

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/');
Expand Down
6 changes: 3 additions & 3 deletions examples/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const options = {

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

try {
await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
Expand All @@ -51,7 +51,7 @@ export async function normal() {

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

try {
page.throttleNetwork(networkProfiles['Slow 3G']);
Expand All @@ -64,7 +64,7 @@ export async function networkThrottled() {

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

try {
page.throttleCPU({ rate: 4 });
Expand Down
4 changes: 2 additions & 2 deletions examples/waitForEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export default async function() {
} })

// Now we create two pages.
const page = context.newPage()
const page2 = context.newPage()
const page = await context.newPage();
const page2 = await context.newPage();

// We await for the page creation events to be processed and the predicate
// to pass.
Expand Down
Loading
Loading