Skip to content

Commit

Permalink
Feature/pre compiled eval scripts (#658)
Browse files Browse the repository at this point in the history
* Added support of pre-compiled eval expressions

* Added unit tests for eval.Function

* Added RemoteType and RemoteObjectType enums

* Refactored function generation

* Refactored Document and Element loading logic

* Removed redundant fields from cdp.Page

* Exposed eval.Runtime to external callers

* Added new eval.RemoteValue interface
  • Loading branch information
ziflex authored Sep 19, 2021
1 parent 90427cd commit 847dda1
Show file tree
Hide file tree
Showing 23 changed files with 1,259 additions and 412 deletions.
1 change: 1 addition & 0 deletions e2e/tests/dynamic/doc/iframes/element_exists.fql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ LET frames = (

LET doc = FIRST(frames)

T::NOT::NONE(doc)
T::TRUE(ELEMENT_EXISTS(doc, '.text-center'))
T::FALSE(ELEMENT_EXISTS(doc, '.foo-bar'))

Expand Down
68 changes: 2 additions & 66 deletions pkg/drivers/cdp/dom/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dom

import (
"context"
"github.com/mafredri/cdp/protocol/runtime"
"hash/fnv"

"github.com/mafredri/cdp"
Expand Down Expand Up @@ -31,69 +30,6 @@ type HTMLDocument struct {
element *HTMLElement
}

func LoadRootHTMLDocument(
ctx context.Context,
logger zerolog.Logger,
client *cdp.Client,
domManager *Manager,
mouse *input.Mouse,
keyboard *input.Keyboard,
) (*HTMLDocument, error) {
ftRepl, err := client.Page.GetFrameTree(ctx)

if err != nil {
return nil, err
}

return LoadHTMLDocument(
ctx,
logger,
client,
domManager,
mouse,
keyboard,
ftRepl.FrameTree,
)
}

func LoadHTMLDocument(
ctx context.Context,
logger zerolog.Logger,
client *cdp.Client,
domManager *Manager,
mouse *input.Mouse,
keyboard *input.Keyboard,
frameTree page.FrameTree,
) (*HTMLDocument, error) {
exec, err := eval.Create(ctx, logger, client, frameTree.Frame.ID)

if err != nil {
return nil, err
}

inputManager := input.NewManager(logger, client, exec, keyboard, mouse)

exec.SetLoader(func(ctx context.Context, remoteType eval.RemoteType, id runtime.RemoteObjectID) (core.Value, error) {
return NewHTMLElement(logger, client, domManager, inputManager, exec, id), nil
})

rootElement, err := exec.EvalElement(ctx, templates.GetDocument())

if err != nil {
return nil, errors.Wrap(err, "failed to load root element")
}

return NewHTMLDocument(
logger,
client,
domManager,
inputManager,
exec,
rootElement.(*HTMLElement),
frameTree,
), nil
}

func NewHTMLDocument(
logger zerolog.Logger,
client *cdp.Client,
Expand Down Expand Up @@ -396,8 +332,8 @@ func (doc *HTMLDocument) Scroll(ctx context.Context, options drivers.ScrollOptio
return doc.input.ScrollByXY(ctx, options)
}

func (doc *HTMLDocument) Eval(ctx context.Context, expression string) (core.Value, error) {
return doc.eval.EvalValue(ctx, eval.F(expression))
func (doc *HTMLDocument) Eval() *eval.Runtime {
return doc.eval
}

func (doc *HTMLDocument) logError(err error) *zerolog.Event {
Expand Down
1 change: 0 additions & 1 deletion pkg/drivers/cdp/dom/document_test.go

This file was deleted.

Loading

0 comments on commit 847dda1

Please sign in to comment.