1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-06-23 00:28:10 +02:00

Refactoring/externalized html (#234)

* Externalized HTML drivers

* Fixed unit tests

* Updated logging

* Added support to set default driver

* Updated GetIn and SetIn helpers
This commit is contained in:
Tim Voronov
2019-02-19 18:10:18 -05:00
committed by GitHub
parent f8e061cc80
commit 34c8c02258
62 changed files with 1356 additions and 1054 deletions

View File

@ -3,18 +3,14 @@ package html
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"github.com/pkg/errors"
)
const defaultTimeout = 5000
var (
ErrNotDynamic = errors.New("expected dynamic document or element")
)
func NewLib() map[string]core.Function {
return map[string]core.Function{
"CLICK": Click,
@ -26,7 +22,6 @@ func NewLib() map[string]core.Function {
"ELEMENTS": Elements,
"ELEMENTS_COUNT": ElementsCount,
"HOVER": Hover,
"HTML_PARSE": Parse,
"INNER_HTML": InnerHTML,
"INNER_HTML_ALL": InnerHTMLAll,
"INNER_TEXT": InnerText,
@ -50,14 +45,12 @@ func NewLib() map[string]core.Function {
}
func ValidateDocument(ctx context.Context, value core.Value) (core.Value, error) {
err := core.ValidateType(value, types.HTMLDocument, types.String)
err := core.ValidateType(value, drivers.HTMLDocumentType, types.String)
if err != nil {
return values.None, err
}
var doc values.DHTMLDocument
var ok bool
var doc drivers.HTMLDocument
if value.Type() == types.String {
buf, err := Document(ctx, value, values.NewBoolean(true))
@ -66,14 +59,42 @@ func ValidateDocument(ctx context.Context, value core.Value) (core.Value, error)
return values.None, err
}
doc, ok = buf.(values.DHTMLDocument)
doc = buf.(drivers.HTMLDocument)
} else {
doc, ok = value.(values.DHTMLDocument)
}
if !ok {
return nil, ErrNotDynamic
doc = value.(drivers.HTMLDocument)
}
return doc, nil
}
func resolveElement(value core.Value) (drivers.HTMLElement, error) {
vt := value.Type()
if vt == drivers.HTMLDocumentType {
return value.(drivers.HTMLDocument).DocumentElement(), nil
} else if vt == drivers.HTMLElementType {
return value.(drivers.HTMLElement), nil
}
return nil, core.TypeError(value.Type(), drivers.HTMLDocumentType, drivers.HTMLElementType)
}
func toDocument(value core.Value) (drivers.HTMLDocument, error) {
err := core.ValidateType(value, drivers.HTMLDocumentType)
if err != nil {
return nil, err
}
return value.(drivers.HTMLDocument), nil
}
func toElement(value core.Value) (drivers.HTMLElement, error) {
err := core.ValidateType(value, drivers.HTMLElementType)
if err != nil {
return nil, err
}
return value.(drivers.HTMLElement), nil
}