Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored methods #376

Merged
merged 2 commits into from
Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions pkg/drivers/cdp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,6 @@ func (doc *HTMLDocument) ClickBySelectorAll(ctx context.Context, selector values
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 {
return doc.input.MoveMouseByXY(ctx, float64(x), float64(y))
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/drivers/cdp/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,10 @@ func (el *HTMLElement) Select(ctx context.Context, value *values.Array) (*values
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 {
return el.input.ScrollIntoView(ctx, el.id.objectID)
}
Expand All @@ -1078,10 +1082,18 @@ func (el *HTMLElement) Focus(ctx context.Context) error {
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 {
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 {
el.mu.Lock()
defer el.mu.Unlock()
Expand Down
26 changes: 1 addition & 25 deletions pkg/drivers/http/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (doc *HTMLDocument) Copy() core.Value {
return cp
}

func (doc *HTMLDocument) Clone() core.Value {
func (doc *HTMLDocument) Clone() core.Cloneable {
cloned, err := NewHTMLDocument(doc.doc, doc.url.String(), doc.parent)

if err != nil {
Expand Down Expand Up @@ -215,18 +215,6 @@ func (doc *HTMLDocument) ClickBySelectorAll(_ context.Context, _ values.String)
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 {
return core.ErrNotSupported
}
Expand All @@ -243,22 +231,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
}

func (doc *HTMLDocument) MoveMouseByXY(_ context.Context, _, _ values.Float) error {
return core.ErrNotSupported
}

func (doc *HTMLDocument) WaitForNavigation(_ context.Context) error {
return core.ErrNotSupported
}

func (doc *HTMLDocument) WaitForElement(_ context.Context, _ values.String, _ drivers.WaitEvent) error {
return core.ErrNotSupported
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/drivers/http/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ func (el *HTMLElement) Select(_ context.Context, _ *values.Array) (*values.Array
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 {
return core.ErrNotSupported
}
Expand All @@ -529,10 +533,18 @@ func (el *HTMLElement) Focus(_ context.Context) error {
return core.ErrNotSupported
}

func (el *HTMLElement) FocusBySelector(_ context.Context, _ values.String) error {
return core.ErrNotSupported
}

func (el *HTMLElement) Hover(_ context.Context) error {
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 {
return core.ErrNotSupported
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/drivers/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,18 @@ type (

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

Focus(ctx context.Context) error

FocusBySelector(ctx context.Context, selector values.String) 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

WaitForStyle(ctx context.Context, name values.String, value core.Value, when WaitEvent) error
Expand All @@ -137,8 +143,6 @@ type (

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

ScrollBottom(ctx context.Context) error
Expand All @@ -147,12 +151,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

WaitForElement(ctx context.Context, selector values.String, when WaitEvent) error

WaitForAttributeBySelector(ctx context.Context, selector, name values.String, value core.Value, when WaitEvent) error
Expand Down
8 changes: 4 additions & 4 deletions pkg/runtime/values/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
case Boolean,
Int,
Expand All @@ -335,7 +335,7 @@ func ToArray(ctx context.Context, input core.Value) core.Value {

return NewArrayWith(value)
case *Array:
return value.Copy()
return value.Copy().(*Array)
case *Object:
arr := NewArray(int(value.Length()))

Expand All @@ -350,7 +350,7 @@ func ToArray(ctx context.Context, input core.Value) core.Value {
iterator, err := value.Iterate(ctx)

if err != nil {
return None
return NewArray(0)
}

arr := NewArray(10)
Expand All @@ -359,7 +359,7 @@ func ToArray(ctx context.Context, input core.Value) core.Value {
val, _, err := iterator.Next(ctx)

if err != nil {
return None
return NewArray(0)
}

if val == None {
Expand Down
8 changes: 2 additions & 6 deletions pkg/runtime/values/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,7 @@ func TestHelpers(t *testing.T) {
}

input := values.NewArrayWith(vals...)
output := values.ToArray(context.Background(), input)

arr := output.(*values.Array)
arr := values.ToArray(context.Background(), input)

So(input == arr, ShouldBeFalse)
So(arr.Length() == input.Length(), ShouldBeTrue)
Expand All @@ -414,9 +412,7 @@ func TestHelpers(t *testing.T) {
values.NewObjectProperty("qaz", values.NewObject()),
)

output := values.ToArray(context.Background(), input)

arr := output.(*values.Array).Sort()
arr := values.ToArray(context.Background(), input).Sort()

So(arr.String(), ShouldEqual, "[1,\"bar\",{}]")
So(arr.Get(values.NewInt(2)) == input.MustGet("qaz"), ShouldBeTrue)
Expand Down
4 changes: 4 additions & 0 deletions pkg/runtime/values/none.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ func (t *none) Hash() uint64 {
func (t *none) Copy() core.Value {
return None
}

func (t *none) Clone() core.Cloneable {
return None
}
18 changes: 6 additions & 12 deletions pkg/stdlib/html/focus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package html

import (
"context"

"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
Expand All @@ -17,22 +18,15 @@ func Focus(ctx context.Context, args ...core.Value) (core.Value, error) {
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)
if len(args) == 1 {
return values.None, el.Focus(ctx)
}

return values.None, el.FocusBySelector(ctx, values.ToString(args[1]))
}
58 changes: 8 additions & 50 deletions pkg/stdlib/html/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,21 @@ func Hover(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, err
}

// page or document or element
err = core.ValidateType(args[0], drivers.HTMLPageType, drivers.HTMLDocumentType, drivers.HTMLElementType)
el, err := drivers.ToElement(args[0])

if err != nil {
return values.None, err
}

selector := values.EmptyString

if len(args) > 1 {
err = core.ValidateType(args[1], types.String)

if err != nil {
return values.None, err
}

selector = args[1].(values.String)
if len(args) == 1 {
return values.None, el.Hover(ctx)
}

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
}
err = core.ValidateType(args[1], types.String)

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)
default:
return values.None, core.TypeError(n.Type(), drivers.HTMLDocumentType, drivers.HTMLElementType)
if err != nil {
return values.None, err
}

return values.None, el.HoverBySelector(ctx, values.ToString(args[1]))
}
Loading