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"
|
2019-02-24 00:52:01 +02:00
|
|
|
"time"
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
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"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-10-07 04:33:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultTimeout = 5000
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
func NewLib() map[string]core.Function {
|
|
|
|
return map[string]core.Function{
|
2019-03-07 04:52:41 +02:00
|
|
|
"CLICK": Click,
|
|
|
|
"CLICK_ALL": ClickAll,
|
|
|
|
"DOCUMENT": Document,
|
|
|
|
"DOWNLOAD": Download,
|
|
|
|
"ELEMENT": Element,
|
|
|
|
"ELEMENT_EXISTS": ElementExists,
|
|
|
|
"ELEMENTS": Elements,
|
|
|
|
"ELEMENTS_COUNT": ElementsCount,
|
|
|
|
"HOVER": Hover,
|
|
|
|
"INNER_HTML": InnerHTML,
|
|
|
|
"INNER_HTML_ALL": InnerHTMLAll,
|
|
|
|
"INNER_TEXT": InnerText,
|
|
|
|
"INNER_TEXT_ALL": InnerTextAll,
|
|
|
|
"INPUT": Input,
|
|
|
|
"MOUSE": MouseMoveXY,
|
|
|
|
"NAVIGATE": Navigate,
|
|
|
|
"NAVIGATE_BACK": NavigateBack,
|
|
|
|
"NAVIGATE_FORWARD": NavigateForward,
|
|
|
|
"PAGINATION": Pagination,
|
|
|
|
"PDF": PDF,
|
|
|
|
"SCREENSHOT": Screenshot,
|
|
|
|
"SCROLL": ScrollXY,
|
|
|
|
"SCROLL_BOTTOM": ScrollBottom,
|
|
|
|
"SCROLL_ELEMENT": ScrollInto,
|
|
|
|
"SCROLL_TOP": ScrollTop,
|
|
|
|
"SELECT": Select,
|
|
|
|
"WAIT_ELEMENT": WaitElement,
|
|
|
|
"WAIT_NO_ELEMENT": WaitNoElement,
|
|
|
|
"WAIT_CLASS": WaitClass,
|
|
|
|
"WAIT_NO_CLASS": WaitNoClass,
|
|
|
|
"WAIT_CLASS_ALL": WaitClassAll,
|
|
|
|
"WAIT_NO_CLASS_ALL": WaitNoClassAll,
|
|
|
|
"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) {
|
2019-02-20 01:10:18 +02:00
|
|
|
err := core.ValidateType(value, drivers.HTMLDocumentType, types.String)
|
2018-12-22 06:14:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
var doc drivers.HTMLDocument
|
2018-12-22 06:14:41 +02:00
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
if value.Type() == types.String {
|
2018-12-22 06:14:41 +02:00
|
|
|
buf, err := Document(ctx, value, values.NewBoolean(true))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
doc = buf.(drivers.HTMLDocument)
|
2018-12-22 06:14:41 +02:00
|
|
|
} else {
|
2019-02-20 01:10:18 +02:00
|
|
|
doc = value.(drivers.HTMLDocument)
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
return doc, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func waitTimeout(ctx context.Context, value values.Int) (context.Context, context.CancelFunc) {
|
|
|
|
return context.WithTimeout(
|
|
|
|
ctx,
|
|
|
|
time.Duration(value)*time.Millisecond,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
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
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
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
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|