mirror of
				https://github.com/MontFerret/ferret.git
				synced 2025-10-30 23:37:40 +02:00 
			
		
		
		
	Feature/#177 hover (#178)
This commit is contained in:
		
							
								
								
									
										55
									
								
								pkg/stdlib/html/hover.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								pkg/stdlib/html/hover.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/MontFerret/ferret/pkg/html/dynamic" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| ) | ||||
|  | ||||
| // Hover  fetches an element with selector, scrolls it into view if needed, and then uses page.mouse to hover over the center of the element. | ||||
| // If there's no element matching selector, the method returns an error. | ||||
| // @param docOrEl (HTMLDocument|HTMLElement) - Target document or element. | ||||
| // @param selector (String, options) - If document is passed, this param must represent an element selector. | ||||
| func Hover(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 1, 2) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	// document or element | ||||
| 	err = core.ValidateType(args[0], core.HTMLDocumentType, core.HTMLElementType) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	if len(args) == 2 { | ||||
| 		err = core.ValidateType(args[1], core.StringType) | ||||
|  | ||||
| 		if err != nil { | ||||
| 			return values.None, err | ||||
| 		} | ||||
|  | ||||
| 		// Document with a selector | ||||
| 		doc, ok := args[0].(*dynamic.HTMLDocument) | ||||
|  | ||||
| 		if !ok { | ||||
| 			return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 		} | ||||
|  | ||||
| 		selector := args[1].(values.String) | ||||
|  | ||||
| 		return values.None, doc.HoverBySelector(selector) | ||||
| 	} | ||||
|  | ||||
| 	// Element | ||||
| 	el, ok := args[0].(*dynamic.HTMLElement) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 	} | ||||
|  | ||||
| 	return values.None, el.Hover() | ||||
| } | ||||
| @@ -13,31 +13,33 @@ var ( | ||||
|  | ||||
| func NewLib() map[string]core.Function { | ||||
| 	return map[string]core.Function{ | ||||
| 		"CLICK":            Click, | ||||
| 		"CLICK_ALL":        ClickAll, | ||||
| 		"DOCUMENT":         Document, | ||||
| 		"DOCUMENT_PARSE":   DocumentParse, | ||||
| 		"DOWNLOAD":         Download, | ||||
| 		"ELEMENT":          Element, | ||||
| 		"ELEMENTS":         Elements, | ||||
| 		"ELEMENTS_COUNT":   ElementsCount, | ||||
| 		"WAIT_ELEMENT":     WaitElement, | ||||
| 		"WAIT_NAVIGATION":  WaitNavigation, | ||||
| 		"WAIT_CLASS":       WaitClass, | ||||
| 		"WAIT_CLASS_ALL":   WaitClassAll, | ||||
| 		"CLICK":            Click, | ||||
| 		"CLICK_ALL":        ClickAll, | ||||
| 		"NAVIGATE":         Navigate, | ||||
| 		"NAVIGATE_BACK":    NavigateBack, | ||||
| 		"NAVIGATE_FORWARD": NavigateForward, | ||||
| 		"INPUT":            Input, | ||||
| 		"HOVER":            Hover, | ||||
| 		"INNER_HTML":       InnerHTML, | ||||
| 		"INNER_HTML_ALL":   InnerHTMLAll, | ||||
| 		"INNER_TEXT":       InnerText, | ||||
| 		"INNER_TEXT_ALL":   InnerTextAll, | ||||
| 		"SELECT":           Select, | ||||
| 		"SCREENSHOT":       Screenshot, | ||||
| 		"SCROLL_TOP":       ScrollTop, | ||||
| 		"SCROLL_BOTTOM":    ScrollBottom, | ||||
| 		"INPUT":            Input, | ||||
| 		"NAVIGATE":         Navigate, | ||||
| 		"NAVIGATE_BACK":    NavigateBack, | ||||
| 		"NAVIGATE_FORWARD": NavigateForward, | ||||
| 		"PAGINATION":       Pagination, | ||||
| 		"PDF":              PDF, | ||||
| 		"DOWNLOAD":         Download, | ||||
| 		"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, | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -1,56 +0,0 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/MontFerret/ferret/pkg/html/dynamic" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| ) | ||||
|  | ||||
| // ScrollTop Scrolls the document's window to its top. | ||||
| // @param doc (Document) - Target document. | ||||
| func ScrollTop(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 1, 1) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[0], core.HTMLDocumentType) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	doc, ok := args[0].(*dynamic.HTMLDocument) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 	} | ||||
|  | ||||
| 	return values.None, doc.ScrollTop() | ||||
| } | ||||
|  | ||||
| // ScrollTop Scrolls the document's window to its bottom. | ||||
| // @param doc (Document) - Target document. | ||||
| func ScrollBottom(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 1, 1) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[0], core.HTMLDocumentType) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	doc, ok := args[0].(*dynamic.HTMLDocument) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 	} | ||||
|  | ||||
| 	return values.None, doc.ScrollBottom() | ||||
| } | ||||
							
								
								
									
										32
									
								
								pkg/stdlib/html/scroll_bottom.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								pkg/stdlib/html/scroll_bottom.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/MontFerret/ferret/pkg/html/dynamic" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| ) | ||||
