Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/#399 navigation #432

Merged
merged 33 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0a28131
Refactored networking
ziflex Oct 16, 2019
23a1d5c
Merge branch 'master' into bugfix/#399-navigation
ziflex Oct 30, 2019
4dac762
Some work
ziflex Nov 5, 2019
94b3ef2
Merge branch 'master' into bugfix/#399-navigation
ziflex Nov 5, 2019
c00f1f9
Added event loop
ziflex Nov 15, 2019
54968bd
Renamed EventHandler to Handler
ziflex Nov 15, 2019
4e2d42c
wip
ziflex Nov 15, 2019
e83d117
Added new wait for navigation logic
ziflex Nov 24, 2019
7edbad4
Removed console logs
ziflex Nov 24, 2019
1fb22ec
Added DOMManager
ziflex Nov 27, 2019
1317f27
Refactored frame managment
ziflex Nov 28, 2019
bf2520e
Fixes
ziflex Nov 28, 2019
2e33d45
Fixed concurrency issues
ziflex Dec 1, 2019
7c880f1
Fixed unit tests
ziflex Dec 1, 2019
060e69a
Improved EventLoop api
ziflex Dec 2, 2019
ea2bd5f
Some fixes
ziflex Dec 13, 2019
9cd03d1
Refactored event loop.
ziflex Dec 14, 2019
72a75d0
Improved logic of initial page load
ziflex Dec 14, 2019
aa62c70
Cleaned up
ziflex Dec 14, 2019
93c8c97
Fixed linting issues
ziflex Dec 14, 2019
670ee34
Fixed dom.Manager.Close
ziflex Dec 14, 2019
990b4c6
SOme works
ziflex Dec 18, 2019
41d348e
Fixes
ziflex Dec 18, 2019
d39ced1
Removed fmt.Println statements
ziflex Dec 21, 2019
d1f421b
Merge branch 'master' into bugfix/#399-navigation
ziflex Dec 21, 2019
501e851
Refactored WaitForNavigation
ziflex Dec 21, 2019
c097fba
Removed filter for e2e tests
ziflex Dec 21, 2019
6076221
Made Cookies Measurable
ziflex Dec 21, 2019
e5dcc73
Made Cookies KeyedCollection
ziflex Dec 21, 2019
dec2136
Fixes after code review
ziflex Dec 23, 2019
5e29e57
Updated e2e tests for iframes
ziflex Dec 23, 2019
9c12351
Fixed iframe lookup in e2e tests
ziflex Dec 24, 2019
f64f7ef
Added comments
ziflex Dec 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ jobs:
- stage: e2e
go: stable
before_script:
- docker pull microbox/chromium-headless:75.0.3765.1
- docker run -d -p 9222:9222 microbox/chromium-headless:75.0.3765.1
- docker pull microbox/chromium-headless:77.0.3844.0
- docker run -d -p 9222:9222 microbox/chromium-headless:77.0.3844.0
- docker ps
script:
- make e2e
Expand Down
13 changes: 13 additions & 0 deletions examples/redirects.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
LET doc = DOCUMENT("http://waos.ovh/redirect.html", {
driver: 'cdp',
viewport: {
width: 1920,
height: 1080
}
})

CLICK(doc, '.click')

WAIT_NAVIGATION(doc)

RETURN ELEMENT(doc, '.title')
1 change: 0 additions & 1 deletion pkg/drivers/cdp/document_test.go

This file was deleted.

165 changes: 52 additions & 113 deletions pkg/drivers/cdp/document.go → pkg/drivers/cdp/dom/document.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cdp
package dom

