From 6fb481b6f08e5f620b0336d7b75e11bbd6d03749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 8 May 2024 13:00:29 +0300 Subject: [PATCH 1/2] Async keyboard --- browser/keyboard_mapping.go | 39 ++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/browser/keyboard_mapping.go b/browser/keyboard_mapping.go index 5dc5868f0..e2a14d77d 100644 --- a/browser/keyboard_mapping.go +++ b/browser/keyboard_mapping.go @@ -1,13 +1,38 @@ package browser -import "github.com/grafana/xk6-browser/common" +import ( + "github.com/dop251/goja" -func mapKeyboard(_ moduleVU, kb *common.Keyboard) mapping { + "github.com/grafana/xk6-browser/common" + "github.com/grafana/xk6-browser/k6ext" +) + +func mapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { return mapping{ - "up": kb.Up, - "down": kb.Down, - "press": kb.Press, - "type": kb.Type, - "insertText": kb.InsertText, + "down": func(key string) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, kb.Down(key) //nolint:wrapcheck + }) + }, + "up": func(key string) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, kb.Up(key) //nolint:wrapcheck + }) + }, + "press": func(key string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, kb.Press(key, opts) //nolint:wrapcheck + }) + }, + "type": func(text string, opts goja.Value) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, kb.Type(text, opts) //nolint:wrapcheck + }) + }, + "insertText": func(text string) *goja.Promise { + return k6ext.Promise(vu.Context(), func() (any, error) { + return nil, kb.InsertText(text) //nolint:wrapcheck + }) + }, } } From 92dd073f8af55c4cfc52b2f89cbab517e9e326c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 8 May 2024 13:01:45 +0300 Subject: [PATCH 2/2] Update keyboard JS examples for async Also fixes a race condition by adding waitForNavigation. --- examples/keyboard.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/keyboard.js b/examples/keyboard.js index 37a5787a1..ff369da9e 100644 --- a/examples/keyboard.js +++ b/examples/keyboard.js @@ -17,16 +17,17 @@ export default async function () { const page = await browser.newPage(); await page.goto('https://test.k6.io/my_messages.php', { waitUntil: 'networkidle' }); - + const userInput = page.locator('input[name="login"]'); await userInput.click(); - page.keyboard.type('admin'); - + await page.keyboard.type("admin"); + const pwdInput = page.locator('input[name="password"]'); await pwdInput.click(); - page.keyboard.type('123'); + await page.keyboard.type("123"); + + await page.keyboard.press('Enter'); // submit + await page.waitForNavigation(); - page.keyboard.press('Enter'); // submit - - await page.close(); + page.close(); }