diff --git a/browser/browser_mapping.go b/browser/browser_mapping.go index f2cfc7640..f9cc307ad 100644 --- a/browser/browser_mapping.go +++ b/browser/browser_mapping.go @@ -6,11 +6,11 @@ import ( "github.com/dop251/goja" "github.com/grafana/xk6-browser/common" + "github.com/grafana/xk6-browser/k6ext" ) // mapBrowser to the JS module. func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop - rt := vu.Runtime() return mapping{ "context": func() (mapping, error) { b, err := vu.browser() @@ -33,23 +33,25 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop } return b.IsConnected(), nil }, - "newContext": func(opts goja.Value) (*goja.Object, error) { - b, err := vu.browser() - if err != nil { - return nil, err - } - bctx, err := b.NewContext(opts) - if err != nil { - return nil, err //nolint:wrapcheck - } + "newContext": func(opts goja.Value) (*goja.Promise, error) { + return k6ext.Promise(vu.Context(), func() (any, error) { + b, err := vu.browser() + if err != nil { + return nil, err + } + bctx, err := b.NewContext(opts) + if err != nil { + return nil, err //nolint:wrapcheck + } - if err := initBrowserContext(bctx, vu.testRunID); err != nil { - return nil, err - } + if err := initBrowserContext(bctx, vu.testRunID); err != nil { + return nil, err + } - m := mapBrowserContext(vu, bctx) + m := mapBrowserContext(vu, bctx) - return rt.ToValue(m).ToObject(rt), nil + return m, nil + }), nil }, "userAgent": func() (string, error) { b, err := vu.browser() diff --git a/examples/colorscheme.js b/examples/colorscheme.js index 58269a903..2a38bae5b 100644 --- a/examples/colorscheme.js +++ b/examples/colorscheme.js @@ -20,7 +20,7 @@ export const options = { export default async function() { const preferredColorScheme = 'dark'; - const context = browser.newContext({ + const context = await browser.newContext({ // valid values are "light", "dark" or "no-preference" colorScheme: preferredColorScheme, }); diff --git a/examples/device_emulation.js b/examples/device_emulation.js index 5759928ad..5352b8a2d 100644 --- a/examples/device_emulation.js +++ b/examples/device_emulation.js @@ -23,7 +23,7 @@ export default async function() { // Object.assign instead to merge browser context and device options. // See https://github.com/grafana/k6/issues/2296 const options = Object.assign({ locale: 'es-ES' }, device); - const context = browser.newContext(options); + const context = await browser.newContext(options); const page = context.newPage(); try { diff --git a/examples/dispatch.js b/examples/dispatch.js index 5414eae8c..04e74d6f5 100644 --- a/examples/dispatch.js +++ b/examples/dispatch.js @@ -18,7 +18,7 @@ export const options = { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/elementstate.js b/examples/elementstate.js index 57d4f1c87..b00f1fb84 100644 --- a/examples/elementstate.js +++ b/examples/elementstate.js @@ -17,8 +17,8 @@ export const options = { } } -export default function() { - const context = browser.newContext(); +export default async function() { + const context = await browser.newContext(); const page = context.newPage(); // Inject page content diff --git a/examples/evaluate.js b/examples/evaluate.js index ec0f0326c..a6ba0d2f6 100644 --- a/examples/evaluate.js +++ b/examples/evaluate.js @@ -18,7 +18,7 @@ export const options = { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/fillform.js b/examples/fillform.js index dfa868a70..8eb5aac18 100644 --- a/examples/fillform.js +++ b/examples/fillform.js @@ -18,7 +18,7 @@ export const options = { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/getattribute.js b/examples/getattribute.js index 6a2bca532..13d7b22a4 100644 --- a/examples/getattribute.js +++ b/examples/getattribute.js @@ -18,7 +18,7 @@ export const options = { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/grant_permission.js b/examples/grant_permission.js index 0df1a78db..fe16c5fba 100644 --- a/examples/grant_permission.js +++ b/examples/grant_permission.js @@ -19,7 +19,7 @@ export const options = { export default async function() { // grant camera and microphone permissions to the // new browser context. - const context = browser.newContext({ + const context = await browser.newContext({ permissions: ["camera", "microphone"], }); diff --git a/examples/hosts.js b/examples/hosts.js index 07a4c2607..44c32b640 100644 --- a/examples/hosts.js +++ b/examples/hosts.js @@ -19,7 +19,7 @@ export const options = { }; export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/locator.js b/examples/locator.js index a7b189809..87df33a28 100644 --- a/examples/locator.js +++ b/examples/locator.js @@ -17,7 +17,7 @@ export const options = { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/locator_pom.js b/examples/locator_pom.js index ca5ccc9b6..97050b0ff 100644 --- a/examples/locator_pom.js +++ b/examples/locator_pom.js @@ -57,7 +57,7 @@ export class Bet { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); const bet = new Bet(page); diff --git a/examples/querying.js b/examples/querying.js index 96d7a0d8a..70863c053 100644 --- a/examples/querying.js +++ b/examples/querying.js @@ -18,7 +18,7 @@ export const options = { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/screenshot.js b/examples/screenshot.js index fb8b59407..a506ac411 100644 --- a/examples/screenshot.js +++ b/examples/screenshot.js @@ -17,7 +17,7 @@ export const options = { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/throttle.js b/examples/throttle.js index 295589718..e26f44606 100644 --- a/examples/throttle.js +++ b/examples/throttle.js @@ -39,7 +39,7 @@ export const options = { } export async function normal() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { @@ -50,7 +50,7 @@ export async function normal() { } export async function networkThrottled() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { @@ -63,7 +63,7 @@ export async function networkThrottled() { } export async function cpuThrottled() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/examples/waitForEvent.js b/examples/waitForEvent.js index 8cd3d2d1a..15260a0d5 100644 --- a/examples/waitForEvent.js +++ b/examples/waitForEvent.js @@ -14,7 +14,7 @@ export const options = { } export default async function() { - const context = browser.newContext() + const context = await browser.newContext() // We want to wait for two page creations before carrying on. var counter = 0 diff --git a/examples/waitforfunction.js b/examples/waitforfunction.js index 506d522c6..631951835 100644 --- a/examples/waitforfunction.js +++ b/examples/waitforfunction.js @@ -18,7 +18,7 @@ export const options = { } export default async function() { - const context = browser.newContext(); + const context = await browser.newContext(); const page = context.newPage(); try { diff --git a/tests/browser_context_test.go b/tests/browser_context_test.go index bd60b9396..c8bbbee2a 100644 --- a/tests/browser_context_test.go +++ b/tests/browser_context_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/dop251/goja" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -651,30 +652,35 @@ func TestK6Object(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - _, rt, _, cleanUp := startIteration(t, env.ConstLookup(env.K6TestRunID, tt.testRunID)) + vu, _, _, cleanUp := startIteration(t, env.ConstLookup(env.K6TestRunID, tt.testRunID)) defer cleanUp() // First test with browser.newPage - got, err := rt.RunString(` - const p = browser.newPage() - p.goto("about:blank") - const o = p.evaluate(() => window.k6) - JSON.stringify(o) + got, err := vu.TestRT.RunOnEventLoop(` + const p = browser.newPage(); + p.goto("about:blank"); + const o = p.evaluate(() => window.k6); + JSON.stringify(o); `) require.NoError(t, err) assert.Equal(t, tt.want, got.String()) // Now test with browser.newContext - got, err = rt.RunString(` - browser.closeContext() - const c = browser.newContext() - const p2 = c.newPage() - p2.goto("about:blank") - const o2 = p2.evaluate(() => window.k6) - JSON.stringify(o2) + got, err = vu.TestRT.RunOnEventLoop(` + const test = async function() { + browser.closeContext(); + const c = await browser.newContext(); + const p2 = c.newPage(); + p2.goto("about:blank"); + const o2 = p2.evaluate(() => window.k6); + return JSON.stringify(o2); + } + test(); `) require.NoError(t, err) - assert.Equal(t, tt.want, got.String()) + p, ok := got.Export().(*goja.Promise) + require.Truef(t, ok, "got: %T, want *goja.Promise", got.Export()) + assert.Equal(t, tt.want, p.Result().String()) }) } }