1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-07-17 01:32:22 +02:00

Refactored methods (#376)

* Refactored methods

* Fixed errors provided by go vet
This commit is contained in:
Tim Voronov
2019-09-06 23:15:27 -04:00
committed by GitHub
parent c773509469
commit 24370d8178
11 changed files with 65 additions and 152 deletions

View File

@ -325,18 +325,6 @@ func (doc *HTMLDocument) ClickBySelectorAll(ctx context.Context, selector values
return doc.element.ClickBySelectorAll(ctx, selector) return doc.element.ClickBySelectorAll(ctx, selector)
} }
func (doc *HTMLDocument) SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error) {
return doc.input.SelectBySelector(ctx, doc.element.id.nodeID, selector.String(), value)
}
func (doc *HTMLDocument) FocusBySelector(ctx context.Context, selector values.String) error {
return doc.input.FocusBySelector(ctx, doc.element.id.nodeID, selector.String())
}
func (doc *HTMLDocument) MoveMouseBySelector(ctx context.Context, selector values.String) error {
return doc.input.MoveMouseBySelector(ctx, doc.element.id.nodeID, selector.String())
}
func (doc *HTMLDocument) MoveMouseByXY(ctx context.Context, x, y values.Float) error { func (doc *HTMLDocument) MoveMouseByXY(ctx context.Context, x, y values.Float) error {
return doc.input.MoveMouseByXY(ctx, float64(x), float64(y)) return doc.input.MoveMouseByXY(ctx, float64(x), float64(y))
} }

View File

