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 browser.newContext #1310

Merged
merged 2 commits into from
May 8, 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
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
2 changes: 1 addition & 1 deletion examples/colorscheme.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 preferredColorScheme = 'dark';

const context = browser.newContext({
const context = await browser.newContext({
// valid values are "light", "dark" or "no-preference"
colorScheme: preferredColorScheme,
});
Expand Down
2 changes: 1 addition & 1 deletion examples/device_emulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/dispatch.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 = browser.newContext();
const context = await browser.newContext();
const page = context.newPage();

try {
Expand Down
4 changes: 2 additions & 2 deletions examples/elementstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/evaluate.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 = browser.newContext();
const context = await browser.newContext();
const page = context.newPage();

try {
Expand Down
2 changes: 1 addition & 1 deletion examples/fillform.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 = browser.newContext();
const context = await browser.newContext();
const page = context.newPage();

try {
Expand Down
2 changes: 1 addition & 1 deletion examples/getattribute.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 = browser.newContext();
const context = await browser.newContext();
const page = context.newPage();

try {
Expand Down
2 changes: 1 addition & 1 deletion examples/grant_permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
});

Expand Down
2 changes: 1 addition & 1 deletion examples/hosts.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 = browser.newContext();
const context = await browser.newContext();
const page = context.newPage();

try {
Expand Down
2 changes: 1 addition & 1 deletion examples/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/locator_pom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion examples/querying.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 = browser.newContext();
const context = await browser.newContext();
const page = context.newPage();

try {
Expand Down
2 changes: 1 addition & 1 deletion examples/screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions examples/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/waitForEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/waitforfunction.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 = browser.newContext();
const context = await browser.newContext();
const page = context.newPage();

try {
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
Loading