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.newPage #1312

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
28 changes: 15 additions & 13 deletions browser/browser_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,23 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop
}
return b.Version(), nil
},
"newPage": func(opts goja.Value) (mapping, error) {
b, err := vu.browser()
if err != nil {
return nil, err
}
page, err := b.NewPage(opts)
if err != nil {
return nil, err //nolint:wrapcheck
}
"newPage": func(opts goja.Value) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
b, err := vu.browser()
if err != nil {
return nil, err
}
page, err := b.NewPage(opts)
if err != nil {
return nil, err //nolint:wrapcheck
}

if err := initBrowserContext(b.Context(), vu.testRunID); err != nil {
return nil, err
}
if err := initBrowserContext(b.Context(), vu.testRunID); err != nil {
return nil, err
}

return mapPage(vu, page), nil
return mapPage(vu, page), nil
})
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/cookies.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 page = browser.newPage();
const page = await browser.newPage();
const context = page.context();

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

await page.goto('https://test.k6.io/my_messages.php', { waitUntil: 'networkidle' });

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

await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });

Expand Down
4 changes: 2 additions & 2 deletions examples/multiple-scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const options = {
}

export async function messages() {
const page = browser.newPage();
const page = await browser.newPage();

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

export async function news() {
const page = browser.newPage();
const page = await browser.newPage();

try {
await page.goto('https://test.k6.io/news.php', { waitUntil: 'networkidle' });
Expand Down
2 changes: 1 addition & 1 deletion examples/pageon.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 page = browser.newPage();
const page = await browser.newPage();

try {
await page.goto('https://test.k6.io/');
Expand Down
2 changes: 1 addition & 1 deletion examples/shadowdom.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 page = browser.newPage();
const page = await browser.newPage();
page.setContent("<html><head><style></style></head><body>hello!</body></html>")
await page.evaluate(() => {
const shadowRoot = document.createElement('div');
Expand Down
2 changes: 1 addition & 1 deletion examples/touchscreen.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 page = browser.newPage();
const page = await browser.newPage();

await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });

Expand Down
40 changes: 22 additions & 18 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,28 +657,31 @@ func TestK6Object(t *testing.T) {

// First test with browser.newPage
got, err := vu.TestRT.RunOnEventLoop(`
const p = browser.newPage();
p.goto("about:blank");
const o = p.evaluate(() => window.k6);
JSON.stringify(o);
(async function() {
const p = await browser.newPage();
p.goto("about:blank");
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
const o = p.evaluate(() => window.k6);
return JSON.stringify(o);
})();
`)
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())

// Now test with browser.newContext
got, err = vu.TestRT.RunOnEventLoop(`
const test = async function() {
(async function() {
await browser.closeContext();
const c = await browser.newContext();
const p2 = c.newPage();
const p2 = await c.newPage();
p2.goto("about:blank");
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
const o2 = p2.evaluate(() => window.k6);
return JSON.stringify(o2);
}
test();
})();
`)
require.NoError(t, err)
p, ok := got.Export().(*goja.Promise)
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 All @@ -704,17 +707,18 @@ func TestNewTab(t *testing.T) {
mux.Handle(path, http.StripPrefix(path, fs))

// Start the iteration
_, rt, _, cleanUp := startIteration(t, env.ConstLookup(env.K6TestRunID, "12345"))
vu, _, _, cleanUp := startIteration(t, env.ConstLookup(env.K6TestRunID, "12345"))
defer cleanUp()

// Run the test script
_, err := rt.RunString(fmt.Sprintf(`
const p = browser.newPage()
p.goto("%s/%s/ping.html")

const p2 = browser.context().newPage()
p2.goto("%s/%s/ping.html")
`, s.URL, testBrowserStaticDir, s.URL, testBrowserStaticDir))
_, err := vu.TestRT.RunOnEventLoop(fmt.Sprintf(`
(async function() {
const p = await browser.newPage()
p.goto("%s/%s/ping.html")
inancgumus marked this conversation as resolved.
Show resolved Hide resolved

const p2 = browser.context().newPage()
p2.goto("%s/%s/ping.html")
})()`, s.URL, testBrowserStaticDir, s.URL, testBrowserStaticDir))
require.NoError(t, err)
}

Expand Down
8 changes: 5 additions & 3 deletions tests/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ func TestBrowserCrashErr(t *testing.T) {

rt := vu.Runtime()
require.NoError(t, rt.Set("browser", jsMod.Browser))
_, err := rt.RunString(`
const p = browser.newPage();
p.close();
_, err := vu.TestRT.RunOnEventLoop(`
(async function() {
const p = await browser.newPage();
p.close();
})();
`)
assert.ErrorContains(t, err, "launching browser: Invalid devtools server port")
}
Expand Down
8 changes: 5 additions & 3 deletions tests/browser_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ func TestBrowserTypeLaunchToConnect(t *testing.T) {

rt := vu.Runtime()
require.NoError(t, rt.Set("browser", jsMod.Browser))
_, err := rt.RunString(`
const p = browser.newPage();
p.close();
_, err := vu.TestRT.RunOnEventLoop(`
(async function() {
const p = await browser.newPage();
p.close();
})();
`)
require.NoError(t, err)

Expand Down
Loading
Loading