import (
"context"
Expand All @@ -23,22 +23,20 @@ import (
)

type HTMLDocument struct {
logger *zerolog.Logger
client *cdp.Client
events *events.EventBroker
input *input.Manager
exec *eval.ExecutionContext
frames page.FrameTree
element *HTMLElement
parent *HTMLDocument
children *common.LazyValue
logger *zerolog.Logger
client *cdp.Client
dom *Manager
input *input.Manager
exec *eval.ExecutionContext
frameTree page.FrameTree
element *HTMLElement
}

func LoadRootHTMLDocument(
ctx context.Context,
logger *zerolog.Logger,
client *cdp.Client,
events *events.EventBroker,
domManager *Manager,
mouse *input.Mouse,
keyboard *input.Keyboard,
) (*HTMLDocument, error) {
Expand All @@ -64,36 +62,34 @@ func LoadRootHTMLDocument(
ctx,
logger,
client,
events,
domManager,
mouse,
keyboard,
gdRepl.Root,
ftRepl.FrameTree,
worldRepl.ExecutionContextID,
nil,
)
}

func LoadHTMLDocument(
ctx context.Context,
logger *zerolog.Logger,
client *cdp.Client,
events *events.EventBroker,
domManager *Manager,
mouse *input.Mouse,
keyboard *input.Keyboard,
node dom.Node,
tree page.FrameTree,
frameTree page.FrameTree,
execID runtime.ExecutionContextID,
parent *HTMLDocument,
) (*HTMLDocument, error) {
exec := eval.NewExecutionContext(client, tree.Frame, execID)
exec := eval.NewExecutionContext(client, frameTree.Frame, execID)
inputManager := input.NewManager(client, exec, keyboard, mouse)

rootElement, err := LoadHTMLElement(
ctx,
logger,
client,
events,
domManager,
inputManager,
exec,
node.NodeID,
Expand All @@ -106,35 +102,31 @@ func LoadHTMLDocument(
return NewHTMLDocument(
logger,
client,
events,
domManager,
inputManager,
exec,
rootElement,
tree,
parent,
frameTree,
), nil
}

func NewHTMLDocument(
logger *zerolog.Logger,
client *cdp.Client,
events *events.EventBroker,
domManager *Manager,
input *input.Manager,
exec *eval.ExecutionContext,
rootElement *HTMLElement,
frames page.FrameTree,
parent *HTMLDocument,
) *HTMLDocument {
doc := new(HTMLDocument)
doc.logger = logger
doc.client = client
doc.events = events
doc.dom = domManager
doc.input = input
doc.exec = exec
doc.element = rootElement
doc.frames = frames
doc.parent = parent
doc.children = common.NewLazyValue(doc.loadChildren)
doc.frameTree = frames

return doc
}
Expand All @@ -148,7 +140,7 @@ func (doc *HTMLDocument) Type() core.Type {
}

func (doc *HTMLDocument) String() string {
return doc.frames.Frame.URL
return doc.frameTree.Frame.URL
}

func (doc *HTMLDocument) Unwrap() interface{} {
Expand All @@ -160,8 +152,8 @@ func (doc *HTMLDocument) Hash() uint64 {

h.Write([]byte(doc.Type().String()))
h.Write([]byte(":"))
h.Write([]byte(doc.frames.Frame.ID))
h.Write([]byte(doc.frames.Frame.URL))
h.Write([]byte(doc.frameTree.Frame.ID))
h.Write([]byte(doc.frameTree.Frame.URL))

return h.Sum64()
}
Expand All @@ -175,7 +167,7 @@ func (doc *HTMLDocument) Compare(other core.Value) int64 {
case drivers.HTMLDocumentType:
other := other.(drivers.HTMLDocument)

return values.NewString(doc.frames.Frame.URL).Compare(other.GetURL())
return values.NewString(doc.frameTree.Frame.URL).Compare(other.GetURL())
default:
return drivers.Compare(doc.Type(), other.Type())
}
Expand All @@ -194,41 +186,11 @@ func (doc *HTMLDocument) SetIn(ctx context.Context, path []core.Value, value cor
}

func (doc *HTMLDocument) Close() error {
errs := make([]error, 0, 5)

if doc.children.Ready() {
val, err := doc.children.Read(context.Background())

if err == nil {
arr := val.(*values.Array)

arr.ForEach(func(value core.Value, _ int) bool {
doc := value.(drivers.HTMLDocument)

err := doc.Close()

if err != nil {
errs = append(errs, errors.Wrapf(err, "failed to close nested document: %s", doc.GetURL()))
}

return true
})
} else {
errs = append(errs, err)
}
}

err := doc.element.Close()

if err != nil {
errs = append(errs, err)
}

if len(errs) == 0 {
return nil
}
return doc.element.Close()
}

return core.Errors(errs...)
func (doc *HTMLDocument) Frame() page.FrameTree {
return doc.frameTree
}

func (doc *HTMLDocument) IsDetached() values.Boolean {
Expand Down Expand Up @@ -280,25 +242,37 @@ func (doc *HTMLDocument) GetTitle() values.String {
}

func (doc *HTMLDocument) GetName() values.String {
if doc.frames.Frame.Name != nil {
return values.NewString(*doc.frames.Frame.Name)
if doc.frameTree.Frame.Name != nil {
return values.NewString(*doc.frameTree.Frame.Name)
}

return values.EmptyString
}

func (doc *HTMLDocument) GetParentDocument() drivers.HTMLDocument {
return doc.parent
func (doc *HTMLDocument) GetParentDocument(ctx context.Context) (drivers.HTMLDocument, error) {
if doc.frameTree.Frame.ParentID == nil {
return nil, nil
}

return doc.dom.GetFrameNode(ctx, *doc.frameTree.Frame.ParentID)
}

func (doc *HTMLDocument) GetChildDocuments(ctx context.Context) (*values.Array, error) {
children, err := doc.children.Read(ctx)
arr := values.NewArray(len(doc.frameTree.ChildFrames))

if err != nil {
return values.NewArray(0), errors.Wrap(err, "failed to load child documents")
for _, childFrame := range doc.frameTree.ChildFrames {
frame, err := doc.dom.GetFrameNode(ctx, childFrame.Frame.ID)

if err != nil {
return nil, err
}

if frame != nil {
arr.Push(frame)
}
}

return children.Copy().(*values.Array), nil
return arr, nil
}

func (doc *HTMLDocument) XPath(ctx context.Context, expression values.String) (core.Value, error) {
Expand All @@ -314,7 +288,7 @@ func (doc *HTMLDocument) GetElement() drivers.HTMLElement {
}

func (doc *HTMLDocument) GetURL() values.String {
return values.NewString(doc.frames.Frame.URL)
return values.NewString(doc.frameTree.Frame.URL)
}

func (doc *HTMLDocument) MoveMouseByXY(ctx context.Context, x, y values.Float) error {
Expand Down Expand Up @@ -484,48 +458,13 @@ func (doc *HTMLDocument) ScrollByXY(ctx context.Context, x, y values.Float) erro
return doc.input.ScrollByXY(ctx, float64(x), float64(y))
}

func (doc *HTMLDocument) loadChildren(ctx context.Context) (value core.Value, e error) {
children := values.NewArray(len(doc.frames.ChildFrames))

if len(doc.frames.ChildFrames) > 0 {
for _, cf := range doc.frames.ChildFrames {
cfNode, cfExecID, err := resolveFrame(ctx, doc.client, cf.Frame)

if err != nil {
return nil, errors.Wrap(err, "failed to resolve frame node")
}

cfDocument, err := LoadHTMLDocument(
ctx,
doc.logger,
doc.client,
doc.events,
doc.input.Mouse(),
doc.input.Keyboard(),
cfNode,
cf,
cfExecID,
doc,
)

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

children.Push(cfDocument)
}
}

return children, nil
}

func (doc *HTMLDocument) logError(err error) *zerolog.Event {
return doc.logger.
Error().
Timestamp().
Str("url", string(doc.frames.Frame.URL)).
Str("securityOrigin", string(doc.frames.Frame.SecurityOrigin)).
Str("mimeType", string(doc.frames.Frame.MimeType)).
Str("frameID", string(doc.frames.Frame.ID)).
Str("url", doc.frameTree.Frame.URL).
Str("securityOrigin", doc.frameTree.Frame.SecurityOrigin).
Str("mimeType", doc.frameTree.Frame.MimeType).
Str("frameID", string(doc.frameTree.Frame.ID)).
Err(err)
}
1 change: 1 addition & 0 deletions pkg/drivers/cdp/dom/document_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package dom
Loading