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

Added errors to ClickX methods and added existence check #341

Merged
merged 3 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 7 additions & 19 deletions pkg/drivers/cdp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (doc *HTMLDocument) CountBySelector(ctx context.Context, selector values.St
return doc.element.CountBySelector(ctx, selector)
}

func (doc *HTMLDocument) ExistsBySelector(ctx context.Context, selector values.String) values.Boolean {
func (doc *HTMLDocument) ExistsBySelector(ctx context.Context, selector values.String) (values.Boolean, error) {
return doc.element.ExistsBySelector(ctx, selector)
}

Expand Down Expand Up @@ -317,28 +317,16 @@ func (doc *HTMLDocument) GetURL() values.String {
return values.NewString(doc.frames.Frame.URL)
}

func (doc *HTMLDocument) ClickBySelector(ctx context.Context, selector values.String) (values.Boolean, error) {
if err := doc.input.ClickBySelector(ctx, doc.element.id.nodeID, selector); err != nil {
return values.False, err
}

return values.True, nil
func (doc *HTMLDocument) ClickBySelector(ctx context.Context, selector values.String) error {
return doc.input.ClickBySelector(ctx, doc.element.id.nodeID, selector)
}

func (doc *HTMLDocument) ClickBySelectorAll(ctx context.Context, selector values.String) (values.Boolean, error) {
if err := doc.input.ClickBySelectorAll(ctx, doc.element.id.nodeID, selector); err != nil {
return values.False, err
}

return values.True, nil
func (doc *HTMLDocument) ClickBySelectorAll(ctx context.Context, selector values.String) error {
return doc.input.ClickBySelectorAll(ctx, doc.element.id.nodeID, selector)
}

func (doc *HTMLDocument) InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) (values.Boolean, error) {
if err := doc.input.TypeBySelector(ctx, doc.element.id.nodeID, selector, value, delay); err != nil {
return values.False, err
}

return values.True, nil
func (doc *HTMLDocument) InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) error {
return doc.input.TypeBySelector(ctx, doc.element.id.nodeID, selector, value, delay)
}

func (doc *HTMLDocument) SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error) {
Expand Down
22 changes: 7 additions & 15 deletions pkg/drivers/cdp/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,28 +850,24 @@ func (el *HTMLElement) CountBySelector(ctx context.Context, selector values.Stri
return values.NewInt(len(res.NodeIDs))
}

func (el *HTMLElement) ExistsBySelector(ctx context.Context, selector values.String) values.Boolean {
func (el *HTMLElement) ExistsBySelector(ctx context.Context, selector values.String) (values.Boolean, error) {
if el.IsDetached() {
return values.False
return values.False, drivers.ErrDetached
}

// TODO: Can we use RemoteObjectID or BackendID instead of NodeId?
selectorArgs := dom.NewQuerySelectorArgs(el.id.nodeID, selector.String())
res, err := el.client.DOM.QuerySelector(ctx, selectorArgs)

if err != nil {
el.logError(err).
Str("selector", selector.String()).
Msg("failed to retrieve nodes by selector")

return values.False
return values.False, err
}

if res.NodeID == 0 {
return values.False
return values.False, nil
}

return values.True
return values.True, nil
}

func (el *HTMLElement) WaitForClass(ctx context.Context, class values.String, when drivers.WaitEvent) error {
Expand Down Expand Up @@ -947,12 +943,8 @@ func (el *HTMLElement) WaitForStyle(ctx context.Context, name values.String, val
return err
}

func (el *HTMLElement) Click(ctx context.Context) (values.Boolean, error) {
if err := el.input.Click(ctx, el.id.objectID); err != nil {
return values.False, err
}

return values.True, nil
func (el *HTMLElement) Click(ctx context.Context) error {
return el.input.Click(ctx, el.id.objectID)
}

func (el *HTMLElement) Input(ctx context.Context, value core.Value, delay values.Int) error {
Expand Down
14 changes: 7 additions & 7 deletions pkg/drivers/http/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (doc *HTMLDocument) CountBySelector(ctx context.Context, selector values.St
return doc.element.CountBySelector(ctx, selector)
}

func (doc *HTMLDocument) ExistsBySelector(ctx context.Context, selector values.String) values.Boolean {
func (doc *HTMLDocument) ExistsBySelector(ctx context.Context, selector values.String) (values.Boolean, error) {
return doc.element.ExistsBySelector(ctx, selector)
}

Expand Down Expand Up @@ -207,16 +207,16 @@ func (doc *HTMLDocument) GetParentDocument() drivers.HTMLDocument {
return doc.parent
}

func (doc *HTMLDocument) ClickBySelector(_ context.Context, _ values.String) (values.Boolean, error) {
return false, core.ErrNotSupported
func (doc *HTMLDocument) ClickBySelector(_ context.Context, _ values.String) error {
return core.ErrNotSupported
}

func (doc *HTMLDocument) ClickBySelectorAll(_ context.Context, _ values.String) (values.Boolean, error) {
return false, core.ErrNotSupported
func (doc *HTMLDocument) ClickBySelectorAll(_ context.Context, _ values.String) error {
return core.ErrNotSupported
}

func (doc *HTMLDocument) InputBySelector(_ context.Context, _ values.String, _ core.Value, _ values.Int) (values.Boolean, error) {
return false, core.ErrNotSupported
func (doc *HTMLDocument) InputBySelector(_ context.Context, _ values.String, _ core.Value, _ values.Int) error {
return core.ErrNotSupported
}

func (doc *HTMLDocument) SelectBySelector(_ context.Context, _ values.String, _ *values.Array) (*values.Array, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/drivers/http/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,14 @@ func (el *HTMLElement) CountBySelector(_ context.Context, selector values.String
return values.NewInt(selection.Size())
}

func (el *HTMLElement) ExistsBySelector(_ context.Context, selector values.String) values.Boolean {
func (el *HTMLElement) ExistsBySelector(_ context.Context, selector values.String) (values.Boolean, error) {
selection := el.selection.Closest(selector.String())

if selection == nil {
return values.False
return values.False, nil
}

return values.True
return values.True, nil
}

func (el *HTMLElement) GetIn(ctx context.Context, path []core.Value) (core.Value, error) {
Expand All @@ -489,8 +489,8 @@ func (el *HTMLElement) Iterate(_ context.Context) (core.Iterator, error) {
return common.NewIterator(el)
}

func (el *HTMLElement) Click(_ context.Context) (values.Boolean, error) {
return false, core.ErrNotSupported
func (el *HTMLElement) Click(_ context.Context) error {
return core.ErrNotSupported
}

func (el *HTMLElement) Input(_ context.Context, _ core.Value, _ values.Int) error {
Expand Down
10 changes: 5 additions & 5 deletions pkg/drivers/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type (

CountBySelector(ctx context.Context, selector values.String) values.Int

ExistsBySelector(ctx context.Context, selector values.String) values.Boolean
ExistsBySelector(ctx context.Context, selector values.String) (values.Boolean, error)

XPath(ctx context.Context, expression values.String) (core.Value, error)
}
Expand Down Expand Up @@ -93,7 +93,7 @@ type (

GetInnerTextBySelectorAll(ctx context.Context, selector values.String) (*values.Array, error)

Click(ctx context.Context) (values.Boolean, error)
Click(ctx context.Context) error

Input(ctx context.Context, value core.Value, delay values.Int) error

Expand Down Expand Up @@ -127,11 +127,11 @@ type (

GetChildDocuments(ctx context.Context) (*values.Array, error)

ClickBySelector(ctx context.Context, selector values.String) (values.Boolean, error)
ClickBySelector(ctx context.Context, selector values.String) error

ClickBySelectorAll(ctx context.Context, selector values.String) (values.Boolean, error)
ClickBySelectorAll(ctx context.Context, selector values.String) error

InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) (values.Boolean, error)
InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) error

SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error)

Expand Down
15 changes: 12 additions & 3 deletions pkg/stdlib/html/click.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Click(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.False, err
}

return el.Click(ctx)
return values.True, el.Click(ctx)
}

// CLICK(doc, selector)
Expand All @@ -36,7 +36,16 @@ func Click(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.False, err
}

selector := args[1].String()
selector := values.ToString(args[1])
exists, err := doc.ExistsBySelector(ctx, selector)

return doc.ClickBySelector(ctx, values.NewString(selector))
if err != nil {
return values.False, err
}

if !exists {
return exists, nil
}

return exists, doc.ClickBySelector(ctx, selector)
}
14 changes: 12 additions & 2 deletions pkg/stdlib/html/click_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ func ClickAll(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, err
}

selector := args[1].String()
selector := values.ToString(args[1])

return doc.ClickBySelectorAll(ctx, values.NewString(selector))
exists, err := doc.ExistsBySelector(ctx, selector)

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

if !exists {
return values.False, nil
}

return values.True, doc.ClickBySelectorAll(ctx, selector)
}
2 changes: 1 addition & 1 deletion pkg/stdlib/html/element_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func ElementExists(ctx context.Context, args ...core.Value) (core.Value, error)
return values.None, err
}

return el.ExistsBySelector(ctx, selector), nil
return el.ExistsBySelector(ctx, selector)
}
15 changes: 13 additions & 2 deletions pkg/stdlib/html/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func Input(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.False, err
}

selector := values.ToString(arg2)
delay := values.Int(0)

if len(args) == 4 {
Expand All @@ -55,10 +56,20 @@ func Input(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.False, err
}

delay = arg4.(values.Int)
delay = values.ToInt(arg4)
}

return doc.InputBySelector(ctx, arg2.(values.String), args[2], delay)
exists, err := doc.ExistsBySelector(ctx, selector)

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

if !exists {
return values.False, nil
}

return values.True, doc.InputBySelector(ctx, selector, args[2], delay)
}

el, err := drivers.ToElement(arg1)
Expand Down
10 changes: 8 additions & 2 deletions pkg/stdlib/html/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ func (i *PagingIterator) Next(ctx context.Context) (core.Value, core.Value, erro
return values.ZeroInt, values.ZeroInt, nil
}

if !i.document.ExistsBySelector(ctx, i.selector) {
exists, err := i.document.ExistsBySelector(ctx, i.selector)

if err != nil {
return values.ZeroInt, values.ZeroInt, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe return values.None instead values.ZeroInt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not really matter, since an execution gets terminated because of the returned error.

Copy link
Member

@3timeslazy 3timeslazy Jul 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent I'm suggest to return values.None every time error occurred.

}

if !exists {
return values.ZeroInt, values.ZeroInt, core.ErrNoMoreData
}

_, err := i.document.ClickBySelector(ctx, i.selector)
err = i.document.ClickBySelector(ctx, i.selector)

if err != nil {
return values.None, values.None, err
Expand Down