diff --git a/browser/page_mapping.go b/browser/page_mapping.go index 9c2ff633c..28be0e234 100644 --- a/browser/page_mapping.go +++ b/browser/page_mapping.go @@ -17,8 +17,16 @@ import ( func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop rt := vu.Runtime() maps := mapping{ - "bringToFront": p.BringToFront, - "check": p.Check, + "bringToFront": func() *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.BringToFront() //nolint:wrapcheck + }) + }, + "check": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.Check(selector, opts) //nolint:wrapcheck + }) + }, "click": func(selector string, opts goja.Value) (*goja.Promise, error) { popts, err := parseFrameClickOptions(vu.Context(), opts, p.Timeout()) if err != nil { @@ -30,37 +38,68 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return nil, err //nolint:wrapcheck }), nil }, - "close": func(opts goja.Value) error { - vu.taskQueueRegistry.close(p.TargetID()) - - return p.Close(opts) //nolint:wrapcheck + "close": func(opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + vu.taskQueueRegistry.close(p.TargetID()) + return nil, p.Close(opts) //nolint:wrapcheck + }) + }, + "content": func() *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.Content() //nolint:wrapcheck + }) }, - "content": p.Content, "context": func() mapping { return mapBrowserContext(vu, p.Context()) }, - "dblclick": p.Dblclick, - "dispatchEvent": func(selector, typ string, eventInit, opts goja.Value) error { + "dblclick": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.Dblclick(selector, opts) //nolint:wrapcheck + }) + }, + "dispatchEvent": func(selector, typ string, eventInit, opts goja.Value) (*goja.Promise, error) { popts := common.NewFrameDispatchEventOptions(p.Timeout()) if err := popts.Parse(vu.Context(), opts); err != nil { - return fmt.Errorf("parsing page dispatch event options: %w", err) + return nil, fmt.Errorf("parsing page dispatch event options: %w", err) } - return p.DispatchEvent(selector, typ, exportArg(eventInit), popts) //nolint:wrapcheck + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.DispatchEvent(selector, typ, exportArg(eventInit), popts) //nolint:wrapcheck + }), nil }, - "emulateMedia": p.EmulateMedia, - "emulateVisionDeficiency": p.EmulateVisionDeficiency, - "evaluate": func(pageFunction goja.Value, gargs ...goja.Value) (any, error) { - return p.Evaluate(pageFunction.String(), exportArgs(gargs)...) //nolint:wrapcheck + "emulateMedia": func(opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.EmulateMedia(opts) //nolint:wrapcheck + }) }, - "evaluateHandle": func(pageFunc goja.Value, gargs ...goja.Value) (mapping, error) { - jsh, err := p.EvaluateHandle(pageFunc.String(), exportArgs(gargs)...) - if err != nil { - return nil, err //nolint:wrapcheck - } - return mapJSHandle(vu, jsh), nil + "emulateVisionDeficiency": func(typ string) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.EmulateVisionDeficiency(typ) //nolint:wrapcheck + }) + }, + "evaluate": func(pageFunction goja.Value, gargs ...goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.Evaluate(pageFunction.String(), exportArgs(gargs)...) //nolint:wrapcheck + }) + }, + "evaluateHandle": func(pageFunc goja.Value, gargs ...goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + jsh, err := p.EvaluateHandle(pageFunc.String(), exportArgs(gargs)...) + if err != nil { + return nil, err //nolint:wrapcheck + } + return mapJSHandle(vu, jsh), nil + }) + }, + "fill": func(selector string, value string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.Fill(selector, value, opts) //nolint:wrapcheck + }) + }, + "focus": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.Focus(selector, opts) //nolint:wrapcheck + }) }, - "fill": p.Fill, - "focus": p.Focus, "frames": func() *goja.Object { var ( mfrs []mapping @@ -71,7 +110,11 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop } return rt.ToValue(mfrs).ToObject(rt) }, - "getAttribute": p.GetAttribute, + "getAttribute": func(selector string, name string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.GetAttribute(selector, name, opts) //nolint:wrapcheck + }) + }, "goto": func(url string, opts goja.Value) (*goja.Promise, error) { gopts := common.NewFrameGotoOptions( p.Referrer(), @@ -89,18 +132,58 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return mapResponse(vu, resp), nil }), nil }, - "hover": p.Hover, - "innerHTML": p.InnerHTML, - "innerText": p.InnerText, - "inputValue": p.InputValue, - "isChecked": p.IsChecked, - "isClosed": p.IsClosed, - "isDisabled": p.IsDisabled, - "isEditable": p.IsEditable, - "isEnabled": p.IsEnabled, - "isHidden": p.IsHidden, - "isVisible": p.IsVisible, - "keyboard": mapKeyboard(vu, p.GetKeyboard()), + "hover": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.Hover(selector, opts) //nolint:wrapcheck + }) + }, + "innerHTML": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.InnerHTML(selector, opts) //nolint:wrapcheck + }) + }, + "innerText": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.InnerText(selector, opts) //nolint:wrapcheck + }) + }, + "inputValue": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.InputValue(selector, opts) //nolint:wrapcheck + }) + }, + "isChecked": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.IsChecked(selector, opts) //nolint:wrapcheck + }) + }, + "isClosed": p.IsClosed, + "isDisabled": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.IsDisabled(selector, opts) //nolint:wrapcheck + }) + }, + "isEditable": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.IsEditable(selector, opts) //nolint:wrapcheck + }) + }, + "isEnabled": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.IsEnabled(selector, opts) //nolint:wrapcheck + }) + }, + "isHidden": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.IsHidden(selector, opts) //nolint:wrapcheck + }) + }, + "isVisible": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.IsVisible(selector, opts) //nolint:wrapcheck + }) + }, + "keyboard": mapKeyboard(vu, p.GetKeyboard()), "locator": func(selector string, opts goja.Value) *goja.Object { ml := mapLocator(vu, p.Locator(selector, opts)) return rt.ToValue(ml).ToObject(rt) @@ -129,40 +212,72 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return p.On(event, runInTaskQueue) //nolint:wrapcheck }, - "opener": p.Opener, - "press": p.Press, - "reload": func(opts goja.Value) (*goja.Object, error) { - resp, err := p.Reload(opts) - if err != nil { - return nil, err //nolint:wrapcheck - } + "opener": func() *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.Opener(), nil + }) + }, + "press": func(selector string, key string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.Press(selector, key, opts) //nolint:wrapcheck + }) + }, + "reload": func(opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + resp, err := p.Reload(opts) + if err != nil { + return nil, err //nolint:wrapcheck + } - r := mapResponse(vu, resp) + r := mapResponse(vu, resp) - return rt.ToValue(r).ToObject(rt), nil + return rt.ToValue(r).ToObject(rt), nil + }) }, - "screenshot": func(opts goja.Value) (*goja.ArrayBuffer, error) { + "screenshot": func(opts goja.Value) (*goja.Promise, error) { popts := common.NewPageScreenshotOptions() if err := popts.Parse(vu.Context(), opts); err != nil { return nil, fmt.Errorf("parsing page screenshot options: %w", err) } - bb, err := p.Screenshot(popts, vu.filePersister) - if err != nil { - return nil, err //nolint:wrapcheck - } + return k6ext.Promise(vu.Context(), func() (any, error) { + bb, err := p.Screenshot(popts, vu.filePersister) + if err != nil { + return nil, err //nolint:wrapcheck + } - ab := rt.NewArrayBuffer(bb) + ab := rt.NewArrayBuffer(bb) - return &ab, nil + return &ab, nil + }), nil + }, + "selectOption": func(selector string, values goja.Value, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.SelectOption(selector, values, opts) //nolint:wrapcheck + }) + }, + "setContent": func(html string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.SetContent(html, opts) //nolint:wrapcheck + }) }, - "selectOption": p.SelectOption, - "setContent": p.SetContent, "setDefaultNavigationTimeout": p.SetDefaultNavigationTimeout, "setDefaultTimeout": p.SetDefaultTimeout, - "setExtraHTTPHeaders": p.SetExtraHTTPHeaders, - "setInputFiles": p.SetInputFiles, - "setViewportSize": p.SetViewportSize, + "setExtraHTTPHeaders": func(headers map[string]string) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.SetExtraHTTPHeaders(headers) //nolint:wrapcheck + }) + }, + "setInputFiles": func(selector string, files goja.Value, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.SetInputFiles(selector, files, opts) //nolint:wrapcheck + }) + }, + "setViewportSize": func(viewportSize goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.SetViewportSize(viewportSize) //nolint:wrapcheck + }) + }, "tap": func(selector string, opts goja.Value) (*goja.Promise, error) { popts := common.NewFrameTapOptions(p.Timeout()) if err := popts.Parse(vu.Context(), opts); err != nil { @@ -172,15 +287,39 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return nil, p.Tap(selector, popts) //nolint:wrapcheck }), nil }, - "textContent": p.TextContent, - "throttleCPU": p.ThrottleCPU, - "throttleNetwork": p.ThrottleNetwork, - "title": p.Title, - "touchscreen": mapTouchscreen(vu, p.GetTouchscreen()), - "type": p.Type, - "uncheck": p.Uncheck, - "url": p.URL, - "viewportSize": p.ViewportSize, + "textContent": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.TextContent(selector, opts) //nolint:wrapcheck + }) + }, + "throttleCPU": func(cpuProfile common.CPUProfile) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.ThrottleCPU(cpuProfile) //nolint:wrapcheck + }) + }, + "throttleNetwork": func(networkProfile common.NetworkProfile) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.ThrottleNetwork(networkProfile) //nolint:wrapcheck + }) + }, + "title": func() *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return p.Title() //nolint:wrapcheck + }) + }, + "touchscreen": mapTouchscreen(vu, p.GetTouchscreen()), + "type": func(selector string, text string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.Type(selector, text, opts) //nolint:wrapcheck + }) + }, + "uncheck": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.Uncheck(selector, opts) //nolint:wrapcheck + }) + }, + "url": p.URL, + "viewportSize": p.ViewportSize, "waitForFunction": func(pageFunc, opts goja.Value, args ...goja.Value) (*goja.Promise, error) { js, popts, pargs, err := parseWaitForFunctionArgs( vu.Context(), p.Timeout(), pageFunc, opts, args..., @@ -193,7 +332,11 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return p.WaitForFunction(js, popts, pargs...) //nolint:wrapcheck }), nil }, - "waitForLoadState": p.WaitForLoadState, + "waitForLoadState": func(state string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, p.WaitForLoadState(state, opts) //nolint:wrapcheck + }) + }, "waitForNavigation": func(opts goja.Value) (*goja.Promise, error) { popts := common.NewFrameWaitForNavigationOptions(p.Timeout()) if err := popts.Parse(vu.Context(), opts); err != nil { @@ -208,14 +351,21 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return mapResponse(vu, resp), nil }), nil }, - "waitForSelector": func(selector string, opts goja.Value) (mapping, error) { - eh, err := p.WaitForSelector(selector, opts) - if err != nil { - return nil, err //nolint:wrapcheck - } - return mapElementHandle(vu, eh), nil + "waitForSelector": func(selector string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + eh, err := p.WaitForSelector(selector, opts) + if err != nil { + return nil, err //nolint:wrapcheck + } + return mapElementHandle(vu, eh), nil + }) + }, + "waitForTimeout": func(timeout int64) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + p.WaitForTimeout(timeout) + return nil, nil + }) }, - "waitForTimeout": p.WaitForTimeout, "workers": func() *goja.Object { var mws []mapping for _, w := range p.Workers() { @@ -225,32 +375,36 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return rt.ToValue(mws).ToObject(rt) }, } - maps["$"] = func(selector string) (mapping, error) { - eh, err := p.Query(selector) - if err != nil { - return nil, err //nolint:wrapcheck - } - // ElementHandle can be null when the selector does not match any elements. - // We do not want to map nil elementHandles since the expectation is a - // null result in the test script for this case. - if eh == nil { - return nil, nil //nolint:nilnil - } - ehm := mapElementHandle(vu, eh) + maps["$"] = func(selector string) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + eh, err := p.Query(selector) + if err != nil { + return nil, err //nolint:wrapcheck + } + // ElementHandle can be null when the selector does not match any elements. + // We do not want to map nil elementHandles since the expectation is a + // null result in the test script for this case. + if eh == nil { + return nil, nil + } + ehm := mapElementHandle(vu, eh) - return ehm, nil + return ehm, nil + }) } - maps["$$"] = func(selector string) ([]mapping, error) { - ehs, err := p.QueryAll(selector) - if err != nil { - return nil, err //nolint:wrapcheck - } - var mehs []mapping - for _, eh := range ehs { - ehm := mapElementHandle(vu, eh) - mehs = append(mehs, ehm) - } - return mehs, nil + maps["$$"] = func(selector string) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + ehs, err := p.QueryAll(selector) + if err != nil { + return nil, err //nolint:wrapcheck + } + var mehs []mapping + for _, eh := range ehs { + ehm := mapElementHandle(vu, eh) + mehs = append(mehs, ehm) + } + return mehs, nil + }) } return maps diff --git a/examples/colorscheme.js b/examples/colorscheme.js index 6fdce9996..101f8a159 100644 --- a/examples/colorscheme.js +++ b/examples/colorscheme.js @@ -31,7 +31,7 @@ export default async function() { 'https://googlechromelabs.github.io/dark-mode-toggle/demo/', { waitUntil: 'load' }, ) - const colorScheme = page.evaluate(() => { + const colorScheme = await page.evaluate(() => { return { isDarkColorScheme: window.matchMedia('(prefers-color-scheme: dark)').matches }; @@ -40,6 +40,6 @@ export default async function() { 'isDarkColorScheme': cs => cs.isDarkColorScheme }); } finally { - page.close(); + await page.close(); } } diff --git a/examples/cookies.js b/examples/cookies.js index 45076fab8..1f31ccef3 100644 --- a/examples/cookies.js +++ b/examples/cookies.js @@ -122,6 +122,6 @@ export default async function () { 'number of cookies should be zero': n => n === 0, }); } finally { - page.close(); + await page.close(); } } diff --git a/examples/device_emulation.js b/examples/device_emulation.js index 94c3b3602..4a730e1f3 100644 --- a/examples/device_emulation.js +++ b/examples/device_emulation.js @@ -28,7 +28,7 @@ export default async function() { try { await page.goto('https://k6.io/', { waitUntil: 'networkidle' }); - const dimensions = page.evaluate(() => { + const dimensions = await page.evaluate(() => { return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight, @@ -46,6 +46,6 @@ export default async function() { sleep(10); } } finally { - page.close(); + await page.close(); } } diff --git a/examples/dispatch.js b/examples/dispatch.js index 8c4defec9..01f0935ad 100644 --- a/examples/dispatch.js +++ b/examples/dispatch.js @@ -31,6 +31,6 @@ export default async function() { const ok = await h3.textContent() == "Contact us"; check(ok, { "header": ok }); } finally { - page.close(); + await page.close(); } } diff --git a/examples/elementstate.js b/examples/elementstate.js index 3932b2316..2b2e10635 100644 --- a/examples/elementstate.js +++ b/examples/elementstate.js @@ -22,7 +22,7 @@ export default async function() { const page = await context.newPage(); // Inject page content - page.setContent(` + await page.setContent(`
Shadow DOM
'; document.body.appendChild(shadowRoot); }); + const shadowEl = page.locator("#find"); const ok = await shadowEl.innerText() === "Shadow DOM"; check(shadowEl, { "shadow element exists": (e) => e !== null, "shadow element text is correct": () => ok, }); - page.close(); + + await page.close(); } diff --git a/examples/throttle.js b/examples/throttle.js index 6fd36c39c..3c661ae8a 100644 --- a/examples/throttle.js +++ b/examples/throttle.js @@ -45,7 +45,7 @@ export async function normal() { try { await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' }); } finally { - page.close(); + await page.close(); } } @@ -54,11 +54,11 @@ export async function networkThrottled() { const page = await context.newPage(); try { - page.throttleNetwork(networkProfiles['Slow 3G']); + await page.throttleNetwork(networkProfiles["Slow 3G"]); await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' }); } finally { - page.close(); + await page.close(); } } @@ -67,10 +67,10 @@ export async function cpuThrottled() { const page = await context.newPage(); try { - page.throttleCPU({ rate: 4 }); + await page.throttleCPU({ rate: 4 }); await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' }); } finally { - page.close(); + await page.close(); } } diff --git a/examples/touchscreen.js b/examples/touchscreen.js index 937843e58..61c4b4574 100644 --- a/examples/touchscreen.js +++ b/examples/touchscreen.js @@ -30,5 +30,5 @@ export default async function () { page.touchscreen.tap(newsLinkBox.x + newsLinkBox.width / 2, newsLinkBox.y), ]); - page.close(); + await page.close(); } diff --git a/examples/waitForEvent.js b/examples/waitForEvent.js index 0d5180465..d905debc6 100644 --- a/examples/waitForEvent.js +++ b/examples/waitForEvent.js @@ -34,6 +34,6 @@ export default async function() { await promise console.log('predicate passed') - page.close() - page2.close() + await page.close() + await page2.close(); }; diff --git a/examples/waitforfunction.js b/examples/waitforfunction.js index de57e4b86..d6c774cdb 100644 --- a/examples/waitforfunction.js +++ b/examples/waitforfunction.js @@ -22,7 +22,7 @@ export default async function() { const page = await context.newPage(); try { - page.evaluate(() => { + await page.evaluate(() => { setTimeout(() => { const el = document.createElement('h1'); el.innerHTML = 'Hello'; @@ -36,6 +36,6 @@ export default async function() { }); check(ok, { 'waitForFunction successfully resolved': ok.innerHTML() == 'Hello' }); } finally { - page.close(); + await page.close(); } } diff --git a/tests/browser_context_test.go b/tests/browser_context_test.go index 696bbcd5c..13cd437f3 100644 --- a/tests/browser_context_test.go +++ b/tests/browser_context_test.go @@ -659,7 +659,7 @@ func TestK6Object(t *testing.T) { got := vu.RunPromise(t, ` const p = await browser.newPage(); await p.goto("about:blank"); - const o = p.evaluate(() => window.k6); + const o = await p.evaluate(() => window.k6); return JSON.stringify(o); `) assert.Equal(t, tt.want, got.Result().String()) @@ -670,7 +670,7 @@ func TestK6Object(t *testing.T) { const c = await browser.newContext(); const p2 = await c.newPage(); await p2.goto("about:blank"); - const o2 = p2.evaluate(() => window.k6); + const o2 = await p2.evaluate(() => window.k6); return JSON.stringify(o2); `) assert.Equal(t, tt.want, got.Result().String()) diff --git a/tests/browser_test.go b/tests/browser_test.go index ac372d037..1cc3e9831 100644 --- a/tests/browser_test.go +++ b/tests/browser_test.go @@ -233,7 +233,7 @@ func TestBrowserCrashErr(t *testing.T) { vu.SetVar(t, "browser", jsMod.Browser) _, err := vu.RunAsync(t, ` const p = await browser.newPage(); - p.close(); + await p.close(); `) assert.ErrorContains(t, err, "launching browser: Invalid devtools server port") } diff --git a/tests/browser_type_test.go b/tests/browser_type_test.go index 1a7080fc9..b803c7256 100644 --- a/tests/browser_type_test.go +++ b/tests/browser_type_test.go @@ -52,7 +52,7 @@ func TestBrowserTypeLaunchToConnect(t *testing.T) { vu.SetVar(t, "browser", jsMod.Browser) _, err := vu.RunAsync(t, ` const p = await browser.newPage(); - p.close(); + await p.close(); `) require.NoError(t, err) diff --git a/tests/launch_options_slowmo_test.go b/tests/launch_options_slowmo_test.go index 0866468c6..889b2205a 100644 --- a/tests/launch_options_slowmo_test.go +++ b/tests/launch_options_slowmo_test.go @@ -128,7 +128,8 @@ func TestBrowserOptionsSlowMo(t *testing.T) { t.Parallel() tb := newTestBrowser(t, withFileServer()) testPageSlowMoImpl(t, tb, func(_ *testBrowser, p *common.Page) { - p.Reload(nil) + _, err := p.Reload(nil) + require.NoError(t, err) }) }) t.Run("setContent", func(t *testing.T) { diff --git a/tests/lifecycle_wait_test.go b/tests/lifecycle_wait_test.go index be762732a..2431ff340 100644 --- a/tests/lifecycle_wait_test.go +++ b/tests/lifecycle_wait_test.go @@ -416,7 +416,8 @@ func TestLifecycleReload(t *testing.T) { WaitUntil: tt.waitUntil, Timeout: 30 * time.Second, }) - p.Reload(opts) + _, err = p.Reload(opts) + require.NoError(t, err) result, err = p.TextContent("#pingRequestText", nil) if err != nil { diff --git a/tests/page_test.go b/tests/page_test.go index 557768059..188e56efd 100644 --- a/tests/page_test.go +++ b/tests/page_test.go @@ -229,13 +229,13 @@ func TestPageEvaluateMapping(t *testing.T) { vu.SetVar(t, "p", &goja.Object{}) got := vu.RunPromise(t, ` p = await browser.newPage() - return p.evaluate(%s) + return await p.evaluate(%s) `, tt.script) assert.Equal(t, vu.ToGojaValue(tt.want), got.Result()) // Test script as string input got = vu.RunPromise(t, - `return p.evaluate("%s")`, + `return await p.evaluate("%s")`, tt.script, ) assert.Equal(t, vu.ToGojaValue(tt.want), got.Result()) @@ -275,13 +275,13 @@ func TestPageEvaluateMappingError(t *testing.T) { vu.SetVar(t, "p", &goja.Object{}) _, err := vu.RunAsync(t, ` p = await browser.newPage() - p.evaluate(%s) + await p.evaluate(%s) `, tt.script) assert.ErrorContains(t, err, tt.wantErr) // Test script as string input _, err = vu.RunAsync(t, ` - p.evaluate("%s") + await p.evaluate("%s") `, tt.script) assert.ErrorContains(t, err, tt.wantErr) }) @@ -666,7 +666,8 @@ func TestPageSetExtraHTTPHeaders(t *testing.T) { headers := map[string]string{ "Some-Header": "Some-Value", } - p.SetExtraHTTPHeaders(headers) + err := p.SetExtraHTTPHeaders(headers) + require.NoError(t, err) opts := &common.FrameGotoOptions{ Timeout: common.DefaultTimeout, @@ -722,7 +723,7 @@ func TestPageWaitForFunction(t *testing.T) { t.Run("ok_func_raf_default_arg", func(t *testing.T) { t.Parallel() - vu, rt, log, cleanUp := startIteration(t) + vu, _, log, cleanUp := startIteration(t) defer cleanUp() _, err := vu.RunOnEventLoop(t, `fn = arg => { @@ -735,12 +736,9 @@ func TestPageWaitForFunction(t *testing.T) { require.NoError(t, err) assert.Contains(t, *log, "ok: null") - argEval, err := vu.RunOnEventLoop(t, `page.evaluate(() => window._arg);`) - require.NoError(t, err) - - var gotArg string - _ = rt.ExportTo(argEval, &gotArg) - assert.Equal(t, "raf_arg", gotArg) + p := vu.RunPromise(t, `return await page.evaluate(() => window._arg);`) + require.Equal(t, p.State(), goja.PromiseStateFulfilled) + assert.Equal(t, "raf_arg", p.Result().String()) }) t.Run("ok_func_raf_default_args", func(t *testing.T) { @@ -763,11 +761,10 @@ func TestPageWaitForFunction(t *testing.T) { require.NoError(t, err) assert.Contains(t, *log, "ok: null") - argEval, err := vu.RunOnEventLoop(t, `page.evaluate(() => window._args);`) - require.NoError(t, err) - + p := vu.RunPromise(t, `return await page.evaluate(() => window._args);`) + require.Equal(t, p.State(), goja.PromiseStateFulfilled) var gotArgs []int - _ = rt.ExportTo(argEval, &gotArgs) + _ = rt.ExportTo(p.Result(), &gotArgs) assert.Equal(t, args, gotArgs) }) @@ -803,7 +800,7 @@ func TestPageWaitForFunction(t *testing.T) { vu.SetVar(t, "page", &goja.Object{}) _, err := vu.RunAsync(t, ` page = await browser.newPage(); - page.evaluate(() => { + await page.evaluate(() => { setTimeout(() => { const el = document.createElement('h1'); el.innerHTML = 'Hello'; @@ -836,7 +833,7 @@ func TestPageWaitForFunction(t *testing.T) { fn = () => document.querySelector('h1') !== null page = await browser.newPage(); - page.evaluate(() => { + await page.evaluate(() => { console.log('calling setTimeout...'); setTimeout(() => { console.log('creating element...'); @@ -1735,7 +1732,7 @@ func TestShadowDOMAndDocumentFragment(t *testing.T) { got := vu.RunPromise(t, ` const p = await browser.newPage() - p.goto("%s/%s/shadow_and_doc_frag.html") + await p.goto("%s/%s/shadow_and_doc_frag.html") const s = p.locator('%s') await s.waitFor({ @@ -1794,13 +1791,15 @@ func TestPageTargetBlank(t *testing.T) { p2, ok := obj.(*common.Page) require.True(t, ok, "return from WaitForEvent is not a Page") - p2.WaitForLoadState(common.LifecycleEventLoad.String(), nil) + err = p2.WaitForLoadState(common.LifecycleEventLoad.String(), nil) + require.NoError(t, err) // Now there should be 2 pages. pp = p.Context().Pages() assert.Equal(t, 2, len(pp)) // Make sure the new page contains the correct page. - got := p2.InnerHTML("h1", nil) + got, err := p2.InnerHTML("h1", nil) + require.NoError(t, err) assert.Equal(t, "you clicked!", got) }