Skip to content

Commit 7430509

Browse files
authored
test: add more worker and element tests (#89)
1 parent e5f3d04 commit 7430509

16 files changed

+375
-36
lines changed

element_handle.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ func newElementHandle(parent *channelOwner, objectType string, guid string, init
277277
func normalizeFilePayloads(files []InputFile) []map[string]string {
278278
out := make([]map[string]string, 0)
279279
for _, file := range files {
280-
// file.Buffer
281280
out = append(out, map[string]string{
282281
"name": file.Name,
283282
"mimeType": file.MimeType,

frame.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func (f *frameImpl) EvalOnSelectorAll(selector string, expression string, option
297297
return parseResult(result), nil
298298
}
299299

300-
func (f *frameImpl) EvaluateHandle(expression string, options ...interface{}) (interface{}, error) {
300+
func (f *frameImpl) EvaluateHandle(expression string, options ...interface{}) (JSHandle, error) {
301301
var arg interface{}
302302
forceExpression := false
303303
if !isFunctionBody(expression) {
@@ -321,7 +321,7 @@ func (f *frameImpl) EvaluateHandle(expression string, options ...interface{}) (i
321321
if channelOwner == nil {
322322
return nil, nil
323323
}
324-
return channelOwner, nil
324+
return channelOwner.(JSHandle), nil
325325
}
326326

327327
func (f *frameImpl) Click(selector string, options ...PageClickOptions) error {

generated_interfaces.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ type Download interface {
213213
// ElementHandles are auto-disposed when their origin frame gets navigated.
214214
// ElementHandle instances can be used as an argument in page.$eval(selector, pageFunction[, arg]) and page.evaluate(pageFunction[, arg]) methods.
215215
type ElementHandle interface {
216-
AsElement() ElementHandle
216+
JSHandle
217217
// This method returns the bounding box of the element, or `null` if the element is not visible. The bounding box is
218218
// calculated relative to the main frame viewport - which is usually the same as the browser window.
219219
// Scrolling affects the returned bonding box, similarly to
@@ -486,7 +486,7 @@ type Frame interface {
486486
// the promise to resolve and return its value.
487487
// A string can also be passed in instead of a function.
488488
// JSHandle instances can be passed as an argument to the `frame.evaluateHandle`:
489-
EvaluateHandle(expression string, options ...interface{}) (interface{}, error)
489+
EvaluateHandle(expression string, options ...interface{}) (JSHandle, error)
490490
// Returns the return value of `pageFunction`
491491
// The method finds an element matching the specified selector within the frame and passes it as a first argument to
492492
// `pageFunction`. See Working with selectors for more details. If no elements match the
@@ -669,7 +669,7 @@ type JSHandle interface {
669669
// If the function passed to the `jsHandle.evaluateHandle` returns a Promise, then `jsHandle.evaluateHandle` would wait
670670
// for the promise to resolve and return its value.
671671
// See page.evaluateHandle(pageFunction[, arg]) for more details.
672-
EvaluateHandle(expression string, options ...interface{}) (interface{}, error)
672+
EvaluateHandle(expression string, options ...interface{}) (JSHandle, error)
673673
// The method returns a map with **own property names** as keys and JSHandle instances for the property values.
674674
GetProperties() (map[string]JSHandle, error)
675675
// Fetches a single property from the referenced object.
@@ -757,10 +757,10 @@ type Mouse interface {
757757
// This example logs a message for a single page `load` event:
758758
// To unsubscribe from events use the `removeListener` method:
759759
type Page interface {
760+
EventEmitter
760761
Mouse() Mouse
761762
Keyboard() Keyboard
762763
Touchscreen() Touchscreen
763-
EventEmitter
764764
// Adds a script which would be evaluated in one of the following scenarios:
765765
// Whenever the page is navigated.
766766
// Whenever the child frame is attached or navigated. In this case, the script is evaluated in the context of the newly attached frame.
@@ -875,7 +875,7 @@ type Page interface {
875875
// promise to resolve and return its value.
876876
// A string can also be passed in instead of a function:
877877
// JSHandle instances can be passed as an argument to the `page.evaluateHandle`:
878-
EvaluateHandle(expression string, options ...interface{}) (interface{}, error)
878+
EvaluateHandle(expression string, options ...interface{}) (JSHandle, error)
879879
// The method finds an element matching the specified selector within the page and passes it as a first argument to
880880
// `pageFunction`. If no elements match the selector, the method throws an error. Returns the value of `pageFunction`.
881881
// If `pageFunction` returns a Promise, then `page.$eval` would wait for the promise to resolve and return its value.
@@ -891,7 +891,7 @@ type Page interface {
891891
ExpectDownload(cb func() error) (Download, error)
892892
ExpectEvent(event string, cb func() error, predicates ...interface{}) (interface{}, error)
893893
ExpectFileChooser(cb func() error) (FileChooser, error)
894-
ExpectLoadState(state string, cb func() error) (ConsoleMessage, error)
894+
ExpectLoadState(state string, cb func() error) error
895895
ExpectNavigation(cb func() error, options ...PageWaitForNavigationOptions) (Response, error)
896896
ExpectPopup(cb func() error) (Page, error)
897897
ExpectRequest(url interface{}, cb func() error, options ...interface{}) (Request, error)
@@ -1269,6 +1269,7 @@ type Video interface {
12691269
// event is emitted on the page object to signal a worker creation. `close` event is emitted on the worker object when the
12701270
// worker is gone.
12711271
type Worker interface {
1272+
EventEmitter
12721273
// Returns the return value of `pageFunction`
12731274
// If the function passed to the `worker.evaluate` returns a Promise, then `worker.evaluate` would wait for the promise
12741275
// to resolve and return its value.
@@ -1283,4 +1284,6 @@ type Worker interface {
12831284
// the promise to resolve and return its value.
12841285
EvaluateHandle(expression string, options ...interface{}) (JSHandle, error)
12851286
URL() string
1287+
WaitForEvent(event string, predicate ...interface{}) interface{}
1288+
ExpectEvent(event string, cb func() error, predicates ...interface{}) (interface{}, error)
12861289
}

js_handle.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (j *jsHandleImpl) Evaluate(expression string, options ...interface{}) (inte
3838
return parseResult(result), nil
3939
}
4040

41-
func (j *jsHandleImpl) EvaluateHandle(expression string, options ...interface{}) (interface{}, error) {
41+
func (j *jsHandleImpl) EvaluateHandle(expression string, options ...interface{}) (JSHandle, error) {
4242
var arg interface{}
4343
forceExpression := false
4444
if !isFunctionBody(expression) {
@@ -171,6 +171,13 @@ func serializeValue(value interface{}, handles *[]*channel, depth int) interface
171171
"h": h,
172172
}
173173
}
174+
if handle, ok := value.(*jsHandleImpl); ok {
175+
h := len(*handles)
176+
*handles = append(*handles, handle.channel)
177+
return map[string]interface{}{
178+
"h": h,
179+
}
180+
}
174181
if depth > 100 {
175182
panic(errors.New("Maximum argument depth exceeded"))
176183
}

page.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (p *pageImpl) Evaluate(expression string, options ...interface{}) (interfac
126126
return p.mainFrame.Evaluate(expression, options...)
127127
}
128128

129-
func (p *pageImpl) EvaluateHandle(expression string, options ...interface{}) (interface{}, error) {
129+
func (p *pageImpl) EvaluateHandle(expression string, options ...interface{}) (JSHandle, error) {
130130
return p.mainFrame.EvaluateHandle(expression, options...)
131131
}
132132

@@ -390,9 +390,9 @@ func (p *pageImpl) ExpectFileChooser(cb func() error) (FileChooser, error) {
390390
return response.(*fileChooserImpl), err
391391
}
392392

393-
func (p *pageImpl) ExpectLoadState(state string, cb func() error) (ConsoleMessage, error) {
394-
response, err := newExpectWrapper(p.mainFrame.WaitForLoadState, []interface{}{state}, cb)
395-
return response.(*consoleMessageImpl), err
393+
func (p *pageImpl) ExpectLoadState(state string, cb func() error) error {
394+
_, err := newExpectWrapper(p.mainFrame.WaitForLoadState, []interface{}{state}, cb)
395+
return err
396396
}
397397

398398
func (p *pageImpl) ExpectPopup(cb func() error) (Page, error) {

scripts/data/interfaces.json

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
]
77
},
88
"Browser": {
9-
"EventEmitter": [],
9+
"extends": [
10+
"EventEmitter"
11+
],
1012
"Close": [
1113
null,
1214
"error"
@@ -33,7 +35,9 @@
3335
]
3436
},
3537
"BrowserContext": {
36-
"EventEmitter": [],
38+
"extends": [
39+
"EventEmitter"
40+
],
3741
"AddCookies": [
3842
"cookies ...SetNetworkCookieParam",
3943
"error"
@@ -216,9 +220,8 @@
216220
]
217221
},
218222
"ElementHandle": {
219-
"AsElement": [
220-
null,
221-
"ElementHandle"
223+
"extends": [
224+
"JSHandle"
222225
],
223226
"BoundingBox": [
224227
null,
@@ -408,7 +411,7 @@
408411
],
409412
"EvaluateHandle": [
410413
"expression string, options ...interface{}",
411-
"interface{}, error"
414+
"JSHandle, error"
412415
],
413416
"EvalOnSelector": [
414417
"selector string, expression string, options ...interface{}",
@@ -550,7 +553,7 @@
550553
],
551554
"EvaluateHandle": [
552555
"expression string, options ...interface{}",
553-
"interface{}, error"
556+
"JSHandle, error"
554557
],
555558
"GetProperties": [
556559
null,
@@ -614,6 +617,9 @@
614617
]
615618
},
616619
"Page": {
620+
"extends": [
621+
"EventEmitter"
622+
],
617623
"Mouse": [
618624
null,
619625
"Mouse"
@@ -626,7 +632,6 @@
626632
null,
627633
"Touchscreen"
628634
],
629-
"EventEmitter": [],
630635
"AddInitScript": [
631636
"options BrowserContextAddInitScriptOptions",
632637
"error"
@@ -689,7 +694,7 @@
689694
],
690695
"EvaluateHandle": [
691696
"expression string, options ...interface{}",
692-
"interface{}, error"
697+
"JSHandle, error"
693698
],
694699
"EvalOnSelector": [
695700
"selector string, expression string, options ...interface{}",
@@ -717,7 +722,7 @@
717722
],
718723
"ExpectLoadState": [
719724
"state string, cb func() error",
720-
"ConsoleMessage, error"
725+
"error"
721726
],
722727
"ExpectNavigation": [
723728
"cb func() error, options ...PageWaitForNavigationOptions",
@@ -1049,7 +1054,9 @@
10491054
]
10501055
},
10511056
"WebSocket": {
1052-
"EventEmitter": [],
1057+
"extends": [
1058+
"EventEmitter"
1059+
],
10531060
"IsClosed": [
10541061
null,
10551062
"bool"
@@ -1070,6 +1077,9 @@
10701077
]
10711078
},
10721079
"Worker": {
1080+
"extends": [
1081+
"EventEmitter"
1082+
],
10731083
"Evaluate": [
10741084
"expression string, options ...interface{}",
10751085
"interface{}, error"
@@ -1081,6 +1091,14 @@
10811091
"URL": [
10821092
null,
10831093
"string"
1094+
],
1095+
"WaitForEvent": [
1096+
"event string, predicate ...interface{}",
1097+
"interface{}"
1098+
],
1099+
"ExpectEvent": [
1100+
"event string, cb func() error, predicates ...interface{}",
1101+
"(interface{}, error)"
10841102
]
10851103
}
10861104
}

scripts/generate-interfaces.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ for (const [className, methods] of Object.entries(interfaceData)) {
4848
writeComment(api[className].comment)
4949
console.log(`type ${className} interface {`)
5050
for (const [funcName, funcData] of Object.entries(methods)) {
51-
if (funcData.length === 0) {
52-
console.log(funcName)
51+
if (funcName === "extends") {
52+
for (const inheritedInterface of funcData)
53+
console.log(inheritedInterface)
5354
} else {
5455
const apiFunc = api[className] && Object.entries(api[className].methods).find(([item]) => transformMethodNamesToGo(item) === funcName)
5556
if (apiFunc && apiFunc[1].comment)

tests/binding_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,25 @@ func TestBrowserContextExposeBindingPanic(t *testing.T) {
8080
stack := strings.Split(innerError["stack"].(string), "\n")
8181
require.Contains(t, stack[len(stack)-1], "binding_test.go")
8282
}
83+
84+
func TestBrowserContextExposeBindingHandleShouldWork(t *testing.T) {
85+
BeforeEach(t)
86+
defer AfterEach(t)
87+
targets := []playwright.JSHandle{}
88+
89+
logme := func(t interface{}) int {
90+
targets = append(targets, t.(playwright.JSHandle))
91+
return 17
92+
}
93+
94+
err := page.ExposeBinding("logme", func(source *playwright.BindingSource, args ...interface{}) interface{} {
95+
return logme(args[0])
96+
}, true)
97+
require.NoError(t, err)
98+
result, err := page.Evaluate("logme({ foo: 42 })")
99+
require.NoError(t, err)
100+
require.Equal(t, result, 17)
101+
res, err := targets[0].Evaluate("x => x.foo")
102+
require.NoError(t, err)
103+
require.Equal(t, 42, res)
104+
}

tests/browser_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ func TestBrowserNewPage(t *testing.T) {
3232
page, err := browser.NewPage()
3333
require.NoError(t, err)
3434
require.Equal(t, 2, len(browser.Contexts()))
35+
require.False(t, page.IsClosed())
3536
require.NoError(t, page.Close())
37+
require.True(t, page.IsClosed())
3638
require.Equal(t, 1, len(browser.Contexts()))
3739
}
3840

tests/dialog_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,25 @@ func TestDialog(t *testing.T) {
1919
_, err := page.Evaluate("alert('yo')")
2020
require.NoError(t, err)
2121
}
22+
23+
func TestDialogDismiss(t *testing.T) {
24+
BeforeEach(t)
25+
defer AfterEach(t)
26+
page.On("dialog", func(dialog playwright.Dialog) {
27+
require.NoError(t, dialog.Dismiss())
28+
})
29+
result, err := page.Evaluate("prompt('question?')")
30+
require.NoError(t, err)
31+
require.Equal(t, result, nil)
32+
}
33+
34+
func TestDialogAcceptWithText(t *testing.T) {
35+
BeforeEach(t)
36+
defer AfterEach(t)
37+
page.On("dialog", func(dialog playwright.Dialog) {
38+
require.NoError(t, dialog.Accept("hey foobar"))
39+
})
40+
result, err := page.Evaluate("prompt('question?')")
41+
require.NoError(t, err)
42+
require.Equal(t, result, "hey foobar")
43+
}

0 commit comments

Comments
 (0)