2018-12-22 06:14:41 +02:00
|
|
|
package cdp
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-23 10:33:20 +02:00
|
|
|
"fmt"
|
2019-07-03 20:05:02 +02:00
|
|
|
"hash/fnv"
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
"github.com/mafredri/cdp"
|
|
|
|
"github.com/mafredri/cdp/protocol/dom"
|
|
|
|
"github.com/mafredri/cdp/protocol/page"
|
|
|
|
"github.com/mafredri/cdp/protocol/runtime"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2018-12-22 06:14:41 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
|
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
|
2019-06-20 19:21:48 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/input"
|
2019-03-14 04:50:29 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/templates"
|
2019-02-20 01:10:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/common"
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2018-09-23 10:33:20 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
type HTMLDocument struct {
|
2019-06-19 23:58:56 +02:00
|
|
|
logger *zerolog.Logger
|
|
|
|
client *cdp.Client
|
|
|
|
events *events.EventBroker
|
2019-06-20 19:21:48 +02:00
|
|
|
input *input.Manager
|
2019-06-19 23:58:56 +02:00
|
|
|
exec *eval.ExecutionContext
|
|
|
|
frames page.FrameTree
|
|
|
|
element *HTMLElement
|
|
|
|
parent *HTMLDocument
|
|
|
|
children *common.LazyValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadRootHTMLDocument(
|
2018-09-18 22:42:38 +02:00
|
|
|
ctx context.Context,
|
2019-06-19 23:58:56 +02:00
|
|
|
logger *zerolog.Logger,
|
2018-10-08 03:23:36 +02:00
|
|
|
client *cdp.Client,
|
2019-06-20 19:21:48 +02:00
|
|
|
events *events.EventBroker,
|
|
|
|
mouse *input.Mouse,
|
|
|
|
keyboard *input.Keyboard,
|
2019-06-19 23:58:56 +02:00
|
|
|
) (*HTMLDocument, error) {
|
|
|
|
gdRepl, err := client.DOM.GetDocument(ctx, dom.NewGetDocumentArgs().SetDepth(1))
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-19 23:58:56 +02:00
|
|
|
return nil, err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
ftRepl, err := client.Page.GetFrameTree(ctx)
|
2018-09-25 17:43:58 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-19 23:58:56 +02:00
|
|
|
return nil, err
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 20:05:02 +02:00
|
|
|
worldRepl, err := client.Page.CreateIsolatedWorld(ctx, page.NewCreateIsolatedWorldArgs(ftRepl.FrameTree.Frame.ID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return LoadHTMLDocument(
|
2018-10-11 18:39:03 +02:00
|
|
|
ctx,
|
|
|
|
logger,
|
|
|
|
client,
|
2019-06-20 19:21:48 +02:00
|
|
|
events,
|
|
|
|
mouse,
|
|
|
|
keyboard,
|
2019-06-19 23:58:56 +02:00
|
|
|
gdRepl.Root,
|
|
|
|
ftRepl.FrameTree,
|
2019-07-03 20:05:02 +02:00
|
|
|
worldRepl.ExecutionContextID,
|
2019-06-19 23:58:56 +02:00
|
|
|
nil,
|
2018-10-11 18:39:03 +02:00
|
|
|
)
|
2019-06-19 23:58:56 +02:00
|
|
|
}
|
2018-10-11 18:39:03 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func LoadHTMLDocument(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *zerolog.Logger,
|
|
|
|
client *cdp.Client,
|
2019-06-20 19:21:48 +02:00
|
|
|
events *events.EventBroker,
|
|
|
|
mouse *input.Mouse,
|
|
|
|
keyboard *input.Keyboard,
|
2019-06-19 23:58:56 +02:00
|
|
|
node dom.Node,
|
|
|
|
tree page.FrameTree,
|
|
|
|
execID runtime.ExecutionContextID,
|
|
|
|
parent *HTMLDocument,
|
|
|
|
) (*HTMLDocument, error) {
|
|
|
|
exec := eval.NewExecutionContext(client, tree.Frame, execID)
|
2019-06-20 19:21:48 +02:00
|
|
|
inputManager := input.NewManager(client, exec, keyboard, mouse)
|
2019-06-19 23:58:56 +02:00
|
|
|
|
|
|
|
rootElement, err := LoadHTMLElement(
|
|
|
|
ctx,
|
|
|
|
logger,
|
|
|
|
client,
|
2019-06-20 19:21:48 +02:00
|
|
|
events,
|
|
|
|
inputManager,
|
2019-06-19 23:58:56 +02:00
|
|
|
exec,
|
|
|
|
node.NodeID,
|
|
|
|
)
|
2018-11-22 18:44:05 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if err != nil {
|
2018-10-11 18:39:03 +02:00
|
|
|
return nil, errors.Wrap(err, "failed to load root element")
|
2018-09-25 17:43:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
return NewHTMLDocument(
|
2018-10-11 18:39:03 +02:00
|
|
|
logger,
|
2018-09-28 06:28:33 +02:00
|
|
|
client,
|
2019-06-20 19:21:48 +02:00
|
|
|
events,
|
|
|
|
inputManager,
|
2019-06-19 23:58:56 +02:00
|
|
|
exec,
|
2018-10-11 18:39:03 +02:00
|
|
|
rootElement,
|
2019-06-19 23:58:56 +02:00
|
|
|
tree,
|
|
|
|
parent,
|
2018-09-28 06:28:33 +02:00
|
|
|
), nil
|
2018-09-25 17:43:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func NewHTMLDocument(
|
2018-09-28 06:28:33 +02:00
|
|
|
logger *zerolog.Logger,
|
2018-09-25 23:58:57 +02:00
|
|
|
client *cdp.Client,
|
2019-06-20 19:21:48 +02:00
|
|
|
events *events.EventBroker,
|
|
|
|
input *input.Manager,
|
2019-06-19 23:58:56 +02:00
|
|
|
exec *eval.ExecutionContext,
|
2018-10-11 18:39:03 +02:00
|
|
|
rootElement *HTMLElement,
|
2019-06-19 23:58:56 +02:00
|
|
|
frames page.FrameTree,
|
|
|
|
parent *HTMLDocument,
|
2018-10-05 22:35:08 +02:00
|
|
|
) *HTMLDocument {
|
|
|
|
doc := new(HTMLDocument)
|
2018-09-28 06:28:33 +02:00
|
|
|
doc.logger = logger
|
2018-09-25 23:58:57 +02:00
|
|
|
doc.client = client
|
2019-06-20 19:21:48 +02:00
|
|
|
doc.events = events
|
|
|
|
doc.input = input
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.exec = exec
|
2018-10-11 18:39:03 +02:00
|
|
|
doc.element = rootElement
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.frames = frames
|
|
|
|
doc.parent = parent
|
|
|
|
doc.children = common.NewLazyValue(doc.loadChildren)
|
2018-09-25 23:58:57 +02:00
|
|
|
|
|
|
|
return doc
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (doc *HTMLDocument) MarshalJSON() ([]byte, error) {
|
2018-09-25 23:58:57 +02:00
|
|
|
return doc.element.MarshalJSON()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (doc *HTMLDocument) Type() core.Type {
|
2019-02-20 01:10:18 +02:00
|
|
|
return drivers.HTMLDocumentType
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (doc *HTMLDocument) String() string {
|
2019-06-19 23:58:56 +02:00
|
|
|
return doc.frames.Frame.URL
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (doc *HTMLDocument) Unwrap() interface{} {
|
2018-09-25 23:58:57 +02:00
|
|
|
return doc.element
|
|
|
|
}
|
|
|
|
|
2018-10-05 23:42:28 +02:00
|
|
|
func (doc *HTMLDocument) Hash() uint64 {
|
2018-10-05 21:17:22 +02:00
|
|
|
h := fnv.New64a()
|
2018-09-25 23:58:57 +02:00
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
h.Write([]byte(doc.Type().String()))
|
|
|
|
h.Write([]byte(":"))
|
2019-06-19 23:58:56 +02:00
|
|
|
h.Write([]byte(doc.frames.Frame.ID))
|
|
|
|
h.Write([]byte(doc.frames.Frame.URL))
|
2018-09-25 23:58:57 +02:00
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
return h.Sum64()
|
2018-09-25 23:58:57 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:58:08 +02:00
|
|
|
func (doc *HTMLDocument) Copy() core.Value {
|
2018-09-27 17:53:26 +02:00
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
func (doc *HTMLDocument) Compare(other core.Value) int64 {
|
2019-02-20 01:10:18 +02:00
|
|
|
switch other.Type() {
|
|
|
|
case drivers.HTMLDocumentType:
|
|
|
|
other := other.(drivers.HTMLDocument)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return values.NewString(doc.frames.Frame.URL).Compare(other.GetURL())
|
2019-02-20 01:10:18 +02:00
|
|
|
default:
|
|
|
|
return drivers.Compare(doc.Type(), other.Type())
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (doc *HTMLDocument) Iterate(ctx context.Context) (core.Iterator, error) {
|
|
|
|
return doc.element.Iterate(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (doc *HTMLDocument) GetIn(ctx context.Context, path []core.Value) (core.Value, error) {
|
|
|
|
return common.GetInDocument(ctx, doc, path)
|
|
|
|
}
|
2019-02-13 19:31:18 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (doc *HTMLDocument) SetIn(ctx context.Context, path []core.Value, value core.Value) error {
|
|
|
|
return common.SetInDocument(ctx, doc, path, value)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-09-23 10:33:20 +02:00
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (doc *HTMLDocument) Close() error {
|
2019-06-19 23:58:56 +02:00
|
|
|
errs := make([]error, 0, 5)
|
2018-09-28 06:28:33 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if doc.children.Ready() {
|
|
|
|
val, err := doc.children.Read(context.Background())
|
2018-09-28 06:28:33 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if err == nil {
|
|
|
|
arr := val.(*values.Array)
|
2018-09-28 06:28:33 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
arr.ForEach(func(value core.Value, _ int) bool {
|
|
|
|
doc := value.(drivers.HTMLDocument)
|
2018-09-28 06:28:33 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
err := doc.Close()
|
2018-09-25 23:58:57 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, errors.Wrapf(err, "failed to close nested document: %s", doc.GetURL()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2018-09-28 06:28:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
err := doc.element.Close()
|
2018-09-28 06:28:33 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-19 23:58:56 +02:00
|
|
|
errs = append(errs, err)
|
2018-09-28 06:28:33 +02:00
|
|
|
}
|
2018-09-25 23:58:57 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if len(errs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-25 23:58:57 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return core.Errors(errs...)
|
2018-09-25 23:58:57 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) IsDetached() values.Boolean {
|
|
|
|
return doc.element.IsDetached()
|
2018-09-25 23:58:57 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) GetNodeType() values.Int {
|
|
|
|
return 9
|
|
|
|
}
|
2018-09-25 23:58:57 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) GetNodeName() values.String {
|
|
|
|
return "#document"
|
2018-09-25 23:58:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (doc *HTMLDocument) GetChildNodes(ctx context.Context) core.Value {
|
|
|
|
return doc.element.GetChildNodes(ctx)
|
2018-09-25 23:58:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (doc *HTMLDocument) GetChildNode(ctx context.Context, idx values.Int) core.Value {
|
|
|
|
return doc.element.GetChildNode(ctx, idx)
|
2018-09-25 23:58:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (doc *HTMLDocument) QuerySelector(ctx context.Context, selector values.String) core.Value {
|
|
|
|
return doc.element.QuerySelector(ctx, selector)
|
2018-09-25 23:58:57 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (doc *HTMLDocument) QuerySelectorAll(ctx context.Context, selector values.String) core.Value {
|
|
|
|
return doc.element.QuerySelectorAll(ctx, selector)
|
2018-09-25 23:58:57 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) CountBySelector(ctx context.Context, selector values.String) values.Int {
|
|
|
|
return doc.element.CountBySelector(ctx, selector)
|
2018-09-28 04:03:35 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:22:06 +02:00
|
|
|
func (doc *HTMLDocument) ExistsBySelector(ctx context.Context, selector values.String) (values.Boolean, error) {
|
2019-06-19 23:58:56 +02:00
|
|
|
return doc.element.ExistsBySelector(ctx, selector)
|
2018-09-28 04:03:35 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) GetTitle() values.String {
|
|
|
|
value, err := doc.exec.ReadProperty(context.Background(), doc.element.id.objectID, "title")
|
2019-03-16 01:59:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.logError(errors.Wrap(err, "failed to read document title"))
|
2019-03-16 01:59:05 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return values.EmptyString
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return values.NewString(value.String())
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) GetName() values.String {
|
|
|
|
if doc.frames.Frame.Name != nil {
|
|
|
|
return values.NewString(*doc.frames.Frame.Name)
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return values.EmptyString
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) GetParentDocument() drivers.HTMLDocument {
|
|
|
|
return doc.parent
|
|
|
|
}
|
2019-03-16 01:59:05 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) GetChildDocuments(ctx context.Context) (*values.Array, error) {
|
|
|
|
children, err := doc.children.Read(ctx)
|
2019-03-16 01:59:05 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.NewArray(0), errors.Wrap(err, "failed to load child documents")
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return children.Copy().(*values.Array), nil
|
2019-03-16 01:59:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 20:05:02 +02:00
|
|
|
func (doc *HTMLDocument) XPath(ctx context.Context, expression values.String) (core.Value, error) {
|
|
|
|
return doc.element.XPath(ctx, expression)
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) Length() values.Int {
|
|
|
|
return doc.element.Length()
|
2018-09-28 04:03:35 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) GetElement() drivers.HTMLElement {
|
|
|
|
return doc.element
|
2018-10-13 03:52:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) GetURL() values.String {
|
|
|
|
return values.NewString(doc.frames.Frame.URL)
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:22:06 +02:00
|
|
|
func (doc *HTMLDocument) ClickBySelector(ctx context.Context, selector values.String) error {
|
2019-08-25 02:26:27 +02:00
|
|
|
return doc.element.ClickBySelector(ctx, selector)
|
2018-09-28 04:03:35 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:22:06 +02:00
|
|
|
func (doc *HTMLDocument) ClickBySelectorAll(ctx context.Context, selector values.String) error {
|
2019-08-25 02:26:27 +02:00
|
|
|
return doc.element.ClickBySelectorAll(ctx, selector)
|
2018-09-28 03:41:41 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:22:06 +02:00
|
|
|
func (doc *HTMLDocument) InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) error {
|
|
|
|
return doc.input.TypeBySelector(ctx, doc.element.id.nodeID, selector, value, delay)
|
2018-09-25 17:43:58 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (doc *HTMLDocument) SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error) {
|
2019-06-20 19:21:48 +02:00
|
|
|
return doc.input.SelectBySelector(ctx, doc.element.id.nodeID, selector, value)
|
2018-11-12 21:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 22:13:04 +02:00
|
|
|
func (doc *HTMLDocument) FocusBySelector(ctx context.Context, selector values.String) error {
|
|
|
|
return doc.input.FocusBySelector(ctx, doc.element.id.nodeID, selector)
|
|
|
|
}
|
|
|
|
|
2019-02-24 00:52:01 +02:00
|
|
|
func (doc *HTMLDocument) MoveMouseBySelector(ctx context.Context, selector values.String) error {
|
2019-06-20 19:21:48 +02:00
|
|
|
return doc.input.MoveMouseBySelector(ctx, doc.element.id.nodeID, selector)
|
2018-11-15 21:33:53 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 00:52:01 +02:00
|
|
|
func (doc *HTMLDocument) MoveMouseByXY(ctx context.Context, x, y values.Float) error {
|
2019-07-17 00:17:42 +02:00
|
|
|
return doc.input.MoveMouseByXY(ctx, x, y)
|
2019-02-24 00:52:01 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:52:41 +02:00
|
|
|
func (doc *HTMLDocument) WaitForElement(ctx context.Context, selector values.String, when drivers.WaitEvent) error {
|
2019-03-14 04:50:29 +02:00
|
|
|
var operator string
|
|
|
|
|
|
|
|
if when == drivers.WaitEventPresence {
|
|
|
|
operator = "!="
|
|
|
|
} else {
|
|
|
|
operator = "=="
|
|
|
|
}
|
2019-03-07 04:52:41 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
task := events.NewEvalWaitTask(
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.exec,
|
2019-03-07 04:52:41 +02:00
|
|
|
fmt.Sprintf(
|
|
|
|
`
|
|
|
|
var el = document.querySelector(%s);
|
|
|
|
|
|
|
|
if (el %s null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// null means we need to repeat
|
|
|
|
return null;
|
|
|
|
`,
|
|
|
|
eval.ParamString(selector.String()),
|
2019-03-14 04:50:29 +02:00
|
|
|
operator,
|
2019-03-07 04:52:41 +02:00
|
|
|
),
|
2018-09-25 23:58:57 +02:00
|
|
|
events.DefaultPolling,
|
2018-09-23 10:33:20 +02:00
|
|
|
)
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
_, err := task.Run(ctx)
|
2018-09-23 10:33:20 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2018-09-25 17:43:58 +02:00
|
|
|
|
2019-03-07 04:52:41 +02:00
|
|
|
func (doc *HTMLDocument) WaitForClassBySelector(ctx context.Context, selector, class values.String, when drivers.WaitEvent) error {
|
2018-10-07 04:33:39 +02:00
|
|
|
task := events.NewEvalWaitTask(
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.exec,
|
2019-03-14 04:50:29 +02:00
|
|
|
templates.WaitBySelector(
|
|
|
|
selector,
|
|
|
|
when,
|
|
|
|
class,
|
|
|
|
fmt.Sprintf("el.className.split(' ').find(i => i === %s)", eval.ParamString(class.String())),
|
2018-10-07 04:33:39 +02:00
|
|
|
),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
_, err := task.Run(ctx)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:52:41 +02:00
|
|
|
func (doc *HTMLDocument) WaitForClassBySelectorAll(ctx context.Context, selector, class values.String, when drivers.WaitEvent) error {
|
2018-10-07 04:33:39 +02:00
|
|
|
task := events.NewEvalWaitTask(
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.exec,
|
2019-03-14 04:50:29 +02:00
|
|
|
templates.WaitBySelectorAll(
|
|
|
|
selector,
|
|
|
|
when,
|
|
|
|
class,
|
|
|
|
fmt.Sprintf("el.className.split(' ').find(i => i === %s)", eval.ParamString(class.String())),
|
2018-10-07 04:33:39 +02:00
|
|
|
),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
_, err := task.Run(ctx)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-14 04:50:29 +02:00
|
|
|
func (doc *HTMLDocument) WaitForAttributeBySelector(
|
|
|
|
ctx context.Context,
|
|
|
|
selector,
|
|
|
|
name values.String,
|
|
|
|
value core.Value,
|
|
|
|
when drivers.WaitEvent,
|
|
|
|
) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.exec,
|
2019-03-14 04:50:29 +02:00
|
|
|
templates.WaitBySelector(
|
|
|
|
selector,
|
|
|
|
when,
|
|
|
|
value,
|
|
|
|
templates.AttributeRead(name),
|
|
|
|
),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (doc *HTMLDocument) WaitForAttributeBySelectorAll(
|
|
|
|
ctx context.Context,
|
|
|
|
selector,
|
|
|
|
name values.String,
|
|
|
|
value core.Value,
|
|
|
|
when drivers.WaitEvent,
|
|
|
|
) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.exec,
|
2019-03-14 04:50:29 +02:00
|
|
|
templates.WaitBySelectorAll(
|
|
|
|
selector,
|
|
|
|
when,
|
|
|
|
value,
|
|
|
|
templates.AttributeRead(name),
|
|
|
|
),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-15 04:10:15 +02:00
|
|
|
func (doc *HTMLDocument) WaitForStyleBySelector(ctx context.Context, selector, name values.String, value core.Value, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.exec,
|
2019-03-15 04:10:15 +02:00
|
|
|
templates.WaitBySelector(
|
|
|
|
selector,
|
|
|
|
when,
|
|
|
|
value,
|
|
|
|
templates.StyleRead(name),
|
|
|
|
),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (doc *HTMLDocument) WaitForStyleBySelectorAll(ctx context.Context, selector, name values.String, value core.Value, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
2019-06-19 23:58:56 +02:00
|
|
|
doc.exec,
|
2019-03-15 04:10:15 +02:00
|
|
|
templates.WaitBySelectorAll(
|
|
|
|
selector,
|
|
|
|
when,
|
|
|
|
value,
|
|
|
|
templates.StyleRead(name),
|
|
|
|
),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (doc *HTMLDocument) ScrollTop(ctx context.Context) error {
|
2019-06-20 19:21:48 +02:00
|
|
|
return doc.input.ScrollTop(ctx)
|
2018-11-13 05:26:02 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (doc *HTMLDocument) ScrollBottom(ctx context.Context) error {
|
2019-06-20 19:21:48 +02:00
|
|
|
return doc.input.ScrollBottom(ctx)
|
2018-11-13 05:26:02 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (doc *HTMLDocument) ScrollBySelector(ctx context.Context, selector values.String) error {
|
2019-06-20 19:21:48 +02:00
|
|
|
return doc.input.ScrollIntoViewBySelector(ctx, selector)
|
2018-11-15 21:33:53 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 00:52:01 +02:00
|
|
|
func (doc *HTMLDocument) ScrollByXY(ctx context.Context, x, y values.Float) error {
|
2019-07-17 00:17:42 +02:00
|
|
|
return doc.input.ScrollByXY(ctx, x, y)
|
2019-02-24 00:52:01 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) loadChildren(ctx context.Context) (value core.Value, e error) {
|
|
|
|
children := values.NewArray(len(doc.frames.ChildFrames))
|
2018-10-08 02:15:41 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if len(doc.frames.ChildFrames) > 0 {
|
|
|
|
for _, cf := range doc.frames.ChildFrames {
|
|
|
|
cfNode, cfExecID, err := resolveFrame(ctx, doc.client, cf.Frame)
|
2018-10-08 02:15:41 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to resolve frame node")
|
|
|
|
}
|
2018-10-08 02:15:41 +02:00
|
|
|
|
2019-06-20 19:21:48 +02:00
|
|
|
cfDocument, err := LoadHTMLDocument(
|
|
|
|
ctx,
|
|
|
|
doc.logger,
|
|
|
|
doc.client,
|
|
|
|
doc.events,
|
|
|
|
doc.input.Mouse(),
|
|
|
|
doc.input.Keyboard(),
|
|
|
|
cfNode,
|
|
|
|
cf,
|
|
|
|
cfExecID,
|
|
|
|
doc,
|
|
|
|
)
|
2018-10-11 18:39:03 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to load frame document")
|
|
|
|
}
|
2018-10-11 18:39:03 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
children.Push(cfDocument)
|
|
|
|
}
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return children, nil
|
2018-10-08 02:15:41 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (doc *HTMLDocument) logError(err error) *zerolog.Event {
|
|
|
|
return doc.logger.
|
|
|
|
Error().
|
2018-10-08 02:15:41 +02:00
|
|
|
Timestamp().
|
2019-06-19 23:58:56 +02:00
|
|
|
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)).
|
|
|
|
Err(err)
|
2018-09-26 01:04:07 +02:00
|
|
|
}
|