github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/lib.go (about)

     1  package html
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/MontFerret/ferret/pkg/drivers"
     8  	"github.com/MontFerret/ferret/pkg/runtime/core"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values"
    10  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    11  )
    12  
    13  func RegisterLib(ns core.Namespace) error {
    14  	return ns.RegisterFunctions(
    15  		core.NewFunctionsFromMap(map[string]core.Function{
    16  			"ATTR_GET":          AttributeGet,
    17  			"ATTR_QUERY":        AttributeQuery,
    18  			"ATTR_REMOVE":       AttributeRemove,
    19  			"ATTR_SET":          AttributeSet,
    20  			"BLUR":              Blur,
    21  			"COOKIE_DEL":        CookieDel,
    22  			"COOKIE_GET":        CookieGet,
    23  			"COOKIE_SET":        CookieSet,
    24  			"CLICK":             Click,
    25  			"CLICK_ALL":         ClickAll,
    26  			"DOCUMENT":          Open,
    27  			"DOCUMENT_EXISTS":   DocumentExists,
    28  			"DOWNLOAD":          Download,
    29  			"ELEMENT":           Element,
    30  			"ELEMENT_EXISTS":    ElementExists,
    31  			"ELEMENTS":          Elements,
    32  			"ELEMENTS_COUNT":    ElementsCount,
    33  			"FRAMES":            Frames,
    34  			"FOCUS":             Focus,
    35  			"HOVER":             Hover,
    36  			"INNER_HTML":        GetInnerHTML,
    37  			"INNER_HTML_SET":    SetInnerHTML,
    38  			"INNER_HTML_ALL":    GetInnerHTMLAll,
    39  			"INNER_TEXT":        GetInnerText,
    40  			"INNER_TEXT_SET":    SetInnerText,
    41  			"INNER_TEXT_ALL":    GetInnerTextAll,
    42  			"INPUT":             Input,
    43  			"INPUT_CLEAR":       InputClear,
    44  			"MOUSE":             MouseMoveXY,
    45  			"NAVIGATE":          Navigate,
    46  			"NAVIGATE_BACK":     NavigateBack,
    47  			"NAVIGATE_FORWARD":  NavigateForward,
    48  			"PAGINATION":        Pagination,
    49  			"PARSE":             Parse,
    50  			"PDF":               PDF,
    51  			"PRESS":             Press,
    52  			"PRESS_SELECTOR":    PressSelector,
    53  			"SCREENSHOT":        Screenshot,
    54  			"SCROLL":            ScrollXY,
    55  			"SCROLL_BOTTOM":     ScrollBottom,
    56  			"SCROLL_ELEMENT":    ScrollInto,
    57  			"SCROLL_TOP":        ScrollTop,
    58  			"SELECT":            Select,
    59  			"STYLE_GET":         StyleGet,
    60  			"STYLE_REMOVE":      StyleRemove,
    61  			"STYLE_SET":         StyleSet,
    62  			"WAIT_ATTR":         WaitAttribute,
    63  			"WAIT_NO_ATTR":      WaitNoAttribute,
    64  			"WAIT_ATTR_ALL":     WaitAttributeAll,
    65  			"WAIT_NO_ATTR_ALL":  WaitNoAttributeAll,
    66  			"WAIT_ELEMENT":      WaitElement,
    67  			"WAIT_NO_ELEMENT":   WaitNoElement,
    68  			"WAIT_CLASS":        WaitClass,
    69  			"WAIT_NO_CLASS":     WaitNoClass,
    70  			"WAIT_CLASS_ALL":    WaitClassAll,
    71  			"WAIT_NO_CLASS_ALL": WaitNoClassAll,
    72  			"WAIT_STYLE":        WaitStyle,
    73  			"WAIT_NO_STYLE":     WaitNoStyle,
    74  			"WAIT_STYLE_ALL":    WaitStyleAll,
    75  			"WAIT_NO_STYLE_ALL": WaitNoStyleAll,
    76  			"WAIT_NAVIGATION":   WaitNavigation,
    77  			"XPATH":             XPath,
    78  			"X":                 XPathSelector,
    79  		}))
    80  }
    81  
    82  func OpenOrCastPage(ctx context.Context, value core.Value) (drivers.HTMLPage, bool, error) {
    83  	err := core.ValidateType(value, drivers.HTMLPageType, types.String)
    84  
    85  	if err != nil {
    86  		return nil, false, err
    87  	}
    88  
    89  	var page drivers.HTMLPage
    90  	var closeAfter bool
    91  
    92  	if value.Type() == types.String {
    93  		buf, err := Open(ctx, value, values.NewBoolean(true))
    94  
    95  		if err != nil {
    96  			return nil, false, err
    97  		}
    98  
    99  		page = buf.(drivers.HTMLPage)
   100  		closeAfter = true
   101  	} else {
   102  		page = value.(drivers.HTMLPage)
   103  	}
   104  
   105  	return page, closeAfter, nil
   106  }
   107  
   108  func waitTimeout(ctx context.Context, value values.Int) (context.Context, context.CancelFunc) {
   109  	return context.WithTimeout(
   110  		ctx,
   111  		time.Duration(value)*time.Millisecond,
   112  	)
   113  }
   114  
   115  func toScrollOptions(value core.Value) (drivers.ScrollOptions, error) {
   116  	result := drivers.ScrollOptions{}
   117  
   118  	err := core.ValidateType(value, types.Object)
   119  
   120  	if err != nil {
   121  		return result, err
   122  	}
   123  
   124  	obj := value.(*values.Object)
   125  
   126  	behavior, exists := obj.Get("behavior")
   127  
   128  	if exists {
   129  		result.Behavior = drivers.NewScrollBehavior(behavior.String())
   130  	}
   131  
   132  	block, exists := obj.Get("block")
   133  
   134  	if exists {
   135  		result.Block = drivers.NewScrollVerticalAlignment(block.String())
   136  	}
   137  
   138  	inline, exists := obj.Get("inline")
   139  
   140  	if exists {
   141  		result.Inline = drivers.NewScrollHorizontalAlignment(inline.String())
   142  	}
   143  
   144  	return result, nil
   145  }