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 Keyboard #1314

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
39 changes: 32 additions & 7 deletions browser/keyboard_mapping.go
Original file line number Diff line number Diff line change
@@ -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
})
},
}
}
15 changes: 8 additions & 7 deletions examples/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
}
Loading