@ -1070,6 +1070,10 @@ func (el *HTMLElement) Select(ctx context.Context, value *values.Array) (*values
return el.input.Select(ctx, el.id.objectID, value) return el.input.Select(ctx, el.id.objectID, value)
} }
func (el *HTMLElement) SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error) {
return el.input.SelectBySelector(ctx, el.id.nodeID, selector.String(), value)
}
func (el *HTMLElement) ScrollIntoView(ctx context.Context) error { func (el *HTMLElement) ScrollIntoView(ctx context.Context) error {
return el.input.ScrollIntoView(ctx, el.id.objectID) return el.input.ScrollIntoView(ctx, el.id.objectID)
} }
@ -1078,10 +1082,18 @@ func (el *HTMLElement) Focus(ctx context.Context) error {
return el.input.Focus(ctx, el.id.objectID) return el.input.Focus(ctx, el.id.objectID)
} }
func (el *HTMLElement) FocusBySelector(ctx context.Context, selector values.String) error {
return el.input.FocusBySelector(ctx, el.id.nodeID, selector.String())
}
func (el *HTMLElement) Hover(ctx context.Context) error { func (el *HTMLElement) Hover(ctx context.Context) error {
return el.input.MoveMouse(ctx, el.id.objectID) return el.input.MoveMouse(ctx, el.id.objectID)
} }
func (el *HTMLElement) HoverBySelector(ctx context.Context, selector values.String) error {
return el.input.MoveMouseBySelector(ctx, el.id.nodeID, selector.String())
}
func (el *HTMLElement) IsDetached() values.Boolean { func (el *HTMLElement) IsDetached() values.Boolean {
el.mu.Lock() el.mu.Lock()
defer el.mu.Unlock() defer el.mu.Unlock()

View File

@ -115,7 +115,7 @@ func (doc *HTMLDocument) Copy() core.Value {
return cp return cp
} }
func (doc *HTMLDocument) Clone() core.Value { func (doc *HTMLDocument) Clone() core.Cloneable {
cloned, err := NewHTMLDocument(doc.doc, doc.url.String(), doc.parent) cloned, err := NewHTMLDocument(doc.doc, doc.url.String(), doc.parent)
if err != nil { if err != nil {
@ -215,18 +215,6 @@ func (doc *HTMLDocument) ClickBySelectorAll(_ context.Context, _ values.String)
return core.ErrNotSupported return core.ErrNotSupported
} }
func (doc *HTMLDocument) SelectBySelector(_ context.Context, _ values.String, _ *values.Array) (*values.Array, error) {
return nil, core.ErrNotSupported
}
func (doc *HTMLDocument) PrintToPDF(_ context.Context, _ drivers.PDFParams) (values.Binary, error) {
return nil, core.ErrNotSupported
}
func (doc *HTMLDocument) CaptureScreenshot(_ context.Context, _ drivers.ScreenshotParams) (values.Binary, error) {
return nil, core.ErrNotSupported
}
func (doc *HTMLDocument) ScrollTop(_ context.Context) error { func (doc *HTMLDocument) ScrollTop(_ context.Context) error {
return core.ErrNotSupported return core.ErrNotSupported
} }
@ -243,22 +231,10 @@ func (doc *HTMLDocument) ScrollByXY(_ context.Context, _, _ values.Float) error
return core.ErrNotSupported 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
}
func (doc *HTMLDocument) MoveMouseByXY(_ context.Context, _, _ values.Float) error { func (doc *HTMLDocument) MoveMouseByXY(_ context.Context, _, _ values.Float) error {
return core.ErrNotSupported return core.ErrNotSupported
} }
func (doc *HTMLDocument) WaitForNavigation(_ context.Context) error {
return core.ErrNotSupported
}
func (doc *HTMLDocument) WaitForElement(_ context.Context, _ values.String, _ drivers.WaitEvent) error { func (doc *HTMLDocument) WaitForElement(_ context.Context, _ values.String, _ drivers.WaitEvent) error {
return core.ErrNotSupported return core.ErrNotSupported
} }

View File

@ -521,6 +521,10 @@ func (el *HTMLElement) Select(_ context.Context, _ *values.Array) (*values.Array
return nil, core.ErrNotSupported return nil, core.ErrNotSupported
} }
func (el *HTMLElement) SelectBySelector(_ context.Context, _ values.String, _ *values.Array) (*values.Array, error) {
return nil, core.ErrNotSupported
}
func (el *HTMLElement) ScrollIntoView(_ context.Context) error { func (el *HTMLElement) ScrollIntoView(_ context.Context) error {
return core.ErrNotSupported return core.ErrNotSupported
} }
@ -529,10 +533,18 @@ func (el *HTMLElement) Focus(_ context.Context) error {
return core.ErrNotSupported return core.ErrNotSupported
} }
func (el *HTMLElement) FocusBySelector(_ context.Context, _ values.String) error {
return core.ErrNotSupported
}
func (el *HTMLElement) Hover(_ context.Context) error { func (el *HTMLElement) Hover(_ context.Context) error {
return core.ErrNotSupported return core.ErrNotSupported
} }
func (el *HTMLElement) HoverBySelector(_ context.Context, _ values.String) error {
return core.ErrNotSupported
}
func (el *HTMLElement) WaitForClass(_ context.Context, _ values.String, _ drivers.WaitEvent) error { func (el *HTMLElement) WaitForClass(_ context.Context, _ values.String, _ drivers.WaitEvent) error {
return core.ErrNotSupported return core.ErrNotSupported
} }

View File

