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
|
|
|
|
|
2019-07-22 23:21:20 +02:00
|
|
|
func RegisterLib(ns core.Namespace) error {
|
|
|
|
return ns.RegisterFunctions(core.Functions{
|
2019-03-13 20:51:30 +02:00
|
|
|
"ATTR_GET": AttributeGet,
|
|
|
|
"ATTR_REMOVE": AttributeRemove,
|
|
|
|
"ATTR_SET": AttributeSet,
|
2019-03-16 01:59:05 +02:00
|
|
|
"COOKIE_DEL": CookieDel,
|
|
|
|
"COOKIE_GET": CookieGet,
|
|
|
|
"COOKIE_SET": CookieSet,
|
2019-03-07 04:52:41 +02:00
|
|
|
"CLICK": Click,
|
|
|
|
"CLICK_ALL": ClickAll,
|
2019-06-19 23:58:56 +02:00
|
|
|
"DOCUMENT": Open,
|
2019-03-07 04:52:41 +02:00
|
|
|
"DOWNLOAD": Download,
|
|
|
|
"ELEMENT": Element,
|
|
|
|
"ELEMENT_EXISTS": ElementExists,
|
|
|
|
"ELEMENTS": Elements,
|
|
|
|
"ELEMENTS_COUNT": ElementsCount,
|
2019-07-23 22:13:04 +02:00
|
|
|
"FOCUS": Focus,
|
2019-03-07 04:52:41 +02:00
|
|
|
"HOVER": Hover,
|
2019-07-11 23:16:34 +02:00
|
|
|
"INNER_HTML": GetInnerHTML,
|
|
|
|
"INNER_HTML_SET": SetInnerHTML,
|
|
|
|
"INNER_HTML_ALL": GetInnerHTMLAll,
|
|
|
|
"INNER_TEXT": GetInnerText,
|
|
|
|
"INNER_TEXT_SET": SetInnerText,
|
|
|
|
"INNER_TEXT_ALL": GetInnerTextAll,
|
2019-03-07 04:52:41 +02:00
|
|
|
"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,
|
2019-03-13 20:51:30 +02:00
|
|
|
"STYLE_GET": StyleGet,
|
|
|
|
"STYLE_REMOVE": StyleRemove,
|
|
|
|
"STYLE_SET": StyleSet,
|
2019-03-14 04:50:29 +02:00
|
|
|
"WAIT_ATTR": WaitAttribute,
|
|
|
|
"WAIT_NO_ATTR": WaitNoAttribute,
|
|
|
|
"WAIT_ATTR_ALL": WaitAttributeAll,
|
|
|
|
"WAIT_NO_ATTR_ALL": WaitNoAttributeAll,
|
2019-03-07 04:52:41 +02:00
|
|
|
"WAIT_ELEMENT": WaitElement,
|
|
|
|
"WAIT_NO_ELEMENT": WaitNoElement,
|
|
|
|
"WAIT_CLASS": WaitClass,
|
|
|
|
"WAIT_NO_CLASS": WaitNoClass,
|
|
|
|
"WAIT_CLASS_ALL": WaitClassAll,
|
|
|
|
"WAIT_NO_CLASS_ALL": WaitNoClassAll,
|
2019-03-15 04:10:15 +02:00
|
|
|
"WAIT_STYLE": WaitStyle,
|
|
|
|
"WAIT_NO_STYLE": WaitNoStyle,
|
|
|
|
"WAIT_STYLE_ALL": WaitStyleAll,
|
|
|
|
"WAIT_NO_STYLE_ALL": WaitNoStyleAll,
|
2019-03-07 04:52:41 +02:00
|
|
|
"WAIT_NAVIGATION": WaitNavigation,
|
2019-07-03 20:05:02 +02:00
|
|
|
"XPATH": XPath,
|
2019-07-22 23:21:20 +02:00
|
|
|
})
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-12-22 06:14:41 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func OpenOrCastPage(ctx context.Context, value core.Value) (drivers.HTMLPage, bool, error) {
|
|
|
|
err := core.ValidateType(value, drivers.HTMLPageType, types.String)
|
2018-12-22 06:14:41 +02:00
|
|
|
if err != nil {
|
2019-06-19 23:58:56 +02:00
|
|
|
return nil, false, err
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
var page drivers.HTMLPage
|
|
|
|
var closeAfter bool
|
2018-12-22 06:14:41 +02:00
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
if value.Type() == types.String {
|
2019-06-19 23:58:56 +02:00
|
|
|
buf, err := Open(ctx, value, values.NewBoolean(true))
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-19 23:58:56 +02:00
|
|
|
return nil, false, err
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
page = buf.(drivers.HTMLPage)
|
|
|
|
closeAfter = true
|
2018-12-22 06:14:41 +02:00
|
|
|
} else {
|
2019-06-19 23:58:56 +02:00
|
|
|
page = value.(drivers.HTMLPage)
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return page, closeAfter, nil
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|