Skip to content

Commit ca837be

Browse files
authored
chore: add documentation to exported structs (playwright-community#92)
1 parent 22b2703 commit ca837be

20 files changed

+84
-57
lines changed

binding_call.go

+4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ type bindingCallImpl struct {
88
channelOwner
99
}
1010

11+
// BindingSource is the value passed to a binding call execution
1112
type BindingSource struct {
1213
Context BrowserContext
1314
Page Page
1415
Frame Frame
1516
}
1617

18+
// ExposedFunction represents the func signature of an exposed function
1719
type ExposedFunction = func(args ...interface{}) interface{}
20+
21+
// BindingCallFunction represents the func signature of an exposed binding call func
1822
type BindingCallFunction = func(source *BindingSource, args ...interface{}) interface{}
1923

2024
func (b *bindingCallImpl) Call(f BindingCallFunction) {

browser_context.go

+27
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ func (b *browserContextImpl) NewPage(options ...BrowserNewPageOptions) (Page, er
5656
return fromChannel(channel).(*pageImpl), nil
5757
}
5858

59+
// NetworkCookie is the return structure of BrowserContext.Cookies()
60+
type NetworkCookie struct {
61+
Name string `json:"name"`
62+
Value string `json:"value"`
63+
Domain string `json:"domain"`
64+
Path string `json:"path"`
65+
Expires int `json:"expires"`
66+
HttpOnly bool `json:"httpOnly"`
67+
Secure bool `json:"secure"`
68+
SameSite string `json:"sameSite"`
69+
}
70+
5971
func (b *browserContextImpl) Cookies(urls ...string) ([]*NetworkCookie, error) {
6072
result, err := b.channel.Send("cookies", map[string]interface{}{
6173
"urls": urls,
@@ -71,6 +83,19 @@ func (b *browserContextImpl) Cookies(urls ...string) ([]*NetworkCookie, error) {
7183
return cookies, nil
7284
}
7385

86+
// SetNetworkCookieParam is used to filter cookies in BrowserContext.AddCookies()
87+
type SetNetworkCookieParam struct {
88+
Name string `json:"name"`
89+
Value string `json:"value"`
90+
URL *string `json:"url"`
91+
Domain *string `json:"domain"`
92+
Path *string `json:"path"`
93+
Expires *int `json:"expires"`
94+
HttpOnly *bool `json:"httpOnly"`
95+
Secure *bool `json:"secure"`
96+
SameSite *string `json:"sameSite"`
97+
}
98+
7499
func (b *browserContextImpl) AddCookies(cookies ...SetNetworkCookieParam) error {
75100
_, err := b.channel.Send("addCookies", map[string]interface{}{
76101
"cookies": cookies,
@@ -95,6 +120,7 @@ func (b *browserContextImpl) ClearPermissions() error {
95120
return err
96121
}
97122

123+
// SetGeolocationOptions represents the options for BrowserContext.SetGeolocation()
98124
type SetGeolocationOptions struct {
99125
Longitude int `json:"longitude"`
100126
Latitude int `json:"latitude"`
@@ -127,6 +153,7 @@ func (b *browserContextImpl) SetOffline(offline bool) error {
127153
return err
128154
}
129155

156+
// BrowserContextAddInitScriptOptions represents the options for BrowserContext.AddInitScript()
130157
type BrowserContextAddInitScriptOptions struct {
131158
Path *string
132159
Script *string

connection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (c *connection) CallOnObjectWithKnownName(name string) (interface{}, error)
4444
return <-c.waitingForRemoteObjects[name], nil
4545
}
4646

47-
func (c *connection) Dispatch(msg *Message) {
47+
func (c *connection) Dispatch(msg *message) {
4848
method := msg.Method
4949
if msg.ID != 0 {
5050
cb, _ := c.callbacks.Load(msg.ID)

console_message.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type consoleMessageImpl struct {
44
channelOwner
55
}
66

7+
// ConsoleMessageLocation represents where a console message was logged in the browser
78
type ConsoleMessageLocation struct {
89
URL string `json:"url"`
910
LineNumber int `json:"lineNumber"`

element_handle.go

+8
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ func (e *elementHandleImpl) SetInputFiles(files []InputFile, options ...ElementH
189189
return err
190190
}
191191

192+
// Rect is the return structure for ElementHandle.BoundingBox()
193+
type Rect struct {
194+
Width int `json:"width"`
195+
Height int `json:"height"`
196+
X int `json:"x"`
197+
Y int `json:"y"`
198+
}
199+
192200
func (e *elementHandleImpl) BoundingBox() (*Rect, error) {
193201
boundingBox, err := e.channel.Send("boundingBox")
194202
if err != nil {

errors.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ type Error struct {
55
Stack string
66
}
77

8+
// Error represents a Playwright error
89
func (e *Error) Error() string {
910
return e.Message
1011
}
1112

13+
// TimeoutError represents a Playwright TimeoutError
1214
type TimeoutError Error
1315

1416
func (e *TimeoutError) Error() string {

examples/end-to-end-testing/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func assertEqual(expected, actual interface{}) {
2020
}
2121
}
2222

23-
const TODO_NAME = "Bake a cake"
23+
const todoName = "Bake a cake"
2424

2525
func main() {
2626
pw, err := playwright.Run()
@@ -48,7 +48,7 @@ func main() {
4848

4949
// Adding a todo entry (click in the input, enter the todo title and press the Enter key)
5050
assertErrorToNilf("could not click: %v", page.Click("input.new-todo"))
51-
assertErrorToNilf("could not type: %v", page.Type("input.new-todo", TODO_NAME))
51+
assertErrorToNilf("could not type: %v", page.Type("input.new-todo", todoName))
5252
assertErrorToNilf("could not press: %v", page.Press("input.new-todo", "Enter"))
5353

5454
// After adding 1 there should be 1 entry in the list
@@ -57,7 +57,7 @@ func main() {
5757
// Here we get the text in the first todo item to see if it"s the same which the user has entered
5858
textContentOfFirstTodoEntry, err := page.EvalOnSelector("ul.todo-list > li:nth-child(1) label", "el => el.textContent")
5959
assertErrorToNilf("could not get first todo entry: %w", err)
60-
assertEqual(TODO_NAME, textContentOfFirstTodoEntry)
60+
assertEqual(todoName, textContentOfFirstTodoEntry)
6161

6262
// The todo list should be persistent. Here we reload the page and see if the entry is still there
6363
_, err = page.Reload()

file_chooser.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ func (f *fileChooserImpl) IsMultiple() bool {
1818
return f.isMultiple
1919
}
2020

21-
func (f *fileChooserImpl) SetFiles(files []InputFile, options ...ElementHandleSetInputFilesOptions) error {
22-
return f.elementHandle.SetInputFiles(files, options...)
23-
}
24-
21+
// InputFile represents the input file for:
22+
// - FileChooser.SetFiles()
23+
// - ElementHandle.SetInputFiles()
24+
// - Page.SetInputFiles()
2525
type InputFile struct {
2626
Name string
2727
MimeType string
2828
Buffer []byte
2929
}
3030

31+
func (f *fileChooserImpl) SetFiles(files []InputFile, options ...ElementHandleSetInputFilesOptions) error {
32+
return f.elementHandle.SetInputFiles(files, options...)
33+
}
34+
3135
func newFileChooser(page Page, elementHandle ElementHandle, isMultiple bool) *fileChooserImpl {
3236
return &fileChooserImpl{
3337
page: page,

frame.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ func (f *frameImpl) Hover(selector string, options ...PageHoverOptions) error {
396396
return err
397397
}
398398

399-
func (e *frameImpl) SetInputFiles(selector string, files []InputFile, options ...FrameSetInputFilesOptions) error {
400-
_, err := e.channel.Send("setInputFiles", map[string]interface{}{
399+
func (f *frameImpl) SetInputFiles(selector string, files []InputFile, options ...FrameSetInputFilesOptions) error {
400+
_, err := f.channel.Send("setInputFiles", map[string]interface{}{
401401
"selector": selector,
402402
"files": normalizeFilePayloads(files),
403403
}, options)

helpers.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func newSafeStringSet(v []string) *safeStringSet {
254254
}
255255
}
256256

257-
const DEFAULT_TIMEOUT = 30 * 1000
257+
const defaultTimeout = 30 * 1000
258258

259259
type timeoutSettings struct {
260260
parent *timeoutSettings
@@ -273,7 +273,7 @@ func (t *timeoutSettings) Timeout() int {
273273
if t.parent != nil {
274274
return t.parent.Timeout()
275275
}
276-
return DEFAULT_TIMEOUT
276+
return defaultTimeout
277277
}
278278

279279
func (t *timeoutSettings) SetNavigationTimeout(navigationTimeout int) {
@@ -287,14 +287,14 @@ func (t *timeoutSettings) NavigationTimeout() int {
287287
if t.parent != nil {
288288
return t.parent.NavigationTimeout()
289289
}
290-
return DEFAULT_TIMEOUT
290+
return defaultTimeout
291291
}
292292

293293
func newTimeoutSettings(parent *timeoutSettings) *timeoutSettings {
294294
return &timeoutSettings{
295295
parent: parent,
296-
timeout: DEFAULT_TIMEOUT,
297-
navigationTimeout: DEFAULT_TIMEOUT,
296+
timeout: defaultTimeout,
297+
navigationTimeout: defaultTimeout,
298298
}
299299
}
300300

network.go

-23
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,3 @@ func parseHeaders(headers []interface{}) map[string]string {
2121
}
2222
return out
2323
}
24-
25-
type NetworkCookie struct {
26-
Name string `json:"name"`
27-
Value string `json:"value"`
28-
Domain string `json:"domain"`
29-
Path string `json:"path"`
30-
Expires int `json:"expires"`
31-
HttpOnly bool `json:"httpOnly"`
32-
Secure bool `json:"secure"`
33-
SameSite string `json:"sameSite"`
34-
}
35-
36-
type SetNetworkCookieParam struct {
37-
Name string `json:"name"`
38-
Value string `json:"value"`
39-
URL *string `json:"url"`
40-
Domain *string `json:"domain"`
41-
Path *string `json:"path"`
42-
Expires *int `json:"expires"`
43-
HttpOnly *bool `json:"httpOnly"`
44-
Secure *bool `json:"secure"`
45-
SameSite *string `json:"sameSite"`
46-
}

page.go

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (p *pageImpl) MainFrame() Frame {
6464
return p.mainFrame
6565
}
6666

67+
// PageFrameOptions is the option struct for Page.Frame()
6768
type PageFrameOptions struct {
6869
Name *string
6970
URL interface{}
@@ -213,6 +214,7 @@ func (p *pageImpl) EmulateMedia(options ...PageEmulateMediaOptions) error {
213214
return err
214215
}
215216

217+
// ViewportSize represents the viewport size
216218
type ViewportSize struct {
217219
Width int `json:"width"`
218220
Height int `json:"height"`

playwright.go

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// is ever-green, capable, reliable and fast.
44
package playwright
55

6+
// DeviceDescriptor represents a single device
67
type DeviceDescriptor struct {
78
UserAgent string `json:"userAgent"`
89
Viewport *BrowserNewContextViewport `json:"viewport"`
@@ -12,6 +13,7 @@ type DeviceDescriptor struct {
1213
DefaultBrowserType string `json:"defaultBrowserType"`
1314
}
1415

16+
// Playwright represents a Playwright instance
1517
type Playwright struct {
1618
channelOwner
1719
Chromium BrowserType
@@ -20,6 +22,7 @@ type Playwright struct {
2022
Devices map[string]*DeviceDescriptor
2123
}
2224

25+
// Stop stops the Playwright instance
2326
func (p *Playwright) Stop() error {
2427
return p.connection.Stop()
2528
}

request.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"encoding/json"
66
)
77

8+
// RequestFailure represents a request failure
89
type RequestFailure struct {
910
ErrorText string
1011
}
1112

13+
// ResourceTiming represents the resource timing
1214
type ResourceTiming struct {
1315
StartTime float64
1416
DomainLookupStart float64

route.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func (r *routeImpl) Request() Request {
1616
return fromChannel(r.initializer["request"]).(*requestImpl)
1717
}
1818

19+
// RouteAbortOptions is the option struct for Route.Abort()
1920
type RouteAbortOptions struct {
2021
ErrorCode *string `json:"errorCode"`
2122
}
@@ -25,6 +26,7 @@ func (r *routeImpl) Abort(options ...RouteAbortOptions) error {
2526
return err
2627
}
2728

29+
// RouteAbortOptions is the option struct for Route.Fulfill()
2830
type RouteFulfillOptions struct {
2931
Status *int `json:"status"`
3032
Headers map[string]string `json:"headers"`

run.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"strings"
1717
)
1818

19-
const PLAYWRIGHT_CLI_VERSION = "0.171.0"
19+
const playwrightCliVersion = "0.171.0"
2020

2121
type playwrightDriver struct {
2222
driverDirectory, driverBinaryLocation, version string
@@ -34,12 +34,12 @@ func newDriver(options *RunOptions) (*playwrightDriver, error) {
3434
return nil, fmt.Errorf("could not get default cache directory: %v", err)
3535
}
3636
}
37-
driverDirectory := filepath.Join(baseDriverDirectory, "ms-playwright-go", PLAYWRIGHT_CLI_VERSION)
37+
driverDirectory := filepath.Join(baseDriverDirectory, "ms-playwright-go", playwrightCliVersion)
3838
driverBinaryLocation := filepath.Join(driverDirectory, getDriverName())
3939
return &playwrightDriver{
4040
driverBinaryLocation: driverBinaryLocation,
4141
driverDirectory: driverDirectory,
42-
version: PLAYWRIGHT_CLI_VERSION,
42+
version: playwrightCliVersion,
4343
}, nil
4444
}
4545

@@ -182,6 +182,7 @@ func (d *playwrightDriver) installBrowsers(driverPath string) error {
182182
return nil
183183
}
184184

185+
// RunOptions are custom options to run the driver
185186
type RunOptions struct {
186187
DriverDirectory string
187188
}
@@ -200,6 +201,7 @@ func Install(options ...*RunOptions) error {
200201
return nil
201202
}
202203

204+
// Run starts a Playwright instance
203205
func Run(options ...*RunOptions) (*Playwright, error) {
204206
driver, err := newDriver(transformRunOptions(options))
205207
if err != nil {

scripts/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const child_process = require("child_process")
55

66
const getCliVersion = () => {
77
const runGoContent = fs.readFileSync(path.join(__dirname, "..", "run.go")).toString()
8-
const findings = /PLAYWRIGHT_CLI_VERSION = "(.*)"/.exec(runGoContent)
8+
const findings = /playwrightCliVersion = "(.*)"/.exec(runGoContent)
99
return findings[1]
1010
}
1111

transport.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
type transport struct {
1616
stdin io.WriteCloser
1717
stdout io.ReadCloser
18-
dispatch func(msg *Message)
18+
dispatch func(msg *message)
1919
rLock sync.Mutex
2020
}
2121

@@ -31,7 +31,7 @@ func (t *transport) Start() error {
3131
}
3232
length := binary.LittleEndian.Uint32(lengthContent)
3333

34-
msg := &Message{}
34+
msg := &message{}
3535
if err := json.NewDecoder(io.LimitReader(reader, int64(length))).Decode(&msg); err != nil {
3636
return fmt.Errorf("could not decode json: %w", err)
3737
}
@@ -55,7 +55,7 @@ type errorPayload struct {
5555
Stack string `json:"stack"`
5656
}
5757

58-
type Message struct {
58+
type message struct {
5959
ID int `json:"id"`
6060
GUID string `json:"guid"`
6161
Method string `json:"method"`
@@ -91,7 +91,7 @@ func (t *transport) Send(message map[string]interface{}) error {
9191
return nil
9292
}
9393

94-
func newTransport(stdin io.WriteCloser, stdout io.ReadCloser, dispatch func(msg *Message)) *transport {
94+
func newTransport(stdin io.WriteCloser, stdout io.ReadCloser, dispatch func(msg *message)) *transport {
9595
return &transport{
9696
stdout: stdout,
9797
stdin: stdin,

0 commit comments

Comments
 (0)