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

add the MouseMoveNode function #787

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ func MouseEvent(typ input.MouseType, x, y float64, opts ...MouseOption) MouseAct
return p
}





Copy link
Member

Choose a reason for hiding this comment

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

remove extra blank lines

Copy link
Member

Choose a reason for hiding this comment

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

remove extra blank lines

func MouseMoveXY(x, y float64, opts ...MouseOption) MouseAction {
return ActionFunc(func(ctx context.Context) error {
p := &input.DispatchMouseEventParams{
Type: input.MouseMoved,
X: x,
Y: y,
}
Copy link
Member

Choose a reason for hiding this comment

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

better to use the constructor func?

p := input.DispatchMouseEvent(input.MouseMoved, x, y)

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I just noticed this function here, it did the thing what I’m looking for for MouseMoveXY


// apply opts
for _, o := range opts {
p = o(p)
}

return p.Do(ctx)
})
}


// MouseClickXY is an action that sends a left mouse button click (ie,
// mousePressed and mouseReleased event) to the X, Y location.
func MouseClickXY(x, y float64, opts ...MouseOption) MouseAction {
Expand All @@ -49,6 +71,38 @@ func MouseClickXY(x, y float64, opts ...MouseOption) MouseAction {
})
}





Copy link
Member

Choose a reason for hiding this comment

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

remove extra blank lines

func MouseMoveNode(n *cdp.Node, opts ...MouseOption) MouseAction {
return ActionFunc(func(ctx context.Context) error {
Copy link
Member

@ZekeLu ZekeLu Apr 6, 2021

Choose a reason for hiding this comment

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

MouseClickNode does some check and scrolls the node into view, should we do the same here? If the answer is yes, maybe we should extract those code into a func.

Copy link
Author

Choose a reason for hiding this comment

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

The MouseMoveNode function is the similar function to the MouseClickNode, but it doesn’t do the click movement, just put the mouse on the target node.

boxes, err := dom.GetContentQuads().WithNodeID(n.NodeID).Do(ctx)
if err != nil {
return err
}
content := boxes[0]

c := len(content)
if c%2 != 0 || c < 1 {
return ErrInvalidDimensions
}

var x, y float64
for i := 0; i < c; i += 2 {
x += content[i]
y += content[i+1]
}
x /= float64(c / 2)
y /= float64(c / 2)

return MouseMoveXY(x, y, opts...).Do(ctx)
})
}




Copy link
Member

Choose a reason for hiding this comment

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

remove extra blank lines above.

// MouseClickNode is an action that dispatches a mouse left button click event
// at the center of a specified node.
//
Expand Down