@ -109,12 +109,18 @@ type (
Select(ctx context.Context, value *values.Array) (*values.Array, error) Select(ctx context.Context, value *values.Array) (*values.Array, error)
SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error)
ScrollIntoView(ctx context.Context) error ScrollIntoView(ctx context.Context) error
Focus(ctx context.Context) error Focus(ctx context.Context) error
FocusBySelector(ctx context.Context, selector values.String) error
Hover(ctx context.Context) error Hover(ctx context.Context) error
HoverBySelector(ctx context.Context, selector values.String) error
WaitForAttribute(ctx context.Context, name values.String, value core.Value, when WaitEvent) error WaitForAttribute(ctx context.Context, name values.String, value core.Value, when WaitEvent) error
WaitForStyle(ctx context.Context, name values.String, value core.Value, when WaitEvent) error WaitForStyle(ctx context.Context, name values.String, value core.Value, when WaitEvent) error
@ -137,8 +143,6 @@ type (
GetChildDocuments(ctx context.Context) (*values.Array, error) GetChildDocuments(ctx context.Context) (*values.Array, error)
SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error)
ScrollTop(ctx context.Context) error ScrollTop(ctx context.Context) error
ScrollBottom(ctx context.Context) error ScrollBottom(ctx context.Context) error
@ -147,12 +151,8 @@ type (
ScrollByXY(ctx context.Context, x, y values.Float) error 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 MoveMouseByXY(ctx context.Context, x, y values.Float) error
MoveMouseBySelector(ctx context.Context, selector values.String) error
WaitForElement(ctx context.Context, selector values.String, when WaitEvent) error WaitForElement(ctx context.Context, selector values.String, when WaitEvent) error
WaitForAttributeBySelector(ctx context.Context, selector, name values.String, value core.Value, when WaitEvent) error WaitForAttributeBySelector(ctx context.Context, selector, name values.String, value core.Value, when WaitEvent) error

View File

@ -325,7 +325,7 @@ func ToInt(input core.Value) Int {
} }
} }
func ToArray(ctx context.Context, input core.Value) core.Value { func ToArray(ctx context.Context, input core.Value) *Array {
switch value := input.(type) { switch value := input.(type) {
case Boolean, case Boolean,
Int, Int,
@ -335,7 +335,7 @@ func ToArray(ctx context.Context, input core.Value) core.Value {
return NewArrayWith(value) return NewArrayWith(value)
case *Array: case *Array:
return value.Copy() return value.Copy().(*Array)
case *Object: case *Object:
arr := NewArray(int(value.Length())) arr := NewArray(int(value.Length()))
@ -350,7 +350,7 @@ func ToArray(ctx context.Context, input core.Value) core.Value {
iterator, err := value.Iterate(ctx) iterator, err := value.Iterate(ctx)
if err != nil { if err != nil {
return None return NewArray(0)
} }
arr := NewArray(10) arr := NewArray(10)
@ -359,7 +359,7 @@ func ToArray(ctx context.Context, input core.Value) core.Value {
val, _, err := iterator.Next(ctx) val, _, err := iterator.Next(ctx)
if err != nil { if err != nil {
return None return NewArray(0)
} }
if val == None { if val == None {

View File

@ -390,9 +390,7 @@ func TestHelpers(t *testing.T) {
} }
input := values.NewArrayWith(vals...) input := values.NewArrayWith(vals...)
output := values.ToArray(context.Background(), input) arr := values.ToArray(context.Background(), input)
arr := output.(*values.Array)
So(input == arr, ShouldBeFalse) So(input == arr, ShouldBeFalse)
So(arr.Length() == input.Length(), ShouldBeTrue) So(arr.Length() == input.Length(), ShouldBeTrue)
@ -414,9 +412,7 @@ func TestHelpers(t *testing.T) {
values.NewObjectProperty("qaz", values.NewObject()), values.NewObjectProperty("qaz", values.NewObject()),
) )
output := values.ToArray(context.Background(), input) arr := values.ToArray(context.Background(), input).Sort()
arr := output.(*values.Array).Sort()
So(arr.String(), ShouldEqual, "[1,\"bar\",{}]") So(arr.String(), ShouldEqual, "[1,\"bar\",{}]")
So(arr.Get(values.NewInt(2)) == input.MustGet("qaz"), ShouldBeTrue) So(arr.Get(values.NewInt(2)) == input.MustGet("qaz"), ShouldBeTrue)

View File

@ -40,3 +40,7 @@ func (t *none) Hash() uint64 {
func (t *none) Copy() core.Value { func (t *none) Copy() core.Value {
return None return None
} }
func (t *none) Clone() core.Cloneable {
return None
}

View File

@ -2,6 +2,7 @@ package html
import ( import (
"context" "context"
"github.com/MontFerret/ferret/pkg/drivers" "github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values" "github.com/MontFerret/ferret/pkg/runtime/values"
@ -17,22 +18,15 @@ func Focus(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, err 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]) el, err := drivers.ToElement(args[0])
if err != nil { if err != nil {
return values.None, err return values.None, err
} }
return values.None, el.Focus(ctx) if len(args) == 1 {
return values.None, el.Focus(ctx)
}
return values.None, el.FocusBySelector(ctx, values.ToString(args[1]))
} }

View File

@ -20,63 +20,21 @@ func Hover(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, err return values.None, err
} }
// page or document or element el, err := drivers.ToElement(args[0])
err = core.ValidateType(args[0], drivers.HTMLPageType, drivers.HTMLDocumentType, drivers.HTMLElementType)
if err != nil { if err != nil {
return values.None, err return values.None, err
} }
selector := values.EmptyString if len(args) == 1 {
if len(args) > 1 {
err = core.ValidateType(args[1], types.String)
if err != nil {
return values.None, err
}
selector = args[1].(values.String)
}
switch n := args[0].(type) {
case drivers.HTMLPage:
if selector == values.EmptyString {
return values.None, core.Error(core.ErrMissedArgument, "selector")
}
return values.None, n.GetMainFrame().MoveMouseBySelector(ctx, selector)
case drivers.HTMLDocument:
if selector == values.EmptyString {
return values.None, core.Error(core.ErrMissedArgument, "selector")
}
return values.None, n.MoveMouseBySelector(ctx, selector)
case drivers.HTMLElement:
if selector == values.EmptyString {
return values.None, n.Hover(ctx)
}
found, err := n.QuerySelector(ctx, selector)
if err != nil {
return values.None, err
}
if found == values.None {
return values.None, core.Errorf(core.ErrNotFound, "element by selector %s", selector)
}
el, ok := found.(drivers.HTMLElement)
if !ok {
return values.None, core.Errorf(core.ErrNotFound, "element by selector %s", selector)
}
defer el.Close()
return values.None, el.Hover(ctx) return values.None, el.Hover(ctx)
default:
return values.None, core.TypeError(n.Type(), drivers.HTMLDocumentType, drivers.HTMLElementType)
} }
err = core.ValidateType(args[1], types.String)
if err != nil {
return values.None, err
}
return values.None, el.HoverBySelector(ctx, values.ToString(args[1]))
} }

View File

@ -6,7 +6,6 @@ import (
"github.com/MontFerret/ferret/pkg/drivers" "github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core" "github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values" "github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
) )
// Select selects a value from an underlying select element. // Select selects a value from an underlying select element.
@ -21,46 +20,20 @@ func Select(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, err return values.None, err
} }
arg1 := args[0] el, err := drivers.ToElement(args[0])
err = core.ValidateType(arg1, drivers.HTMLPageType, drivers.HTMLDocumentType, drivers.HTMLElementType)
if err != nil { if err != nil {
return values.False, err return values.None, err
} }
if arg1.Type() == drivers.HTMLPageType || arg1.Type() == drivers.HTMLDocumentType { if len(args) == 2 {
doc, err := drivers.ToDocument(arg1) arr := values.ToArray(ctx, args[1])
if err != nil { return el.Select(ctx, arr)
return values.None, err
}
// selector
arg2 := args[1]
err = core.ValidateType(arg2, types.String)
if err != nil {
return values.False, err
}
arg3 := args[2]
err = core.ValidateType(arg3, types.Array)
if err != nil {
return values.False, err
}
return doc.SelectBySelector(ctx, arg2.(values.String), arg3.(*values.Array))
} }
el := arg1.(drivers.HTMLElement) selector := values.ToString(args[1])
arg2 := args[1] arr := values.ToArray(ctx, args[2])
err = core.ValidateType(arg2, types.Array) return el.SelectBySelector(ctx, selector, arr)
if err != nil {
return values.False, err
}
return el.Select(ctx, arg2.(*values.Array))
} }