Skip to content

Commit

Permalink
Expose VU to the mapping layer
Browse files Browse the repository at this point in the history
Exposing VU is preliminary for using promises in the mapping layer.

Updates: #683
  • Loading branch information
inancgumus committed Jan 11, 2023
1 parent 1f16b6c commit d9c46d1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
87 changes: 47 additions & 40 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ func wildcards() map[string]string {
// methods.
// See issue #661 for more details.
func mapBrowserToGoja(vu k6modules.VU) *goja.Object {
rt := vu.Runtime()

var (
rt = vu.Runtime()
obj = rt.NewObject()
browserType = chromium.NewBrowserType(vu)
)
for k, v := range mapBrowserType(rt, browserType) {
for k, v := range mapBrowserType(vu, browserType) {
err := obj.Set(k, rt.ToValue(v))
if err != nil {
k6common.Throw(rt, fmt.Errorf("mapping: %w", err))
Expand All @@ -56,12 +55,13 @@ func mapBrowserToGoja(vu k6modules.VU) *goja.Object {
}

// mapRequest to the JS module.
func mapRequest(rt *goja.Runtime, r api.Request) mapping {
func mapRequest(vu k6modules.VU, r api.Request) mapping {
rt := vu.Runtime()
maps := mapping{
"allHeaders": r.AllHeaders,
"failure": r.Failure,
"frame": func() *goja.Object {
mf := mapFrame(rt, r.Frame())
mf := mapFrame(vu, r.Frame())
return rt.ToValue(mf).ToObject(rt)
},
"headerValue": r.HeaderValue,
Expand All @@ -73,16 +73,16 @@ func mapRequest(rt *goja.Runtime, r api.Request) mapping {
"postDataBuffer": r.PostDataBuffer,
"postDataJSON": r.PostDataJSON,
"redirectedFrom": func() *goja.Object {
mr := mapRequest(rt, r.RedirectedFrom())
mr := mapRequest(vu, r.RedirectedFrom())
return rt.ToValue(mr).ToObject(rt)
},
"redirectedTo": func() *goja.Object {
mr := mapRequest(rt, r.RedirectedTo())
mr := mapRequest(vu, r.RedirectedTo())
return rt.ToValue(mr).ToObject(rt)
},
"resourceType": r.ResourceType,
"response": func() *goja.Object {
mr := mapResponse(rt, r.Response())
mr := mapResponse(vu, r.Response())
return rt.ToValue(mr).ToObject(rt)
},
"size": r.Size,
Expand All @@ -94,13 +94,14 @@ func mapRequest(rt *goja.Runtime, r api.Request) mapping {
}

// mapResponse to the JS module.
func mapResponse(rt *goja.Runtime, r api.Response) mapping {
func mapResponse(vu k6modules.VU, r api.Response) mapping {
rt := vu.Runtime()
maps := mapping{
"allHeaders": r.AllHeaders,
"body": r.Body,
"finished": r.Finished,
"frame": func() *goja.Object {
mf := mapFrame(rt, r.Frame())
mf := mapFrame(vu, r.Frame())
return rt.ToValue(mf).ToObject(rt)
},
"headerValue": r.HeaderValue,
Expand All @@ -110,7 +111,7 @@ func mapResponse(rt *goja.Runtime, r api.Response) mapping {
"jSON": r.JSON,
"ok": r.Ok,
"request": func() *goja.Object {
mr := mapRequest(rt, r.Request())
mr := mapRequest(vu, r.Request())
return rt.ToValue(mr).ToObject(rt)
},
"securityDetails": r.SecurityDetails,
Expand All @@ -127,10 +128,11 @@ func mapResponse(rt *goja.Runtime, r api.Response) mapping {
// mapElementHandle to the JS module.
//
//nolint:funlen
func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
func mapElementHandle(vu k6modules.VU, eh api.ElementHandle) mapping {
rt := vu.Runtime()
maps := mapping{
"asElement": func() *goja.Object {
m := mapElementHandle(rt, eh.AsElement())
m := mapElementHandle(vu, eh.AsElement())
return rt.ToValue(m).ToObject(rt)
},
"dispose": eh.Dispose,
Expand All @@ -145,7 +147,7 @@ func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
"click": eh.Click,
"contentFrame": func() *goja.Object {
f := eh.ContentFrame()
mf := mapFrame(rt, f)
mf := mapFrame(vu, f)
return rt.ToValue(mf).ToObject(rt)
},
"dblclick": eh.Dblclick,
Expand All @@ -165,7 +167,7 @@ func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
"isVisible": eh.IsVisible,
"ownerFrame": func() *goja.Object {
f := eh.OwnerFrame()
mf := mapFrame(rt, f)
mf := mapFrame(vu, f)
return rt.ToValue(mf).ToObject(rt)
},
"press": eh.Press,
Expand All @@ -181,13 +183,13 @@ func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
"waitForElementState": eh.WaitForElementState,
"waitForSelector": func(selector string, opts goja.Value) *goja.Object {
eh := eh.WaitForSelector(selector, opts)
ehm := mapElementHandle(rt, eh)
ehm := mapElementHandle(vu, eh)
return rt.ToValue(ehm).ToObject(rt)
},
}
maps["$"] = func(selector string) *goja.Object {
eh := eh.Query(selector)
ehm := mapElementHandle(rt, eh)
ehm := mapElementHandle(vu, eh)
return rt.ToValue(ehm).ToObject(rt)
}
maps["$$"] = func(selector string) *goja.Object {
Expand All @@ -196,7 +198,7 @@ func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
ehs = eh.QueryAll(selector)
)
for _, eh := range ehs {
ehm := mapElementHandle(rt, eh)
ehm := mapElementHandle(vu, eh)
mehs = append(mehs, ehm)
}
return rt.ToValue(mehs).ToObject(rt)
Expand All @@ -208,7 +210,8 @@ func mapElementHandle(rt *goja.Runtime, eh api.ElementHandle) mapping {
// mapFrame to the JS module.
//
//nolint:funlen
func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
func mapFrame(vu k6modules.VU, f api.Frame) mapping {
rt := vu.Runtime()
maps := mapping{
"addScriptTag": f.AddScriptTag,
"addStyleTag": f.AddStyleTag,
Expand All @@ -219,7 +222,7 @@ func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
cfs = f.ChildFrames()
)
for _, fr := range cfs {
mcfs = append(mcfs, mapFrame(rt, fr))
mcfs = append(mcfs, mapFrame(vu, fr))
}
return rt.ToValue(mcfs).ToObject(rt)
},
Expand All @@ -232,7 +235,7 @@ func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
"fill": f.Fill,
"focus": f.Focus,
"frameElement": func() *goja.Object {
eh := mapElementHandle(rt, f.FrameElement())
eh := mapElementHandle(vu, f.FrameElement())
return rt.ToValue(eh).ToObject(rt)
},
"getAttribute": f.GetAttribute,
Expand All @@ -253,11 +256,11 @@ func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
"locator": f.Locator,
"name": f.Name,
"page": func() *goja.Object {
mp := mapPage(rt, f.Page())
mp := mapPage(vu, f.Page())
return rt.ToValue(mp).ToObject(rt)
},
"parentFrame": func() *goja.Object {
mf := mapFrame(rt, f.ParentFrame())
mf := mapFrame(vu, f.ParentFrame())
return rt.ToValue(mf).ToObject(rt)
},
"press": f.Press,
Expand All @@ -275,14 +278,14 @@ func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
"waitForNavigation": f.WaitForNavigation,
"waitForSelector": func(selector string, opts goja.Value) *goja.Object {
eh := f.WaitForSelector(selector, opts)
ehm := mapElementHandle(rt, eh)
ehm := mapElementHandle(vu, eh)
return rt.ToValue(ehm).ToObject(rt)
},
"waitForTimeout": f.WaitForTimeout,
}
maps["$"] = func(selector string) *goja.Object {
eh := f.Query(selector)
ehm := mapElementHandle(rt, eh)
ehm := mapElementHandle(vu, eh)
return rt.ToValue(ehm).ToObject(rt)
}
maps["$$"] = func(selector string) *goja.Object {
Expand All @@ -291,7 +294,7 @@ func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
ehs = f.QueryAll(selector)
)
for _, eh := range ehs {
ehm := mapElementHandle(rt, eh)
ehm := mapElementHandle(vu, eh)
mehs = append(mehs, ehm)
}
return rt.ToValue(mehs).ToObject(rt)
Expand All @@ -303,7 +306,8 @@ func mapFrame(rt *goja.Runtime, f api.Frame) mapping {
// mapPage to the JS module.
//
//nolint:funlen
func mapPage(rt *goja.Runtime, p api.Page) mapping {
func mapPage(vu k6modules.VU, p api.Page) mapping {
rt := vu.Runtime()
maps := mapping{
"addInitScript": p.AddInitScript,
"addScriptTag": p.AddScriptTag,
Expand Down Expand Up @@ -332,7 +336,7 @@ func mapPage(rt *goja.Runtime, p api.Page) mapping {
frs = p.Frames()
)
for _, fr := range frs {
mfrs = append(mfrs, mapFrame(rt, fr))
mfrs = append(mfrs, mapFrame(vu, fr))
}
return rt.ToValue(mfrs).ToObject(rt)
},
Expand All @@ -353,15 +357,15 @@ func mapPage(rt *goja.Runtime, p api.Page) mapping {
"isVisible": p.IsVisible,
"locator": p.Locator,
"mainFrame": func() *goja.Object {
mf := mapFrame(rt, p.MainFrame())
mf := mapFrame(vu, p.MainFrame())
return rt.ToValue(mf).ToObject(rt)
},
"opener": p.Opener,
"pause": p.Pause,
"pdf": p.Pdf,
"press": p.Press,
"reload": func(opts goja.Value) *goja.Object {
r := mapResponse(rt, p.Reload(opts))
r := mapResponse(vu, p.Reload(opts))
return rt.ToValue(r).ToObject(rt)
},
"route": p.Route,
Expand Down Expand Up @@ -394,7 +398,7 @@ func mapPage(rt *goja.Runtime, p api.Page) mapping {
}
maps["$"] = func(selector string) *goja.Object {
eh := p.Query(selector)
ehm := mapElementHandle(rt, eh)
ehm := mapElementHandle(vu, eh)
return rt.ToValue(ehm).ToObject(rt)
}
maps["$$"] = func(selector string) *goja.Object {
Expand All @@ -403,7 +407,7 @@ func mapPage(rt *goja.Runtime, p api.Page) mapping {
ehs = p.QueryAll(selector)
)
for _, eh := range ehs {
ehm := mapElementHandle(rt, eh)
ehm := mapElementHandle(vu, eh)
mehs = append(mehs, ehm)
}
return rt.ToValue(mehs).ToObject(rt)
Expand All @@ -413,7 +417,8 @@ func mapPage(rt *goja.Runtime, p api.Page) mapping {
}

// mapBrowserContext to the JS module.
func mapBrowserContext(rt *goja.Runtime, bc api.BrowserContext) mapping {
func mapBrowserContext(vu k6modules.VU, bc api.BrowserContext) mapping {
rt := vu.Runtime()
return mapping{
"addCookies": bc.AddCookies,
"addInitScript": bc.AddInitScript,
Expand Down Expand Up @@ -445,22 +450,23 @@ func mapBrowserContext(rt *goja.Runtime, bc api.BrowserContext) mapping {
if page == nil {
continue
}
m := mapPage(rt, page)
m := mapPage(vu, page)
mpages = append(mpages, m)
}

return rt.ToValue(mpages).ToObject(rt)
},
"newPage": func() *goja.Object {
page := bc.NewPage()
m := mapPage(rt, page)
m := mapPage(vu, page)
return rt.ToValue(m).ToObject(rt)
},
}
}

// mapBrowser to the JS module.
func mapBrowser(rt *goja.Runtime, b api.Browser) mapping {
func mapBrowser(vu k6modules.VU, b api.Browser) mapping {
rt := vu.Runtime()
return mapping{
"close": b.Close,
"contexts": b.Contexts,
Expand All @@ -470,26 +476,27 @@ func mapBrowser(rt *goja.Runtime, b api.Browser) mapping {
"version": b.Version,
"newContext": func(opts goja.Value) *goja.Object {
bctx := b.NewContext(opts)
m := mapBrowserContext(rt, bctx)
m := mapBrowserContext(vu, bctx)
return rt.ToValue(m).ToObject(rt)
},
"newPage": func(opts goja.Value) *goja.Object {
page := b.NewPage(opts)
m := mapPage(rt, page)
m := mapPage(vu, page)
return rt.ToValue(m).ToObject(rt)
},
}
}

// mapBrowserType to the JS module.
func mapBrowserType(rt *goja.Runtime, bt api.BrowserType) mapping {
func mapBrowserType(vu k6modules.VU, bt api.BrowserType) mapping {
rt := vu.Runtime()
return mapping{
"connect": bt.Connect,
"executablePath": bt.ExecutablePath,
"launchPersistentContext": bt.LaunchPersistentContext,
"name": bt.Name,
"launch": func(opts goja.Value) *goja.Object {
m := mapBrowser(rt, bt.Launch(opts))
m := mapBrowser(vu, bt.Launch(opts))
return rt.ToValue(m).ToObject(rt)
},
}
Expand Down
17 changes: 8 additions & 9 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func TestMappings(t *testing.T) {
Registry: k6metrics.NewRegistry(),
},
}
rt = vu.Runtime()
wildcards = wildcards()
)

Expand Down Expand Up @@ -78,49 +77,49 @@ func TestMappings(t *testing.T) {
"browserType": {
apiInterface: (*api.BrowserType)(nil),
mapp: func() mapping {
return mapBrowserType(rt, &chromium.BrowserType{})
return mapBrowserType(vu, &chromium.BrowserType{})
},
},
"browser": {
apiInterface: (*api.Browser)(nil),
mapp: func() mapping {
return mapBrowser(rt, &chromium.Browser{})
return mapBrowser(vu, &chromium.Browser{})
},
},
"browserContext": {
apiInterface: (*api.BrowserContext)(nil),
mapp: func() mapping {
return mapBrowserContext(rt, &common.BrowserContext{})
return mapBrowserContext(vu, &common.BrowserContext{})
},
},
"page": {
apiInterface: (*api.Page)(nil),
mapp: func() mapping {
return mapPage(rt, &common.Page{})
return mapPage(vu, &common.Page{})
},
},
"elementHandle": {
apiInterface: (*api.ElementHandle)(nil),
mapp: func() mapping {
return mapElementHandle(rt, &common.ElementHandle{})
return mapElementHandle(vu, &common.ElementHandle{})
},
},
"frame": {
apiInterface: (*api.Frame)(nil),
mapp: func() mapping {
return mapFrame(rt, &common.Frame{})
return mapFrame(vu, &common.Frame{})
},
},
"mapRequest": {
apiInterface: (*api.Request)(nil),
mapp: func() mapping {
return mapRequest(rt, &common.Request{})
return mapRequest(vu, &common.Request{})
},
},
"mapResponse": {
apiInterface: (*api.Response)(nil),
mapp: func() mapping {
return mapResponse(rt, &common.Response{})
return mapResponse(vu, &common.Response{})
},
},
} {
Expand Down

0 comments on commit d9c46d1

Please sign in to comment.