Skip to content

Commit

Permalink
Async browser.newContext
Browse files Browse the repository at this point in the history
About the TestK6Object update:
- To prevent race conditions, the JavaScript codes in the tests run in
  the event loop.
  • Loading branch information
inancgumus committed May 7, 2024
1 parent 991c699 commit b1771a7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
32 changes: 17 additions & 15 deletions browser/browser_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
34 changes: 20 additions & 14 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -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())
})
}
}
Expand Down

0 comments on commit b1771a7

Please sign in to comment.