|  | ||||
| // ScrollTop scrolls the document's window to its bottom. | ||||
| // @param doc (HTMLDocument) - Target document. | ||||
| func ScrollBottom(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 1, 1) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[0], core.HTMLDocumentType) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	doc, ok := args[0].(*dynamic.HTMLDocument) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 	} | ||||
|  | ||||
| 	return values.None, doc.ScrollBottom() | ||||
| } | ||||
							
								
								
									
										54
									
								
								pkg/stdlib/html/scroll_element.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								pkg/stdlib/html/scroll_element.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/MontFerret/ferret/pkg/html/dynamic" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| ) | ||||
|  | ||||
| // ScrollInto scrolls an element on. | ||||
| // @param docOrEl (HTMLDocument|HTMLElement) - Target document or element. | ||||
| // @param selector (String, options) - If document is passed, this param must represent an element selector. | ||||
| func ScrollInto(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 1, 2) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	// document or element | ||||
| 	err = core.ValidateType(args[0], core.HTMLDocumentType, core.HTMLElementType) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	if len(args) == 2 { | ||||
| 		err = core.ValidateType(args[1], core.StringType) | ||||
|  | ||||
| 		if err != nil { | ||||
| 			return values.None, err | ||||
| 		} | ||||
|  | ||||
| 		// Document with a selector | ||||
| 		doc, ok := args[0].(*dynamic.HTMLDocument) | ||||
|  | ||||
| 		if !ok { | ||||
| 			return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 		} | ||||
|  | ||||
| 		selector := args[1].(values.String) | ||||
|  | ||||
| 		return values.None, doc.ScrollBySelector(selector) | ||||
| 	} | ||||
|  | ||||
| 	// Element | ||||
| 	el, ok := args[0].(*dynamic.HTMLElement) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 	} | ||||
|  | ||||
| 	return values.None, el.ScrollIntoView() | ||||
| } | ||||
							
								
								
									
										32
									
								
								pkg/stdlib/html/scroll_top.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								pkg/stdlib/html/scroll_top.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| package html | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/MontFerret/ferret/pkg/html/dynamic" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/core" | ||||
| 	"github.com/MontFerret/ferret/pkg/runtime/values" | ||||
| ) | ||||
|  | ||||
| // ScrollTop scrolls the document's window to its top. | ||||
| // @param doc (HTMLDocument) - Target document. | ||||
| func ScrollTop(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
| 	err := core.ValidateArgs(args, 1, 1) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	err = core.ValidateType(args[0], core.HTMLDocumentType) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return values.None, err | ||||
| 	} | ||||
|  | ||||
| 	doc, ok := args[0].(*dynamic.HTMLDocument) | ||||
|  | ||||
| 	if !ok { | ||||
| 		return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 	} | ||||
|  | ||||
| 	return values.None, doc.ScrollTop() | ||||
| } | ||||
| @@ -50,7 +50,12 @@ func WaitClass(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
| 			return values.None, err | ||||
| 		} | ||||
|  | ||||
| 		doc := args[0].(*dynamic.HTMLDocument) | ||||
| 		doc, ok := args[0].(*dynamic.HTMLDocument) | ||||
|  | ||||
| 		if !ok { | ||||
| 			return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 		} | ||||
|  | ||||
| 		selector := args[1].(values.String) | ||||
| 		class := args[2].(values.String) | ||||
|  | ||||
| @@ -66,7 +71,12 @@ func WaitClass(_ context.Context, args ...core.Value) (core.Value, error) { | ||||
|  | ||||
| 		return values.None, doc.WaitForClass(selector, class, timeout) | ||||
| 	case *dynamic.HTMLElement: | ||||
| 		el := args[0].(*dynamic.HTMLElement) | ||||
| 		el, ok := args[0].(*dynamic.HTMLElement) | ||||
|  | ||||
| 		if !ok { | ||||
| 			return values.None, core.Errors(core.ErrInvalidType, ErrNotDynamic) | ||||
| 		} | ||||
|  | ||||
| 		class := args[1].(values.String) | ||||
|  | ||||
| 		if len(args) == 3 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user