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

Commit c613b92

Browse files
authored
Merge pull request #196 from grafana/refactor/elementhandle-evaluate-calls
Refactor ElementHandle evaluate calls
2 parents 9e0085e + 6987036 commit c613b92

8 files changed

+273
-391
lines changed

common/element_handle.go

Lines changed: 231 additions & 348 deletions
Large diffs are not rendered by default.

common/execution_context.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ func (ew executionWorld) valid() bool {
4949
return ew == mainWorld || ew == utilityWorld
5050
}
5151

52-
type evaluateOptions struct {
52+
type evalOptions struct {
5353
forceCallable, returnByValue bool
5454
}
5555

56-
func (ea evaluateOptions) String() string {
56+
func (ea evalOptions) String() string {
5757
return fmt.Sprintf("forceCallable:%t returnByValue:%t", ea.forceCallable, ea.returnByValue)
5858
}
5959

@@ -162,11 +162,10 @@ func (e *ExecutionContext) adoptElementHandle(eh *ElementHandle) (*ElementHandle
162162
return e.adoptBackendNodeID(node.BackendNodeID)
163163
}
164164

165-
// evaluate will evaluate provided callable within this execution context
166-
// and return by value or handle
167-
func (e *ExecutionContext) evaluate(
165+
// eval will evaluate provided callable within this execution context and return by value or handle.
166+
func (e *ExecutionContext) eval(
168167
apiCtx context.Context,
169-
opts evaluateOptions, pageFunc goja.Value, args ...goja.Value,
168+
opts evalOptions, pageFunc goja.Value, args ...goja.Value,
170169
) (res interface{}, err error) {
171170
e.logger.Debugf(
172171
"ExecutionContext:evaluate",
@@ -290,11 +289,11 @@ func (e *ExecutionContext) getInjectedScript(apiCtx context.Context) (api.JSHand
290289
expressionWithSourceURL = expression + "\n" + suffix
291290
}
292291

293-
opts := evaluateOptions{
292+
opts := evalOptions{
294293
forceCallable: false,
295294
returnByValue: false,
296295
}
297-
handle, err := e.evaluate(apiCtx, opts, rt.ToValue(expressionWithSourceURL))
296+
handle, err := e.eval(apiCtx, opts, rt.ToValue(expressionWithSourceURL))
298297
if handle == nil || err != nil {
299298
return nil, fmt.Errorf("cannot get injected script (%q): %w", suffix, err)
300299
}
@@ -303,28 +302,28 @@ func (e *ExecutionContext) getInjectedScript(apiCtx context.Context) (api.JSHand
303302
return e.injectedScript, nil
304303
}
305304

306-
// Evaluate will evaluate provided page function within this execution context
307-
func (e *ExecutionContext) Evaluate(
305+
// Eval will evaluate provided page function within this execution context
306+
func (e *ExecutionContext) Eval(
308307
apiCtx context.Context,
309308
pageFunc goja.Value, args ...goja.Value,
310309
) (interface{}, error) {
311-
opts := evaluateOptions{
310+
opts := evalOptions{
312311
forceCallable: true,
313312
returnByValue: true,
314313
}
315-
return e.evaluate(apiCtx, opts, pageFunc, args...)
314+
return e.eval(apiCtx, opts, pageFunc, args...)
316315
}
317316

318-
// EvaluateHandle will evaluate provided page function within this execution context
319-
func (e *ExecutionContext) EvaluateHandle(
317+
// EvalHandle will evaluate provided page function within this execution context
318+
func (e *ExecutionContext) EvalHandle(
320319
apiCtx context.Context,
321320
pageFunc goja.Value, args ...goja.Value,
322321
) (api.JSHandle, error) {
323-
opts := evaluateOptions{
322+
opts := evalOptions{
324323
forceCallable: true,
325324
returnByValue: false,
326325
}
327-
res, err := e.evaluate(apiCtx, opts, pageFunc, args...)
326+
res, err := e.eval(apiCtx, opts, pageFunc, args...)
328327
if err != nil {
329328
return nil, err
330329
}

common/frame.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (f *Frame) document() (*ElementHandle, error) {
321321

322322
f.waitForExecutionContext(mainWorld)
323323

324-
opts := evaluateOptions{
324+
opts := evalOptions{
325325
forceCallable: false,
326326
returnByValue: false,
327327
}
@@ -531,11 +531,11 @@ func (f *Frame) waitForFunction(
531531
} else {
532532
predicate = fmt.Sprintf("return (%s)(...args);", predicateFn.ToString().String())
533533
}
534-
opts := evaluateOptions{
534+
opts := evalOptions{
535535
forceCallable: true,
536536
returnByValue: false,
537537
}
538-
result, err := execCtx.evaluate(
538+
result, err := execCtx.eval(
539539
apiCtx, opts, pageFn, append([]goja.Value{
540540
rt.ToValue(injected),
541541
rt.ToValue(predicate),
@@ -721,7 +721,7 @@ func (f *Frame) Evaluate(pageFunc goja.Value, args ...goja.Value) interface{} {
721721

722722
f.waitForExecutionContext(mainWorld)
723723

724-
opts := evaluateOptions{
724+
opts := evalOptions{
725725
forceCallable: true,
726726
returnByValue: true,
727727
}
@@ -750,7 +750,7 @@ func (f *Frame) EvaluateHandle(pageFunc goja.Value, args ...goja.Value) (handle
750750
if ec == nil {
751751
k6common.Throw(rt, fmt.Errorf("cannot find execution context: %q", mainWorld))
752752
}
753-
handle, err = ec.EvaluateHandle(f.ctx, pageFunc, args...)
753+
handle, err = ec.EvalHandle(f.ctx, pageFunc, args...)
754754
}
755755
f.executionContextMu.RUnlock()
756756
if err != nil {
@@ -1245,7 +1245,7 @@ func (f *Frame) SetContent(html string, opts goja.Value) {
12451245

12461246
f.waitForExecutionContext(utilityWorld)
12471247

1248-
eopts := evaluateOptions{
1248+
eopts := evalOptions{
12491249
forceCallable: true,
12501250
returnByValue: true,
12511251
}
@@ -1474,7 +1474,7 @@ func (f *Frame) adoptBackendNodeID(world executionWorld, id cdp.BackendNodeID) (
14741474
func (f *Frame) evaluate(
14751475
apiCtx context.Context,
14761476
world executionWorld,
1477-
opts evaluateOptions, pageFunc goja.Value, args ...goja.Value,
1477+
opts evalOptions, pageFunc goja.Value, args ...goja.Value,
14781478
) (interface{}, error) {
14791479
f.log.Debugf("Frame:evaluate", "fid:%s furl:%q world:%s opts:%s", f.ID(), f.URL(), world, opts)
14801480

@@ -1485,7 +1485,7 @@ func (f *Frame) evaluate(
14851485
if ec == nil {
14861486
return nil, fmt.Errorf("cannot find execution context: %q", world)
14871487
}
1488-
eh, err := ec.evaluate(apiCtx, opts, pageFunc, args...)
1488+
eh, err := ec.eval(apiCtx, opts, pageFunc, args...)
14891489
if err != nil {
14901490
return nil, fmt.Errorf("frame cannot evaluate: %w", err)
14911491
}
@@ -1502,28 +1502,28 @@ type frameExecutionContext interface {
15021502
// execution context from another execution context.
15031503
adoptElementHandle(elementHandle *ElementHandle) (*ElementHandle, error)
15041504

1505-
// evaluate will evaluate provided callable within this execution
1505+
// eval will evaluate provided callable within this execution
15061506
// context and return by value or handle.
1507-
evaluate(
1507+
eval(
15081508
apiCtx context.Context,
1509-
opts evaluateOptions,
1509+
opts evalOptions,
15101510
pageFunc goja.Value, args ...goja.Value,
15111511
) (res interface{}, err error)
15121512

15131513
// getInjectedScript returns a JS handle to the injected script of helper
15141514
// functions.
15151515
getInjectedScript(apiCtx context.Context) (api.JSHandle, error)
15161516

1517-
// Evaluate will evaluate provided page function within this execution
1517+
// Eval will evaluate provided page function within this execution
15181518
// context.
1519-
Evaluate(
1519+
Eval(
15201520
apiCtx context.Context,
15211521
pageFunc goja.Value, args ...goja.Value,
15221522
) (interface{}, error)
15231523

1524-
// EvaluateHandle will evaluate provided page function within this
1524+
// EvalHandle will evaluate provided page function within this
15251525
// execution context.
1526-
EvaluateHandle(
1526+
EvalHandle(
15271527
apiCtx context.Context,
15281528
pageFunc goja.Value, args ...goja.Value,
15291529
) (api.JSHandle, error)

common/frame_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestFrameNilDocument(t *testing.T) {
4343

4444
// frame should not panic with a nil document
4545
stub := &executionContextTestStub{
46-
evaluateFn: func(apiCtx context.Context, opts evaluateOptions, pageFunc goja.Value, args ...goja.Value) (res interface{}, err error) {
46+
evalFn: func(apiCtx context.Context, opts evalOptions, pageFunc goja.Value, args ...goja.Value) (res interface{}, err error) {
4747
// return nil to test for panic
4848
return nil, nil
4949
},
@@ -68,7 +68,7 @@ func TestFrameNilDocument(t *testing.T) {
6868

6969
// frame gets the document from the evaluate call
7070
want := &ElementHandle{}
71-
stub.evaluateFn = func(apiCtx context.Context, opts evaluateOptions, pageFunc goja.Value, args ...goja.Value) (res interface{}, err error) {
71+
stub.evalFn = func(apiCtx context.Context, opts evalOptions, pageFunc goja.Value, args ...goja.Value) (res interface{}, err error) {
7272
return want, nil
7373
}
7474
got, err := frame.document()
@@ -116,9 +116,9 @@ func TestFrameManagerFrameAbortedNavigationShouldEmitANonNilPendingDocument(t *t
116116

117117
type executionContextTestStub struct {
118118
ExecutionContext
119-
evaluateFn func(apiCtx context.Context, opts evaluateOptions, pageFunc goja.Value, args ...goja.Value) (res interface{}, err error)
119+
evalFn func(apiCtx context.Context, opts evalOptions, pageFunc goja.Value, args ...goja.Value) (res interface{}, err error)
120120
}
121121

122-
func (e executionContextTestStub) evaluate(apiCtx context.Context, opts evaluateOptions, pageFunc goja.Value, args ...goja.Value) (res interface{}, err error) {
123-
return e.evaluateFn(apiCtx, opts, pageFunc, args...)
122+
func (e executionContextTestStub) eval(apiCtx context.Context, opts evalOptions, pageFunc goja.Value, args ...goja.Value) (res interface{}, err error) {
123+
return e.evalFn(apiCtx, opts, pageFunc, args...)
124124
}

common/js_handle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (h *BaseJSHandle) Dispose() {
9595
func (h *BaseJSHandle) Evaluate(pageFunc goja.Value, args ...goja.Value) interface{} {
9696
rt := k6common.GetRuntime(h.ctx)
9797
args = append([]goja.Value{rt.ToValue(h)}, args...)
98-
res, err := h.execCtx.Evaluate(h.ctx, pageFunc, args...)
98+
res, err := h.execCtx.Eval(h.ctx, pageFunc, args...)
9999
if err != nil {
100100
k6common.Throw(rt, err)
101101
}
@@ -106,7 +106,7 @@ func (h *BaseJSHandle) Evaluate(pageFunc goja.Value, args ...goja.Value) interfa
106106
func (h *BaseJSHandle) EvaluateHandle(pageFunc goja.Value, args ...goja.Value) api.JSHandle {
107107
rt := k6common.GetRuntime(h.ctx)
108108
args = append([]goja.Value{rt.ToValue(h)}, args...)
109-
res, err := h.execCtx.EvaluateHandle(h.ctx, pageFunc, args...)
109+
res, err := h.execCtx.EvalHandle(h.ctx, pageFunc, args...)
110110
if err != nil {
111111
k6common.Throw(rt, err)
112112
}

common/page.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,11 @@ func (p *Page) getOwnerFrame(apiCtx context.Context, h *ElementHandle) cdp.Frame
225225
}
226226
`)
227227

228-
opts := evaluateOptions{
228+
opts := evalOptions{
229229
forceCallable: true,
230230
returnByValue: false,
231231
}
232-
result, err := h.execCtx.evaluate(apiCtx, opts, pageFn, []goja.Value{rt.ToValue(h)}...)
232+
result, err := h.execCtx.eval(apiCtx, opts, pageFn, []goja.Value{rt.ToValue(h)}...)
233233
if err != nil {
234234
p.logger.Debugf("Page:getOwnerFrame:return", "sid:%v err:%v", p.sessionID(), err)
235235
return ""

common/screenshotter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func newScreenshotter(ctx context.Context) *screenshotter {
4747

4848
func (s *screenshotter) fullPageSize(p *Page) (*Size, error) {
4949
rt := k6common.GetRuntime(s.ctx)
50-
opts := evaluateOptions{
50+
opts := evalOptions{
5151
forceCallable: true,
5252
returnByValue: true,
5353
}
@@ -91,7 +91,7 @@ func (s *screenshotter) originalViewportSize(p *Page) (*Size, *Size, error) {
9191
return &viewportSize, &originalViewportSize, nil
9292
}
9393

94-
opts := evaluateOptions{
94+
opts := evalOptions{
9595
forceCallable: true,
9696
returnByValue: true,
9797
}

tests/element_handle_click_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,5 @@ func TestElementHandleClickWithDetachedNode(t *testing.T) {
140140
}))
141141
}
142142
panicTestFn()
143-
assert.Equal(t, "element is not attached to the DOM", errorMsg, "expected click to result in correct error to be thrown")
143+
assert.Contains(t, errorMsg, "element is not attached to the DOM", "expected click to result in correct error to be thrown")
144144
}

0 commit comments

Comments
 (0)