2019-12-24 18:47:21 -05:00
|
|
|
package dom
|
2018-09-18 16:42:38 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2018-10-11 12:39:03 -04:00
|
|
|
"fmt"
|
2018-10-08 20:07:08 -07:00
|
|
|
"hash/fnv"
|
|
|
|
"strings"
|
2019-09-05 12:17:22 -04:00
|
|
|
"time"
|
2018-10-08 20:07:08 -07:00
|
|
|
|
2019-09-07 13:12:19 -04:00
|
|
|
"github.com/mafredri/cdp"
|
|
|
|
"github.com/mafredri/cdp/protocol/dom"
|
|
|
|
"github.com/mafredri/cdp/protocol/runtime"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
2020-11-23 20:12:04 -05:00
|
|
|
"github.com/wI2L/jettison"
|
2019-09-07 13:12:19 -04:00
|
|
|
|
2019-02-19 18:10:18 -05:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2018-12-21 23:14:41 -05:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
|
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
|
2019-06-20 13:21:48 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/input"
|
2019-07-11 17:16:34 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/templates"
|
2018-12-21 23:14:41 -05:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/common"
|
2018-09-18 16:42:38 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2021-09-07 16:33:30 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/logging"
|
2018-09-18 16:42:38 -04:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
type HTMLElement struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
client *cdp.Client
|
|
|
|
dom *Manager
|
|
|
|
input *input.Manager
|
|
|
|
exec *eval.Runtime
|
|
|
|
id runtime.RemoteObjectID
|
|
|
|
nodeType *common.LazyValue
|
|
|
|
nodeName *common.LazyValue
|
|
|
|
}
|
2018-09-18 16:42:38 -04:00
|
|
|
|
2019-06-19 17:58:56 -04:00
|
|
|
func LoadHTMLElement(
|
2018-10-11 12:39:03 -04:00
|
|
|
ctx context.Context,
|
2021-09-02 11:09:48 -04:00
|
|
|
logger zerolog.Logger,
|
2018-09-18 16:42:38 -04:00
|
|
|
client *cdp.Client,
|
2019-12-24 18:47:21 -05:00
|
|
|
domManager *Manager,
|
2019-06-20 13:21:48 -04:00
|
|
|
input *input.Manager,
|
2021-09-02 11:09:48 -04:00
|
|
|
exec *eval.Runtime,
|
2018-10-11 12:39:03 -04:00
|
|
|
nodeID dom.NodeID,
|
2018-10-05 16:35:08 -04:00
|
|
|
) (*HTMLElement, error) {
|
2018-09-18 16:42:38 -04:00
|
|
|
if client == nil {
|
|
|
|
return nil, core.Error(core.ErrMissedArgument, "client")
|
|
|
|
}
|
|
|
|
|
2018-10-11 12:39:03 -04:00
|
|
|
// getting a remote object that represents the current DOM Node
|
2021-09-02 11:09:48 -04:00
|
|
|
args := dom.NewResolveNodeArgs().SetNodeID(nodeID).SetExecutionContextID(exec.ContextID())
|
2018-10-11 12:39:03 -04:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
ref, err := client.DOM.ResolveNode(ctx, args)
|
2018-09-18 16:42:38 -04:00
|
|
|
|
2018-10-11 12:39:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
if ref.Object.ObjectID == nil {
|
|
|
|
return nil, core.Error(core.ErrNotFound, fmt.Sprintf("element %s", ref.Object.Value))
|
2018-10-11 12:39:03 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return ResolveHTMLElement(
|
2019-07-03 14:05:02 -04:00
|
|
|
logger,
|
|
|
|
client,
|
2019-12-24 18:47:21 -05:00
|
|
|
domManager,
|
2019-07-03 14:05:02 -04:00
|
|
|
input,
|
|
|
|
exec,
|
2021-09-07 16:33:30 -04:00
|
|
|
ref.Object,
|
2019-07-03 14:05:02 -04:00
|
|
|
)
|
|
|
|
}
|
2018-09-18 16:42:38 -04:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
func ResolveHTMLElement(
|
2021-09-02 11:09:48 -04:00
|
|
|
logger zerolog.Logger,
|
2019-07-03 14:05:02 -04:00
|
|
|
client *cdp.Client,
|
2019-12-24 18:47:21 -05:00
|
|
|
domManager *Manager,
|
2019-07-03 14:05:02 -04:00
|
|
|
input *input.Manager,
|
2021-09-02 11:09:48 -04:00
|
|
|
exec *eval.Runtime,
|
2021-09-07 16:33:30 -04:00
|
|
|
ref runtime.RemoteObject,
|
2019-07-03 14:05:02 -04:00
|
|
|
) (*HTMLElement, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
if ref.ObjectID == nil {
|
|
|
|
return nil, core.Error(core.ErrNotFound, fmt.Sprintf("element %s", ref.Value))
|
2018-09-26 22:03:06 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
id := *ref.ObjectID
|
|
|
|
|
2018-10-05 16:35:08 -04:00
|
|
|
return NewHTMLElement(
|
2018-09-28 00:28:33 -04:00
|
|
|
logger,
|
2018-09-26 22:03:06 -04:00
|
|
|
client,
|
2019-12-24 18:47:21 -05:00
|
|
|
domManager,
|
2019-06-20 13:21:48 -04:00
|
|
|
input,
|
2019-06-19 17:58:56 -04:00
|
|
|
exec,
|
2018-09-26 22:03:06 -04:00
|
|
|
id,
|
|
|
|
), nil
|
2018-09-25 11:43:58 -04:00
|
|
|
}
|
|
|
|
|
2018-10-05 16:35:08 -04:00
|
|
|
func NewHTMLElement(
|
2021-09-02 11:09:48 -04:00
|
|
|
logger zerolog.Logger,
|
2018-09-25 11:43:58 -04:00
|
|
|
client *cdp.Client,
|
2019-12-24 18:47:21 -05:00
|
|
|
domManager *Manager,
|
2019-06-20 13:21:48 -04:00
|
|
|
input *input.Manager,
|
2021-09-02 11:09:48 -04:00
|
|
|
exec *eval.Runtime,
|
2021-09-07 16:33:30 -04:00
|
|
|
id runtime.RemoteObjectID,
|
2018-10-05 16:35:08 -04:00
|
|
|
) *HTMLElement {
|
|
|
|
el := new(HTMLElement)
|
2021-09-02 11:09:48 -04:00
|
|
|
el.logger = logging.
|
|
|
|
WithName(logger.With(), "dom_element").
|
2021-09-07 16:33:30 -04:00
|
|
|
Str("object_id", string(id)).
|
2021-09-02 11:09:48 -04:00
|
|
|
Logger()
|
2018-09-25 11:43:58 -04:00
|
|
|
el.client = client
|
2019-12-24 18:47:21 -05:00
|
|
|
el.dom = domManager
|
2019-06-20 13:21:48 -04:00
|
|
|
el.input = input
|
2019-06-19 17:58:56 -04:00
|
|
|
el.exec = exec
|
2018-09-25 11:43:58 -04:00
|
|
|
el.id = id
|
2021-09-07 16:33:30 -04:00
|
|
|
el.nodeType = common.NewLazyValue(func(ctx context.Context) (core.Value, error) {
|
|
|
|
return el.exec.EvalValue(ctx, templates.GetNodeType(el.id))
|
|
|
|
})
|
|
|
|
el.nodeName = common.NewLazyValue(func(ctx context.Context) (core.Value, error) {
|
|
|
|
return el.exec.EvalValue(ctx, templates.GetNodeName(el.id))
|
|
|
|
})
|
2018-09-25 11:43:58 -04:00
|
|
|
|
|
|
|
return el
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2018-10-05 16:35:08 -04:00
|
|
|
func (el *HTMLElement) Close() error {
|
2018-09-18 16:42:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:35:08 -04:00
|
|
|
func (el *HTMLElement) Type() core.Type {
|
2019-02-19 18:10:18 -05:00
|
|
|
return drivers.HTMLElementType
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2018-10-05 16:35:08 -04:00
|
|
|
func (el *HTMLElement) MarshalJSON() ([]byte, error) {
|
2020-11-23 20:12:04 -05:00
|
|
|
return jettison.MarshalOpts(el.String(), jettison.NoHTMLEscaping())
|
2019-07-11 17:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) String() string {
|
2019-09-05 12:17:22 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(drivers.DefaultWaitTimeout)*time.Millisecond)
|
2019-07-11 17:16:34 -04:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := el.GetInnerHTML(ctx)
|
2018-09-27 19:05:56 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2019-07-11 17:16:34 -04:00
|
|
|
el.logError(errors.Wrap(err, "HTMLElement.String"))
|
2018-09-27 19:05:56 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
return ""
|
|
|
|
}
|
2018-09-18 16:42:38 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
return res.String()
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-02-13 12:31:18 -05:00
|
|
|
func (el *HTMLElement) Compare(other core.Value) int64 {
|
2019-02-19 18:10:18 -05:00
|
|
|
switch other.Type() {
|
|
|
|
case drivers.HTMLElementType:
|
|
|
|
other := other.(drivers.HTMLElement)
|
2018-09-18 16:42:38 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
return int64(strings.Compare(el.String(), other.String()))
|
2019-02-19 18:10:18 -05:00
|
|
|
default:
|
|
|
|
return drivers.Compare(el.Type(), other.Type())
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:35:08 -04:00
|
|
|
func (el *HTMLElement) Unwrap() interface{} {
|
2018-09-25 11:43:58 -04:00
|
|
|
return el
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2018-10-05 17:42:28 -04:00
|
|
|
func (el *HTMLElement) Hash() uint64 {
|
2018-10-05 15:17:22 -04:00
|
|
|
h := fnv.New64a()
|
2018-09-28 00:28:33 -04:00
|
|
|
|
2018-10-05 15:17:22 -04:00
|
|
|
h.Write([]byte(el.Type().String()))
|
|
|
|
h.Write([]byte(":"))
|
2021-09-07 16:33:30 -04:00
|
|
|
h.Write([]byte(el.id))
|
2018-09-18 16:42:38 -04:00
|
|
|
|
2018-10-05 15:17:22 -04:00
|
|
|
return h.Sum64()
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-02-19 18:10:18 -05:00
|
|
|
func (el *HTMLElement) Copy() core.Value {
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) Iterate(_ context.Context) (core.Iterator, error) {
|
|
|
|
return common.NewIterator(el)
|
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
func (el *HTMLElement) GetIn(ctx context.Context, path []core.Value) (core.Value, core.PathError) {
|
|
|
|
return common.GetInElement(ctx, path, el)
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
func (el *HTMLElement) SetIn(ctx context.Context, path []core.Value, value core.Value) core.PathError {
|
|
|
|
return common.SetInElement(ctx, path, el, value)
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
func (el *HTMLElement) GetValue(ctx context.Context) (core.Value, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.EvalValue(ctx, templates.GetValue(el.id))
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:24:05 -05:00
|
|
|
func (el *HTMLElement) SetValue(ctx context.Context, value core.Value) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.SetValue(el.id, value))
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
func (el *HTMLElement) GetNodeType(ctx context.Context) (values.Int, error) {
|
|
|
|
out, err := el.nodeType.Read(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.ZeroInt, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.ToInt(out), nil
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
func (el *HTMLElement) GetNodeName(ctx context.Context) (values.String, error) {
|
|
|
|
out, err := el.nodeName.Read(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.ToString(out), nil
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-02-19 18:10:18 -05:00
|
|
|
func (el *HTMLElement) Length() values.Int {
|
2021-09-07 16:33:30 -04:00
|
|
|
value, err := el.exec.EvalValue(context.Background(), templates.GetChildrenCount(el.id))
|
2019-02-19 18:10:18 -05:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
if err != nil {
|
|
|
|
el.logError(err)
|
|
|
|
|
|
|
|
return 0
|
2020-07-09 21:25:58 -04:00
|
|
|
}
|
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
return values.ToInt(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) GetStyles(ctx context.Context) (*values.Object, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.GetStyles(el.id))
|
2019-03-13 14:51:30 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.NewObject(), err
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToObject(ctx, out), nil
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) GetStyle(ctx context.Context, name values.String) (core.Value, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.EvalValue(ctx, templates.GetStyle(el.id, name))
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) SetStyles(ctx context.Context, styles *values.Object) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.SetStyles(el.id, styles))
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
2020-11-20 20:09:21 -05:00
|
|
|
func (el *HTMLElement) SetStyle(ctx context.Context, name, value values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.SetStyle(el.id, name, value))
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) RemoveStyle(ctx context.Context, names ...values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.RemoveStyles(el.id, names))
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
func (el *HTMLElement) GetAttributes(ctx context.Context) (*values.Object, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.GetAttributes(el.id))
|
2018-09-26 22:03:06 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-06 21:02:41 -04:00
|
|
|
return values.NewObject(), err
|
2018-09-26 22:03:06 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToObject(ctx, out), nil
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
func (el *HTMLElement) GetAttribute(ctx context.Context, name values.String) (core.Value, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.EvalValue(ctx, templates.GetAttribute(el.id, name))
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-03-13 14:51:30 -04:00
|
|
|
func (el *HTMLElement) SetAttributes(ctx context.Context, attrs *values.Object) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.SetAttributes(el.id, attrs))
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:24:05 -05:00
|
|
|
func (el *HTMLElement) SetAttribute(ctx context.Context, name, value values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.SetAttribute(el.id, name, value))
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
2019-03-13 14:51:30 -04:00
|
|
|
func (el *HTMLElement) RemoveAttribute(ctx context.Context, names ...values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.RemoveAttributes(el.id, names))
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
func (el *HTMLElement) GetChildNodes(ctx context.Context) (*values.Array, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.evalTo(ctx, templates.GetChildren(el.id))
|
2020-12-19 13:42:57 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyArray(), err
|
2020-07-09 21:25:58 -04:00
|
|
|
}
|
2018-09-26 22:03:06 -04:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToArray(ctx, out), nil
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
func (el *HTMLElement) GetChildNode(ctx context.Context, idx values.Int) (core.Value, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.evalToElement(ctx, templates.GetChildByIndex(el.id, idx))
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2020-11-20 23:09:12 -05:00
|
|
|
func (el *HTMLElement) GetParentElement(ctx context.Context) (core.Value, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.evalToElement(ctx, templates.GetParent(el.id))
|
2020-11-20 23:09:12 -05:00
|
|
|
}
|
|
|
|
|
2020-11-13 21:48:00 -05:00
|
|
|
func (el *HTMLElement) GetPreviousElementSibling(ctx context.Context) (core.Value, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.evalToElement(ctx, templates.GetPreviousElementSibling(el.id))
|
2020-11-13 21:48:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) GetNextElementSibling(ctx context.Context) (core.Value, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.evalToElement(ctx, templates.GetNextElementSibling(el.id))
|
2020-11-13 21:48:00 -05:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
func (el *HTMLElement) QuerySelector(ctx context.Context, selector values.String) (core.Value, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.evalToElement(ctx, templates.QuerySelector(el.id, selector))
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
func (el *HTMLElement) QuerySelectorAll(ctx context.Context, selector values.String) (*values.Array, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalRef(ctx, templates.QuerySelectorAll(el.id, selector))
|
2018-09-18 16:42:38 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-19 13:42:57 -05:00
|
|
|
return values.EmptyArray(), err
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
res, err := el.fromEvalRef(ctx, out)
|
2018-09-18 16:42:38 -04:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return values.EmptyArray(), err
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToArray(ctx, res), nil
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-07-03 14:05:02 -04:00
|
|
|
func (el *HTMLElement) XPath(ctx context.Context, expression values.String) (result core.Value, err error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalRef(ctx, templates.XPath(el.id, expression))
|
2019-07-03 14:05:02 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.fromEvalRef(ctx, out)
|
2019-07-03 14:05:02 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) GetInnerText(ctx context.Context) (values.String, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.GetInnerText(el.id))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.ToString(out), nil
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) SetInnerText(ctx context.Context, innerText values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(
|
|
|
|
ctx,
|
|
|
|
templates.SetInnerText(el.id, innerText),
|
|
|
|
)
|
2019-07-11 17:16:34 -04:00
|
|
|
}
|
2018-10-08 20:20:40 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) GetInnerTextBySelector(ctx context.Context, selector values.String) (values.String, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.GetInnerTextBySelector(el.id, selector))
|
2018-10-11 12:39:03 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
2018-10-11 12:39:03 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToString(out), nil
|
2019-07-11 17:16:34 -04:00
|
|
|
}
|
2018-10-11 12:39:03 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) SetInnerTextBySelector(ctx context.Context, selector, innerText values.String) error {
|
2021-09-02 11:09:48 -04:00
|
|
|
return el.exec.Eval(
|
2019-08-05 19:57:02 -04:00
|
|
|
ctx,
|
2021-09-07 16:33:30 -04:00
|
|
|
templates.SetInnerTextBySelector(el.id, selector, innerText),
|
2019-08-05 19:57:02 -04:00
|
|
|
)
|
2019-07-11 17:16:34 -04:00
|
|
|
}
|
2018-10-11 12:39:03 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) GetInnerTextBySelectorAll(ctx context.Context, selector values.String) (*values.Array, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.GetInnerTextBySelectorAll(el.id, selector))
|
2018-10-11 12:39:03 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-19 13:42:57 -05:00
|
|
|
return values.EmptyArray(), err
|
2019-07-11 17:16:34 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToArray(ctx, out), nil
|
2018-10-08 20:20:40 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) GetInnerHTML(ctx context.Context) (values.String, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.GetInnerHTML(el.id))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.ToString(out), nil
|
2018-09-18 16:42:38 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) SetInnerHTML(ctx context.Context, innerHTML values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.SetInnerHTML(el.id, innerHTML))
|
2019-07-11 17:16:34 -04:00
|
|
|
}
|
2018-10-08 20:20:40 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) GetInnerHTMLBySelector(ctx context.Context, selector values.String) (values.String, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.GetInnerHTMLBySelector(el.id, selector))
|
2018-10-08 20:20:40 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2019-07-11 17:16:34 -04:00
|
|
|
return values.EmptyString, err
|
2018-10-08 20:20:40 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToString(out), nil
|
2018-10-08 20:20:40 -04:00
|
|
|
}
|
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) SetInnerHTMLBySelector(ctx context.Context, selector, innerHTML values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.exec.Eval(ctx, templates.SetInnerHTMLBySelector(el.id, selector, innerHTML))
|
2019-07-11 17:16:34 -04:00
|
|
|
}
|
2018-10-08 20:20:40 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
func (el *HTMLElement) GetInnerHTMLBySelectorAll(ctx context.Context, selector values.String) (*values.Array, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.GetInnerHTMLBySelectorAll(el.id, selector))
|
2018-10-08 20:20:40 -04:00
|
|
|
|
2019-07-11 17:16:34 -04:00
|
|
|
if err != nil {
|
2020-12-19 13:42:57 -05:00
|
|
|
return values.EmptyArray(), err
|
2019-07-11 17:16:34 -04:00
|
|
|
}
|
2018-10-08 20:20:40 -04:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToArray(ctx, out), nil
|
2018-10-08 20:20:40 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
func (el *HTMLElement) CountBySelector(ctx context.Context, selector values.String) (values.Int, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.CountBySelector(el.id, selector))
|
2018-10-12 21:52:27 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-06 21:02:41 -04:00
|
|
|
return values.ZeroInt, err
|
2018-10-12 21:52:27 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToInt(out), nil
|
2018-10-12 21:52:27 -04:00
|
|
|
}
|
|
|
|
|
2019-07-26 13:22:06 -04:00
|
|
|
func (el *HTMLElement) ExistsBySelector(ctx context.Context, selector values.String) (values.Boolean, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
out, err := el.exec.EvalValue(ctx, templates.ExistsBySelector(el.id, selector))
|
2018-12-21 23:14:41 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2019-07-26 13:22:06 -04:00
|
|
|
return values.False, err
|
2018-12-21 23:14:41 -05:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return values.ToBoolean(out), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForElement(ctx context.Context, selector values.String, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForElement(el.id, selector, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
2018-12-21 23:14:41 -05:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForElementAll(ctx context.Context, selector values.String, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForElementAll(el.id, selector, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
2018-12-21 23:14:41 -05:00
|
|
|
}
|
|
|
|
|
2019-03-06 21:52:41 -05:00
|
|
|
func (el *HTMLElement) WaitForClass(ctx context.Context, class values.String, when drivers.WaitEvent) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForClass(el.id, class, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
2019-09-06 21:02:41 -04:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
_, err := task.Run(ctx)
|
2018-11-15 14:33:53 -05:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return err
|
|
|
|
}
|
2018-11-15 14:33:53 -05:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
func (el *HTMLElement) WaitForClassBySelector(ctx context.Context, selector, class values.String, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForClassBySelector(el.id, selector, class, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
2019-03-06 21:52:41 -05:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
_, err := task.Run(ctx)
|
2018-11-15 14:33:53 -05:00
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForClassBySelectorAll(ctx context.Context, selector, class values.String, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForClassBySelectorAll(el.id, selector, class, when),
|
2018-11-15 14:33:53 -05:00
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
2019-02-20 21:24:05 -05:00
|
|
|
_, err := task.Run(ctx)
|
2018-11-15 14:33:53 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-13 22:50:29 -04:00
|
|
|
func (el *HTMLElement) WaitForAttribute(
|
|
|
|
ctx context.Context,
|
|
|
|
name values.String,
|
|
|
|
value core.Value,
|
|
|
|
when drivers.WaitEvent,
|
|
|
|
) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForAttribute(el.id, name, value, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForAttributeBySelector(ctx context.Context, selector, name values.String, value core.Value, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForAttributeBySelector(el.id, selector, name, value, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForAttributeBySelectorAll(ctx context.Context, selector, name values.String, value core.Value, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForAttributeBySelectorAll(el.id, selector, name, value, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
2019-03-13 22:50:29 -04:00
|
|
|
|
2019-03-14 22:10:15 -04:00
|
|
|
_, err := task.Run(ctx)
|
2019-03-13 22:50:29 -04:00
|
|
|
|
2019-03-14 22:10:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForStyle(ctx context.Context, name values.String, value core.Value, when drivers.WaitEvent) error {
|
2021-09-02 11:09:48 -04:00
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
2021-09-07 16:33:30 -04:00
|
|
|
templates.WaitForStyle(el.id, name, value, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForStyleBySelector(ctx context.Context, selector, name values.String, value core.Value, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForStyleBySelector(el.id, selector, name, value, when),
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForStyleBySelectorAll(ctx context.Context, selector, name values.String, value core.Value, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewEvalWaitTask(
|
|
|
|
el.exec,
|
|
|
|
templates.WaitForStyleBySelectorAll(el.id, selector, name, value, when),
|
2021-09-02 11:09:48 -04:00
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
2019-03-13 22:50:29 -04:00
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-07 01:59:32 -04:00
|
|
|
func (el *HTMLElement) Click(ctx context.Context, count values.Int) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.Click(ctx, el.id, int(count))
|
2018-09-25 11:43:58 -04:00
|
|
|
}
|
2018-09-27 00:26:56 -04:00
|
|
|
|
2019-09-07 01:59:32 -04:00
|
|
|
func (el *HTMLElement) ClickBySelector(ctx context.Context, selector values.String, count values.Int) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.ClickBySelector(ctx, el.id, selector, count)
|
2019-08-24 20:26:27 -04:00
|
|
|
}
|
|
|
|
|
2019-09-07 01:59:32 -04:00
|
|
|
func (el *HTMLElement) ClickBySelectorAll(ctx context.Context, selector values.String, count values.Int) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
elements, err := el.QuerySelectorAll(ctx, selector)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
elements.ForEach(func(value core.Value, idx int) bool {
|
|
|
|
found := value.(*HTMLElement)
|
|
|
|
|
|
|
|
if e := found.Click(ctx, count); e != nil {
|
|
|
|
err = e
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
2019-08-24 20:26:27 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:24:05 -05:00
|
|
|
func (el *HTMLElement) Input(ctx context.Context, value core.Value, delay values.Int) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
name, err := el.GetNodeName(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ToLower(string(name)) != "input" {
|
2019-06-20 13:21:48 -04:00
|
|
|
return core.Error(core.ErrInvalidOperation, "element is not an <input> element.")
|
2018-10-08 20:07:08 -07:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.Type(ctx, el.id, input.TypeParams{
|
2019-09-05 12:17:22 -04:00
|
|
|
Text: value.String(),
|
2019-09-01 16:09:35 -04:00
|
|
|
Clear: false,
|
2019-09-05 12:17:22 -04:00
|
|
|
Delay: time.Duration(delay) * time.Millisecond,
|
2019-09-01 16:09:35 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.TypeBySelector(ctx, el.id, selector, input.TypeParams{
|
2019-09-05 12:17:22 -04:00
|
|
|
Text: value.String(),
|
2019-09-01 16:09:35 -04:00
|
|
|
Clear: false,
|
2019-09-05 12:17:22 -04:00
|
|
|
Delay: time.Duration(delay) * time.Millisecond,
|
2019-09-01 16:09:35 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-23 10:42:31 -04:00
|
|
|
func (el *HTMLElement) Press(ctx context.Context, keys []values.String, count values.Int) error {
|
|
|
|
return el.input.Press(ctx, values.UnwrapStrings(keys), int(count))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) PressBySelector(ctx context.Context, selector values.String, keys []values.String, count values.Int) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.PressBySelector(ctx, el.id, selector, values.UnwrapStrings(keys), int(count))
|
2021-04-23 10:42:31 -04:00
|
|
|
}
|
|
|
|
|
2019-09-01 16:09:35 -04:00
|
|
|
func (el *HTMLElement) Clear(ctx context.Context) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.Clear(ctx, el.id)
|
2019-09-01 16:09:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) ClearBySelector(ctx context.Context, selector values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.ClearBySelector(ctx, el.id, selector)
|
2018-09-27 19:05:56 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:24:05 -05:00
|
|
|
func (el *HTMLElement) Select(ctx context.Context, value *values.Array) (*values.Array, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.Select(ctx, el.id, value)
|
2018-11-12 14:53:36 -05:00
|
|
|
}
|
|
|
|
|
2019-09-06 23:15:27 -04:00
|
|
|
func (el *HTMLElement) SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error) {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.SelectBySelector(ctx, el.id, selector, value)
|
2019-09-06 23:15:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-25 15:06:00 -04:00
|
|
|
func (el *HTMLElement) ScrollIntoView(ctx context.Context, options drivers.ScrollOptions) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.ScrollIntoView(ctx, el.id, options)
|
2018-11-15 14:33:53 -05:00
|
|
|
}
|
|
|
|
|
2019-07-23 16:13:04 -04:00
|
|
|
func (el *HTMLElement) Focus(ctx context.Context) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.Focus(ctx, el.id)
|
2019-07-23 16:13:04 -04:00
|
|
|
}
|
|
|
|
|
2019-09-06 23:15:27 -04:00
|
|
|
func (el *HTMLElement) FocusBySelector(ctx context.Context, selector values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.FocusBySelector(ctx, el.id, selector)
|
2019-09-06 23:15:27 -04:00
|
|
|
}
|
|
|
|
|
2019-09-07 12:59:37 -04:00
|
|
|
func (el *HTMLElement) Blur(ctx context.Context) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.Blur(ctx, el.id)
|
2019-09-07 12:59:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) BlurBySelector(ctx context.Context, selector values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.BlurBySelector(ctx, el.id, selector)
|
2019-09-07 12:59:37 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 21:24:05 -05:00
|
|
|
func (el *HTMLElement) Hover(ctx context.Context) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.MoveMouse(ctx, el.id)
|
2018-11-15 14:33:53 -05:00
|
|
|
}
|
|
|
|
|
2019-09-06 23:15:27 -04:00
|
|
|
func (el *HTMLElement) HoverBySelector(ctx context.Context, selector values.String) error {
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.input.MoveMouseBySelector(ctx, el.id, selector)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) evalTo(ctx context.Context, fn *eval.Function) (core.Value, error) {
|
|
|
|
out, err := el.exec.EvalRef(ctx, fn)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
2018-09-27 11:32:52 -04:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return el.fromEvalRef(ctx, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) evalToElement(ctx context.Context, fn *eval.Function) (core.Value, error) {
|
|
|
|
obj, err := el.exec.EvalRef(ctx, fn)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if obj.Type != "object" || obj.ObjectID == nil {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResolveHTMLElement(
|
|
|
|
el.logger,
|
|
|
|
el.client,
|
|
|
|
el.dom,
|
|
|
|
el.input,
|
|
|
|
el.exec,
|
|
|
|
obj,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) fromEvalRef(ctx context.Context, out runtime.RemoteObject) (core.Value, error) {
|
|
|
|
var subtype string
|
|
|
|
|
|
|
|
if out.Subtype != nil {
|
|
|
|
subtype = *out.Subtype
|
|
|
|
}
|
|
|
|
|
|
|
|
if subtype == "null" || subtype == "undefined" {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch subtype {
|
|
|
|
case "array", "HTMLCollection", "NodeList":
|
2020-12-19 13:42:57 -05:00
|
|
|
if out.ObjectID == nil {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
props, err := el.client.Runtime.GetProperties(ctx, runtime.NewGetPropertiesArgs(*out.ObjectID).SetOwnProperties(true))
|
2018-09-27 11:32:52 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
2019-09-01 16:09:35 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
if props.ExceptionDetails != nil {
|
|
|
|
exception := *props.ExceptionDetails
|
2018-09-27 11:32:52 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
return values.None, errors.New(exception.Text)
|
2018-09-27 11:32:52 -04:00
|
|
|
}
|
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
result := values.NewArray(len(props.Result))
|
2018-09-27 11:32:52 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
for _, descr := range props.Result {
|
|
|
|
if !descr.Enumerable {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-11 12:39:03 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
if descr.Value == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-27 11:32:52 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
// it's not a Node, it's an attr value
|
|
|
|
if descr.Value.ObjectID == nil {
|
|
|
|
var value interface{}
|
2018-09-27 11:32:52 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
if err := json.Unmarshal(descr.Value.Value, &value); err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
2018-09-27 11:32:52 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
result.Push(values.Parse(value))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
el, err := ResolveHTMLElement(
|
2020-12-19 13:42:57 -05:00
|
|
|
el.logger,
|
|
|
|
el.client,
|
|
|
|
el.dom,
|
|
|
|
el.input,
|
|
|
|
el.exec,
|
2021-09-07 16:33:30 -04:00
|
|
|
*descr.Value,
|
2020-12-19 13:42:57 -05:00
|
|
|
)
|
2019-09-01 16:09:35 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
2018-09-27 11:32:52 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
result.Push(el)
|
2018-09-27 11:32:52 -04:00
|
|
|
}
|
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
return result, nil
|
2021-09-07 16:33:30 -04:00
|
|
|
case "node":
|
2020-12-19 13:42:57 -05:00
|
|
|
if out.ObjectID == nil {
|
|
|
|
var value interface{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(out.Value, &value); err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
2018-09-27 11:32:52 -04:00
|
|
|
|
2020-12-19 13:42:57 -05:00
|
|
|
return values.Parse(value), nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:30 -04:00
|
|
|
return ResolveHTMLElement(
|
2020-12-19 13:42:57 -05:00
|
|
|
el.logger,
|
|
|
|
el.client,
|
|
|
|
el.dom,
|
|
|
|
el.input,
|
|
|
|
el.exec,
|
2021-09-07 16:33:30 -04:00
|
|
|
out,
|
2020-12-19 13:42:57 -05:00
|
|
|
)
|
|
|
|
default:
|
2021-09-07 16:33:30 -04:00
|
|
|
return eval.Unmarshal(out)
|
2020-12-19 13:42:57 -05:00
|
|
|
}
|
2018-09-27 11:32:52 -04:00
|
|
|
}
|
2018-10-11 12:39:03 -04:00
|
|
|
|
|
|
|
func (el *HTMLElement) logError(err error) *zerolog.Event {
|
|
|
|
return el.logger.
|
|
|
|
Error().
|
|
|
|
Timestamp().
|
2021-09-07 16:33:30 -04:00
|
|
|
Str("objectID", string(el.id)).
|
2018-10-11 12:39:03 -04:00
|
|
|
Err(err)
|
|
|
|
}
|