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

Make Tap async #1268

Merged
merged 4 commits into from
Apr 23, 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
45 changes: 32 additions & 13 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
"press": lo.Press,
"type": lo.Type,
"hover": lo.Hover,
"tap": func(opts goja.Value) error {
"tap": func(opts goja.Value) (*goja.Promise, error) {
copts := common.NewFrameTapOptions(lo.DefaultTimeout())
if err := copts.Parse(vu.Context(), opts); err != nil {
return fmt.Errorf("parsing locator tap options: %w", err)
return nil, fmt.Errorf("parsing locator tap options: %w", err)
}
return lo.Tap(copts) //nolint:wrapcheck
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, lo.Tap(copts) //nolint:wrapcheck
}), nil
},
"dispatchEvent": func(typ string, eventInit, opts goja.Value) error {
popts := common.NewFrameDispatchEventOptions(lo.DefaultTimeout())
Expand Down Expand Up @@ -304,12 +306,14 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping {
"selectOption": eh.SelectOption,
"selectText": eh.SelectText,
"setInputFiles": eh.SetInputFiles,
"tap": func(opts goja.Value) error {
"tap": func(opts goja.Value) (*goja.Promise, error) {
popts := common.NewElementHandleTapOptions(eh.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return fmt.Errorf("parsing element tap options: %w", err)
return nil, fmt.Errorf("parsing element tap options: %w", err)
}
return eh.Tap(popts) //nolint:wrapcheck
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, eh.Tap(popts) //nolint:wrapcheck
}), nil
},
"textContent": eh.TextContent,
"type": eh.Type,
Expand Down Expand Up @@ -462,12 +466,14 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping {
"selectOption": f.SelectOption,
"setContent": f.SetContent,
"setInputFiles": f.SetInputFiles,
"tap": func(selector string, opts goja.Value) error {
"tap": func(selector string, opts goja.Value) (*goja.Promise, error) {
popts := common.NewFrameTapOptions(f.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return fmt.Errorf("parsing frame tap options: %w", err)
return nil, fmt.Errorf("parsing frame tap options: %w", err)
}
return f.Tap(selector, popts) //nolint:wrapcheck
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, f.Tap(selector, popts) //nolint:wrapcheck
}), nil
},
"textContent": f.TextContent,
"title": f.Title,
Expand Down Expand Up @@ -727,18 +733,20 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
"setExtraHTTPHeaders": p.SetExtraHTTPHeaders,
"setInputFiles": p.SetInputFiles,
"setViewportSize": p.SetViewportSize,
"tap": func(selector string, opts goja.Value) error {
"tap": func(selector string, opts goja.Value) (*goja.Promise, error) {
popts := common.NewFrameTapOptions(p.Timeout())
if err := popts.Parse(vu.Context(), opts); err != nil {
return fmt.Errorf("parsing page tap options: %w", err)
return nil, fmt.Errorf("parsing page tap options: %w", err)
}
return p.Tap(selector, popts) //nolint:wrapcheck
return k6ext.Promise(vu.Context(), func() (any, error) {
return nil, p.Tap(selector, popts) //nolint:wrapcheck
}), nil
},
"textContent": p.TextContent,
"throttleCPU": p.ThrottleCPU,
"throttleNetwork": p.ThrottleNetwork,
"title": p.Title,
"touchscreen": rt.ToValue(p.GetTouchscreen()).ToObject(rt),
"touchscreen": mapTouchscreen(vu, p.GetTouchscreen()),
"type": p.Type,
"uncheck": p.Uncheck,
"unroute": p.Unroute,
Expand Down Expand Up @@ -822,6 +830,17 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
return maps
}

// mapTouchscreen to the JS module.
func mapTouchscreen(vu moduleVU, ts *common.Touchscreen) mapping {
return mapping{
"tap": func(x float64, y float64) *goja.Promise {
return k6ext.Promise(vu.Context(), func() (result any, reason error) {
return nil, ts.Tap(x, y) //nolint:wrapcheck
})
},
}
}

// mapWorker to the JS module.
func mapWorker(vu moduleVU, w *common.Worker) mapping {
return mapping{
Expand Down
21 changes: 12 additions & 9 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ func TestMappings(t *testing.T) {
return mapConsoleMessage(moduleVU{VU: vu}, &common.ConsoleMessage{})
},
},
"mapTouchscreen": {
apiInterface: (*touchscreenAPI)(nil),
mapp: func() mapping {
return mapTouchscreen(moduleVU{VU: vu}, &common.Touchscreen{})
},
},
} {
tt := tt
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -341,7 +347,7 @@ type pageAPI interface {
SetExtraHTTPHeaders(headers map[string]string)
SetInputFiles(selector string, files goja.Value, opts goja.Value)
SetViewportSize(viewportSize goja.Value)
Tap(selector string, opts goja.Value) error
Tap(selector string, opts goja.Value) (*goja.Promise, error)
TextContent(selector string, opts goja.Value) string
ThrottleCPU(common.CPUProfile) error
ThrottleNetwork(common.NetworkProfile) error
Expand Down Expand Up @@ -413,7 +419,7 @@ type frameAPI interface {
SelectOption(selector string, values goja.Value, opts goja.Value) []string
SetContent(html string, opts goja.Value)
SetInputFiles(selector string, files goja.Value, opts goja.Value)
Tap(selector string, opts goja.Value) error
Tap(selector string, opts goja.Value) (*goja.Promise, error)
TextContent(selector string, opts goja.Value) string
Title() string
Type(selector string, text string, opts goja.Value)
Expand Down Expand Up @@ -458,7 +464,7 @@ type elementHandleAPI interface {
SelectOption(values goja.Value, opts goja.Value) []string
SelectText(opts goja.Value)
SetInputFiles(files goja.Value, opts goja.Value)
Tap(opts goja.Value) error
Tap(opts goja.Value) (*goja.Promise, error)
TextContent() string
Type(text string, opts goja.Value)
Uncheck(opts goja.Value)
Expand Down Expand Up @@ -533,7 +539,7 @@ type locatorAPI interface {
Press(key string, opts goja.Value)
Type(text string, opts goja.Value)
Hover(opts goja.Value)
Tap(opts goja.Value) error
Tap(opts goja.Value) (*goja.Promise, error)
DispatchEvent(typ string, eventInit, opts goja.Value)
WaitFor(opts goja.Value)
}
Expand All @@ -551,11 +557,8 @@ type keyboardAPI interface { //nolint: unused
}

// touchscreenAPI is the interface of a touchscreen.
// TODO: map this to page.GetTouchscreen(). Currently, the common.TouchscreenAPI type
// mapping is not tested using this interface. We use the concrete type
// without testing its exported methods.
type touchscreenAPI interface { //nolint: unused
Tap(x float64, y float64) error
type touchscreenAPI interface {
Tap(x float64, y float64) *goja.Promise
}

// mouseAPI is the interface of a mouse input device.
Expand Down
2 changes: 1 addition & 1 deletion examples/touchscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async function () {
// Obtain ElementHandle for news link and navigate to it
// by tapping in the 'a' element's bounding box
const newsLinkBox = page.$('a[href="/news.php"]').boundingBox();
page.touchscreen.tap(newsLinkBox.x + newsLinkBox.width / 2, newsLinkBox.y);
await page.touchscreen.tap(newsLinkBox.x + newsLinkBox.width / 2, newsLinkBox.y);

// Wait until the navigation is done before closing the page.
// Otherwise, there will be a race condition between the page closing
Expand Down
Loading