Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit ef0cea2

Browse files
committed
Map page to Goja
1 parent b8d958a commit ef0cea2

File tree

1 file changed

+107
-4
lines changed

1 file changed

+107
-4
lines changed

browser/mapping.go

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,90 @@ func mapBrowserToGoja(vu k6modules.VU) *goja.Object {
4040
return obj
4141
}
4242

43+
// mapPage to the JS module.
44+
//
45+
//nolint:funlen
46+
func mapPage(rt *goja.Runtime, p api.Page) mapping {
47+
_ = rt
48+
maps := mapping{
49+
"addInitScript": p.AddInitScript,
50+
"addScriptTag": p.AddScriptTag,
51+
"addStyleTag": p.AddStyleTag,
52+
"bringToFront": p.BringToFront,
53+
"check": p.Check,
54+
"click": p.Click,
55+
"close": p.Close,
56+
"content": p.Content,
57+
"context": p.Context,
58+
"dblclick": p.Dblclick,
59+
"dispatchEvent": p.DispatchEvent,
60+
"dragAndDrop": p.DragAndDrop,
61+
"emulateMedia": p.EmulateMedia,
62+
"emulateVisionDeficiency": p.EmulateVisionDeficiency,
63+
"evaluate": p.Evaluate,
64+
"evaluateHandle": p.EvaluateHandle,
65+
"exposeBinding": p.ExposeBinding,
66+
"exposeFunction": p.ExposeFunction,
67+
"fill": p.Fill,
68+
"focus": p.Focus,
69+
"frame": p.Frame,
70+
"frames": p.Frames,
71+
"getAttribute": p.GetAttribute,
72+
"goBack": p.GoBack,
73+
"goForward": p.GoForward,
74+
"goto": p.Goto,
75+
"hover": p.Hover,
76+
"innerHTML": p.InnerHTML,
77+
"innerText": p.InnerText,
78+
"inputValue": p.InputValue,
79+
"isChecked": p.IsChecked,
80+
"isClosed": p.IsClosed,
81+
"isDisabled": p.IsDisabled,
82+
"isEditable": p.IsEditable,
83+
"isEnabled": p.IsEnabled,
84+
"isHidden": p.IsHidden,
85+
"isVisible": p.IsVisible,
86+
"locator": p.Locator,
87+
"mainFrame": p.MainFrame,
88+
"opener": p.Opener,
89+
"pause": p.Pause,
90+
"pdf": p.Pdf,
91+
"press": p.Press,
92+
"reload": p.Reload,
93+
"route": p.Route,
94+
"screenshot": p.Screenshot,
95+
"selectOption": p.SelectOption,
96+
"setContent": p.SetContent,
97+
"setDefaultNavigationTimeout": p.SetDefaultNavigationTimeout,
98+
"setDefaultTimeout": p.SetDefaultTimeout,
99+
"setExtraHTTPHeaders": p.SetExtraHTTPHeaders,
100+
"setInputFiles": p.SetInputFiles,
101+
"setViewportSize": p.SetViewportSize,
102+
"tap": p.Tap,
103+
"textContent": p.TextContent,
104+
"title": p.Title,
105+
"type": p.Type,
106+
"uncheck": p.Uncheck,
107+
"unroute": p.Unroute,
108+
"url": p.URL,
109+
"video": p.Video,
110+
"viewportSize": p.ViewportSize,
111+
"waitForEvent": p.WaitForEvent,
112+
"waitForFunction": p.WaitForFunction,
113+
"waitForLoadState": p.WaitForLoadState,
114+
"waitForNavigation": p.WaitForNavigation,
115+
"waitForRequest": p.WaitForRequest,
116+
"waitForResponse": p.WaitForResponse,
117+
"waitForSelector": p.WaitForSelector,
118+
"waitForTimeout": p.WaitForTimeout,
119+
"workers": p.Workers,
120+
}
121+
122+
return maps
123+
}
124+
43125
// mapBrowserContext to the JS module.
44126
func mapBrowserContext(rt *goja.Runtime, bc api.BrowserContext) mapping {
45-
_ = rt
46127
return mapping{
47128
"addCookies": bc.AddCookies,
48129
"addInitScript": bc.AddInitScript,
@@ -65,8 +146,26 @@ func mapBrowserContext(rt *goja.Runtime, bc api.BrowserContext) mapping {
65146
"storageState": bc.StorageState,
66147
"unroute": bc.Unroute,
67148
"waitForEvent": bc.WaitForEvent,
68-
"pages": bc.Pages,
69-
"newPage": bc.NewPage,
149+
"pages": func() *goja.Object {
150+
var (
151+
mpages []mapping
152+
pages = bc.Pages()
153+
)
154+
for _, page := range pages {
155+
if page == nil {
156+
continue
157+
}
158+
m := mapPage(rt, page)
159+
mpages = append(mpages, m)
160+
}
161+
162+
return rt.ToValue(mpages).ToObject(rt)
163+
},
164+
"newPage": func() *goja.Object {
165+
page := bc.NewPage()
166+
m := mapPage(rt, page)
167+
return rt.ToValue(m).ToObject(rt)
168+
},
70169
}
71170
}
72171

@@ -84,7 +183,11 @@ func mapBrowser(rt *goja.Runtime, b api.Browser) mapping {
84183
m := mapBrowserContext(rt, bctx)
85184
return rt.ToValue(m).ToObject(rt)
86185
},
87-
"newPage": b.NewPage,
186+
"newPage": func(opts goja.Value) *goja.Object {
187+
page := b.NewPage(opts)
188+
m := mapPage(rt, page)
189+
return rt.ToValue(m).ToObject(rt)
190+
},
88191
}
89192
}
90193

0 commit comments

Comments
 (0)