2018-09-18 22:42:38 +02:00
|
|
|
package html
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
import (
|
2018-12-22 06:14:41 +02:00
|
|
|
"context"
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2018-12-22 06:14:41 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2018-10-07 04:33:39 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultTimeout = 5000
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNotDynamic = errors.New("expected dynamic document or element")
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
func NewLib() map[string]core.Function {
|
|
|
|
return map[string]core.Function{
|
2018-11-15 21:33:53 +02:00
|
|
|
"CLICK": Click,
|
|
|
|
"CLICK_ALL": ClickAll,
|
2018-10-12 22:30:17 +02:00
|
|
|
"DOCUMENT": Document,
|
2018-11-15 21:33:53 +02:00
|
|
|
"DOWNLOAD": Download,
|
2018-10-12 22:30:17 +02:00
|
|
|
"ELEMENT": Element,
|
2018-12-22 06:14:41 +02:00
|
|
|
"ELEMENT_EXISTS": ElementExists,
|
2018-10-12 22:30:17 +02:00
|
|
|
"ELEMENTS": Elements,
|
2018-10-13 03:52:27 +02:00
|
|
|
"ELEMENTS_COUNT": ElementsCount,
|
2018-11-15 21:33:53 +02:00
|
|
|
"HOVER": Hover,
|
2018-12-22 06:14:41 +02:00
|
|
|
"HTML_PARSE": Parse,
|
2018-10-12 22:30:17 +02:00
|
|
|
"INNER_HTML": InnerHTML,
|
|
|
|
"INNER_HTML_ALL": InnerHTMLAll,
|
|
|
|
"INNER_TEXT": InnerText,
|
|
|
|
"INNER_TEXT_ALL": InnerTextAll,
|
2018-11-15 21:33:53 +02:00
|
|
|
"INPUT": Input,
|
|
|
|
"NAVIGATE": Navigate,
|
|
|
|
"NAVIGATE_BACK": NavigateBack,
|
|
|
|
"NAVIGATE_FORWARD": NavigateForward,
|
2018-11-13 02:58:12 +02:00
|
|
|
"PAGINATION": Pagination,
|
2018-10-14 03:08:18 +02:00
|
|
|
"PDF": PDF,
|
2018-11-15 21:33:53 +02:00
|
|
|
"SCREENSHOT": Screenshot,
|
|
|
|
"SCROLL_BOTTOM": ScrollBottom,
|
|
|
|
"SCROLL_ELEMENT": ScrollInto,
|
|
|
|
"SCROLL_TOP": ScrollTop,
|
|
|
|
"SELECT": Select,
|
|
|
|
"WAIT_ELEMENT": WaitElement,
|
|
|
|
"WAIT_CLASS": WaitClass,
|
|
|
|
"WAIT_CLASS_ALL": WaitClassAll,
|
|
|
|
"WAIT_NAVIGATION": WaitNavigation,
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
func ValidateDocument(ctx context.Context, value core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateType(value, core.HTMLDocumentType, core.StringType)
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var doc values.DHTMLDocument
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if value.Type() == core.StringType {
|
|
|
|
buf, err := Document(ctx, value, values.NewBoolean(true))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, ok = buf.(values.DHTMLDocument)
|
|
|
|
} else {
|
|
|
|
doc, ok = value.(values.DHTMLDocument)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrNotDynamic
|
|
|
|
}
|
|
|
|
|
|
|
|
return doc, nil
|
|
|
|
}
|