mirror of
https://github.com/MontFerret/ferret.git
synced 2025-11-06 08:39:09 +02:00
Feature/focus (#340)
* Added and implemented Focus and FocusBySelector methods * Added e2e tests * Updated CHANGELOG * Fixed linting errors
This commit is contained in:
@@ -345,6 +345,10 @@ func (doc *HTMLDocument) SelectBySelector(ctx context.Context, selector values.S
|
||||
return doc.input.SelectBySelector(ctx, doc.element.id.nodeID, selector, value)
|
||||
}
|
||||
|
||||
func (doc *HTMLDocument) FocusBySelector(ctx context.Context, selector values.String) error {
|
||||
return doc.input.FocusBySelector(ctx, doc.element.id.nodeID, selector)
|
||||
}
|
||||
|
||||
func (doc *HTMLDocument) MoveMouseBySelector(ctx context.Context, selector values.String) error {
|
||||
return doc.input.MoveMouseBySelector(ctx, doc.element.id.nodeID, selector)
|
||||
}
|
||||
|
||||
@@ -971,6 +971,10 @@ func (el *HTMLElement) ScrollIntoView(ctx context.Context) error {
|
||||
return el.input.ScrollIntoView(ctx, el.id.objectID)
|
||||
}
|
||||
|
||||
func (el *HTMLElement) Focus(ctx context.Context) error {
|
||||
return el.input.Focus(ctx, el.id.objectID)
|
||||
}
|
||||
|
||||
func (el *HTMLElement) Hover(ctx context.Context) error {
|
||||
return el.input.MoveMouse(ctx, el.id.objectID)
|
||||
}
|
||||
|
||||
@@ -247,6 +247,10 @@ func (doc *HTMLDocument) ScrollByXY(_ context.Context, _, _ values.Float) error
|
||||
return core.ErrNotSupported
|
||||
}
|
||||
|
||||
func (doc *HTMLDocument) FocusBySelector(_ context.Context, _ values.String) error {
|
||||
return core.ErrNotSupported
|
||||
}
|
||||
|
||||
func (doc *HTMLDocument) MoveMouseBySelector(_ context.Context, _ values.String) error {
|
||||
return core.ErrNotSupported
|
||||
}
|
||||
|
||||
@@ -505,6 +505,10 @@ func (el *HTMLElement) ScrollIntoView(_ context.Context) error {
|
||||
return core.ErrNotSupported
|
||||
}
|
||||
|
||||
func (el *HTMLElement) Focus(_ context.Context) error {
|
||||
return core.ErrNotSupported
|
||||
}
|
||||
|
||||
func (el *HTMLElement) Hover(_ context.Context) error {
|
||||
return core.ErrNotSupported
|
||||
}
|
||||
|
||||
@@ -101,6 +101,8 @@ type (
|
||||
|
||||
ScrollIntoView(ctx context.Context) error
|
||||
|
||||
Focus(ctx context.Context) error
|
||||
|
||||
Hover(ctx context.Context) error
|
||||
|
||||
WaitForAttribute(ctx context.Context, name values.String, value core.Value, when WaitEvent) error
|
||||
@@ -141,6 +143,8 @@ type (
|
||||
|
||||
ScrollByXY(ctx context.Context, x, y values.Float) error
|
||||
|
||||
FocusBySelector(ctx context.Context, selector values.String) error
|
||||
|
||||
MoveMouseByXY(ctx context.Context, x, y values.Float) error
|
||||
|
||||
MoveMouseBySelector(ctx context.Context, selector values.String) error
|
||||
|
||||
38
pkg/stdlib/html/focus.go
Normal file
38
pkg/stdlib/html/focus.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package html
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/MontFerret/ferret/pkg/drivers"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
)
|
||||
|
||||
// FOCUS Calls focus on the element.
|
||||
// @param target (HTMLPage | HTMLDocument | HTMLElement) - Target node.
|
||||
// @param selector (String, optional) - Optional CSS selector. Required when target is HTMLPage or HTMLDocument.
|
||||
func Focus(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 1, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
// Document with selector
|
||||
if len(args) == 2 {
|
||||
doc, err := drivers.ToDocument(args[0])
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
return values.None, doc.FocusBySelector(ctx, values.ToString(args[1]))
|
||||
}
|
||||
|
||||
el, err := drivers.ToElement(args[0])
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
return values.None, el.Focus(ctx)
|
||||
}
|
||||
@@ -28,6 +28,7 @@ func RegisterLib(ns core.Namespace) error {
|
||||
"ELEMENT_EXISTS": ElementExists,
|
||||
"ELEMENTS": Elements,
|
||||
"ELEMENTS_COUNT": ElementsCount,
|
||||
"FOCUS": Focus,
|
||||
"HOVER": Hover,
|
||||
"INNER_HTML": GetInnerHTML,
|
||||
"INNER_HTML_SET": SetInnerHTML,
|
||||
|
||||
Reference in New Issue
Block a user