Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: drag <from> <to> silently no-ops on HTML5 draggables while the dragX/dragY form works, though the help presents them as equivalent (PIN-130)

Luigi Agosti6722fa84eea3
diff --git a/cmd/pinchtab/cmd_cli_browser_actions.go b/cmd/pinchtab/cmd_cli_browser_actions.go
index e5d6a22f..739ae569 100644
--- a/cmd/pinchtab/cmd_cli_browser_actions.go
+++ b/cmd/pinchtab/cmd_cli_browser_actions.go
@@ -88,14 +88,19 @@ var dragCmd = &cobra.Command{
Short: "Drag from one target to another (or by pixel offset)",
Long: `Drag a DOM element.
-Two forms:
+Two forms, both one HTTP "drag" action. They differ in how the endpoint of the
+drag is expressed — a target or an offset — and both drive the interpolated
+pointer sequence HTML5 drag-and-drop needs, so a draggable element fires
+dragstart and the destination receives drop.
+
pinchtab drag <from> <to>
- Synthesizes mouse-move → mouse-down → mouse-move → mouse-up.
- Each target is a selector (CSS, ref, text:) or an "x,y" coord pair.
+ TARGET form: drag onto another element. Each target is a selector (CSS,
+ ref, text:) or an "x,y" coord pair. Symmetric with the HTTP /action body
+ {"kind":"drag","selector":"...","toSelector":"..."}.
pinchtab drag <selector> --drag-x <n> --drag-y <n>
- Single-step HTTP "drag" action with pixel offsets from the element's
- current position. Symmetric with the HTTP /action body
+ OFFSET form: drag by a pixel delta from the element's current position,
+ for handles and sliders with no element to drop onto. Symmetric with
{"kind":"drag","selector":"...","dragX":N,"dragY":N}.`,
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
diff --git a/docs/reference/mouse.md b/docs/reference/mouse.md
index c1dc65ab..0418752e 100644
--- a/docs/reference/mouse.md
+++ b/docs/reference/mouse.md
@@ -46,7 +46,14 @@ Notes:
- `mouse move` accepts either coordinates or a unified selector.
- `mouse down` and `mouse up` accept an optional selector. Without one, they use the current pointer position.
- `mouse wheel` accepts either a delta form (`<dy> [--dx <n>]`) or an optional selector. Without a selector, it uses the current pointer position.
-- `drag <from> <to>` accepts selector/ref targets or `x,y` coordinate pairs.
+- `drag <from> <to>` accepts selector/ref targets or `x,y` coordinate pairs on either side.
+- `drag` has two forms and they differ only in how the end of the drag is expressed: a
+ target (`drag <from> <to>`, `toSelector`/`toX`/`toY`) or an offset (`--drag-x/--drag-y`,
+ `dragX`/`dragY`). Both are one action that interpolates the pointer between the two
+ points, which is what makes HTML5 drag-and-drop work — Chrome starts a drag only after
+ sustained movement follows the press, so a single jump fires no `dragstart` at all.
+ Assembling a drag yourself out of `mouse move`/`mouse down`/`mouse up` has that problem:
+ use `drag` for anything with a `draggable` source.
- `button` supports `left`, `right`, and `middle`.
- Pointer move uses a bounded CDP `mouseMoved` dispatch first. If headless Chromium stalls that dispatch, PinchTab falls back to DOM mouse events so hover and mouse-move flows remain responsive.
diff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.go
index 3387a0a7..70efd94f 100644
--- a/internal/bridge/action_pointer.go
+++ b/internal/bridge/action_pointer.go
@@ -606,6 +606,9 @@ func (b *Bridge) actionScroll(ctx context.Context, req ActionRequest) (map[strin
}
func (b *Bridge) actionDrag(ctx context.Context, req ActionRequest) (map[string]any, error) {
+ if req.hasDragDestination() {
+ return b.dragToDestination(ctx, req)
+ }
if req.DragX == 0 && req.DragY == 0 {
return nil, fmt.Errorf("dragX or dragY required for drag")
}
@@ -630,6 +633,60 @@ func (b *Bridge) actionDrag(ctx context.Context, req ActionRequest) (map[string]
return nil, fmt.Errorf("need selector, ref, or nodeId")
}
+// dragToDestination is the target form: source and destination in one action, so the
+// interpolated move sequence Chrome needs to recognise a drag happens between them. The
+// zero-offset refusal above does not apply here — a drop onto an overlapping target is a
+// legitimate request, and it is the destination, not a delta, that says where to go.
+func (b *Bridge) dragToDestination(ctx context.Context, req ActionRequest) (map[string]any, error) {
+ fromX, fromY, err := dragPointFor(ctx, dragEnd{nodeID: req.NodeID, selector: req.Selector, x: req.X, y: req.Y, hasPoint: req.HasXY})
+ if err != nil {
+ return nil, fmt.Errorf("drag source: %w", err)
+ }
+ toX, toY, err := dragPointFor(ctx, dragEnd{nodeID: req.ToNodeID, selector: req.ToSelector, x: req.ToX, y: req.ToY, hasPoint: req.HasToXY})
+ if err != nil {
+ return nil, fmt.Errorf("drag destination: %w", err)
+ }
+ if err := DragBetweenPoints(ctx, fromX, fromY, toX, toY); err != nil {
+ return nil, err
+ }
+ return map[string]any{
+ "dragged": true,
+ "fromX": fromX,
+ "fromY": fromY,
+ "toX": toX,
+ "toY": toY,
+ }, nil
+}
+
+// dragEnd is one end of a target drag. Both ends accept the same three vocabularies, so
+// they resolve through one function rather than two bodies that can drift apart.
+type dragEnd struct {
+ nodeID int64
+ selector string
+ x, y float64
+ hasPoint bool
+}
+
+func dragPointFor(ctx context.Context, end dragEnd) (float64, float64, error) {
+ switch {
+ case end.nodeID > 0:
+ return PointerPointForNode(ctx, end.nodeID, true)
+ case end.selector != "":
+ node, err := firstNodeBySelector(ctx, end.selector)
+ if err != nil {
+ return 0, 0, err
+ }
+ return PointerPointForNode(ctx, int64(node.BackendNodeID), true)
+ case end.hasPoint:
+ return end.x, end.y, nil
+ }
+ return 0, 0, fmt.Errorf("need selector, ref, nodeId, or coordinates")
+}
+
+func (r ActionRequest) hasDragDestination() bool {
+ return r.ToNodeID > 0 || r.ToSelector != "" || r.HasToXY
+}
+
func (b *Bridge) actionHumanizedClick(ctx context.Context, req ActionRequest) (result map[string]any, err error) {
auto := b.beginAutoSwitch(req)
defer func() { result = auto.settle(ctx, result, err) }()
diff --git a/internal/bridge/action_pointer_drag_test.go b/internal/bridge/action_pointer_drag_test.go
new file mode 100644
index 00000000..6ac5a5d0
--- /dev/null
+++ b/internal/bridge/action_pointer_drag_test.go
@@ -0,0 +1,260 @@
+package bridge
+
+import (
+ "context"
+ "encoding/base64"
+ "os"
+ "testing"
+ "time"
+
+ "github.com/chromedp/cdproto/cdp"
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/testbrowser"
+)
+
+// Two independent pairs in one page: #mouseSrc/#mouseDst answer plain mousedown/mouseup,
+// #h5Src/#h5Dst are real HTML5 drag-and-drop. A page-level record is what has to be
+// asserted — the action's own dragged:true was already there while nothing happened.
+const dragFixtureHTML = `<style>div{width:120px;height:80px;display:inline-block;border:1px solid #000}</style>
+<div id="h5Src" draggable="true">h5 src</div><div id="h5Dst">h5 dst</div><br>
+<div id="mouseSrc">mouse src</div><div id="mouseDst">mouse dst</div>
+<script>
+window.h5 = 'no';
+window.mouse = 'no';
+const h5Src = document.getElementById('h5Src'), h5Dst = document.getElementById('h5Dst');
+h5Src.addEventListener('dragstart', e => { window.h5 = 'started'; e.dataTransfer.setData('text/plain', 'payload'); });
+h5Dst.addEventListener('dragover', e => e.preventDefault());
+h5Dst.addEventListener('drop', e => { e.preventDefault(); window.h5 = 'DROPPED'; });
+const mouseSrc = document.getElementById('mouseSrc'), mouseDst = document.getElementById('mouseDst');
+let pressed = false;
+mouseSrc.addEventListener('mousedown', () => { pressed = true; });
+mouseDst.addEventListener('mouseup', () => { if (pressed) window.mouse = 'DROPPED'; });
+</script>`
+
+type dragFixture struct {
+ ctx context.Context
+ b *Bridge
+}
+
+func newDragFixture(t *testing.T) *dragFixture {
+ t.Helper()
+ chromePath := testbrowser.Path(t)
+
+ profile, err := os.MkdirTemp("", "pinchtab-drag-")
+ if err != nil {
+ t.Fatal(err)
+ }
+ alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), append(
+ chromedp.DefaultExecAllocatorOptions[:],
+ chromedp.ExecPath(chromePath),
+ chromedp.UserDataDir(profile),
+ chromedp.Flag("headless", true),
+ chromedp.Flag("no-sandbox", true),
+ )...)
+ ctx, cancelBrowser := chromedp.NewContext(alloc)
+ ctx, cancelTimeout := context.WithTimeout(ctx, 60*time.Second)
+ t.Cleanup(func() {
+ cancelTimeout()
+ cancelBrowser()
+ cancelAlloc()
+ _ = os.RemoveAll(profile)
+ })
+
+ return &dragFixture{ctx: ctx, b: &Bridge{}}
+}
+
+// Each trial reloads, so one trial cannot inherit another's drop.
+func (f *dragFixture) reload(t *testing.T) {
+ t.Helper()
+ url := "data:text/html;base64," + base64.StdEncoding.EncodeToString([]byte(dragFixtureHTML))
+ if err := chromedp.Run(f.ctx, chromedp.Navigate(url)); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func (f *dragFixture) state(t *testing.T) (html5 string, mouse string) {
+ t.Helper()
+ if err := chromedp.Run(f.ctx,
+ chromedp.Evaluate(`window.h5`, &html5),
+ chromedp.Evaluate(`window.mouse`, &mouse),
+ ); err != nil {
+ t.Fatal(err)
+ }
+ return html5, mouse
+}
+
+func (f *dragFixture) nodeID(t *testing.T, selector string) int64 {
+ t.Helper()
+ var nodes []*cdp.Node
+ if err := chromedp.Run(f.ctx, chromedp.Nodes(selector, &nodes, chromedp.ByQuery)); err != nil {
+ t.Fatalf("%s: %v", selector, err)
+ }
+ if len(nodes) == 0 {
+ t.Fatalf("%s matched nothing", selector)
+ }
+ return int64(nodes[0].BackendNodeID)
+}
+
+func (f *dragFixture) point(t *testing.T, selector string) (float64, float64) {
+ t.Helper()
+ x, y, err := PointerPointForNode(f.ctx, f.nodeID(t, selector), true)
+ if err != nil {
+ t.Fatalf("%s: %v", selector, err)
+ }
+ return x, y
+}
+
+// The defect: `drag <from> <to>` reported three OKs and did nothing on an HTML5 draggable,
+// while the offset form worked. Both forms now express the same drag, so both complete it.
+func TestDragCompletesAnHTML5DropInBothForms(t *testing.T) {
+ f := newDragFixture(t)
+
+ for _, tc := range []struct {
+ name string
+ body func(t *testing.T) ActionRequest
+ }{
+ {
+ name: "target form",
+ body: func(t *testing.T) ActionRequest {
+ return ActionRequest{Kind: ActionDrag, Selector: "#h5Src", ToSelector: "#h5Dst"}
+ },
+ },
+ {
+ // What the handler hands the bridge once it has resolved a ref or a semantic
+ // selector: the destination as a node id.
+ name: "target form by node id",
+ body: func(t *testing.T) ActionRequest {
+ return ActionRequest{Kind: ActionDrag, NodeID: f.nodeID(t, "#h5Src"), ToNodeID: f.nodeID(t, "#h5Dst")}
+ },
+ },
+ {
+ name: "target form by coordinates",
+ body: func(t *testing.T) ActionRequest {
+ x, y := f.point(t, "#h5Dst")
+ return ActionRequest{Kind: ActionDrag, Selector: "#h5Src", ToX: x, ToY: y, HasToXY: true}
+ },
+ },
+ {
+ name: "offset form",
+ body: func(t *testing.T) ActionRequest {
+ srcX, srcY := f.point(t, "#h5Src")
+ dstX, dstY := f.point(t, "#h5Dst")
+ return ActionRequest{Kind: ActionDrag, Selector: "#h5Src", DragX: int(dstX - srcX), DragY: int(dstY - srcY)}
+ },
+ },
+ } {
+ f.reload(t)
+ req := tc.body(t)
+
+ result, err := f.b.actionDrag(f.ctx, req)
+ if err != nil {
+ t.Fatalf("%s: %v", tc.name, err)
+ }
+ if result["dragged"] != true {
+ t.Errorf("%s: result = %v, want dragged:true", tc.name, result)
+ }
+
+ html5, _ := f.state(t)
+ if html5 != "DROPPED" {
+ t.Errorf("%s: page recorded h5=%q, want DROPPED; %q means dragstart never fired at all", tc.name, html5, "no")
+ }
+ }
+}
+
+// The mouse-event pair works today and must keep working: the fix cannot be a trade of one
+// drag style for the other.
+func TestDragStillCompletesAMouseDrivenDrop(t *testing.T) {
+ f := newDragFixture(t)
+ f.reload(t)
+
+ if _, err := f.b.actionDrag(f.ctx, ActionRequest{Kind: ActionDrag, Selector: "#mouseSrc", ToSelector: "#mouseDst"}); err != nil {
+ t.Fatal(err)
+ }
+
+ _, mouse := f.state(t)
+ if mouse != "DROPPED" {
+ t.Errorf("page recorded mouse=%q, want DROPPED", mouse)
+ }
+}
+
+// The two forms are documented as forms of one command, so the same source and destination
+// must produce the same page state whichever one expresses it.
+func TestBothDragFormsProduceTheSameOutcome(t *testing.T) {
+ f := newDragFixture(t)
+
+ f.reload(t)
+ if _, err := f.b.actionDrag(f.ctx, ActionRequest{Kind: ActionDrag, Selector: "#h5Src", ToSelector: "#h5Dst"}); err != nil {
+ t.Fatal(err)
+ }
+ targetH5, targetMouse := f.state(t)
+
+ f.reload(t)
+ srcX, srcY := f.point(t, "#h5Src")
+ dstX, dstY := f.point(t, "#h5Dst")
+ if _, err := f.b.actionDrag(f.ctx, ActionRequest{Kind: ActionDrag, Selector: "#h5Src", DragX: int(dstX - srcX), DragY: int(dstY - srcY)}); err != nil {
+ t.Fatal(err)
+ }
+ offsetH5, offsetMouse := f.state(t)
+
+ if targetH5 != offsetH5 || targetMouse != offsetMouse {
+ t.Errorf("target form left (h5=%q mouse=%q), offset form left (h5=%q mouse=%q); the help presents them as forms of one command", targetH5, targetMouse, offsetH5, offsetMouse)
+ }
+}
+
+// A drop onto an overlapping or same-position target is a legitimate request. The offset
+// form has to refuse 0,0 — it means "no movement asked for" — but the target form must not
+// inherit a refusal written for a different question.
+func TestATargetDragOntoTheSourceIsNotRefusedAsAZeroOffset(t *testing.T) {
+ f := newDragFixture(t)
+ f.reload(t)
+
+ if _, err := f.b.actionDrag(f.ctx, ActionRequest{Kind: ActionDrag, Selector: "#h5Src", ToSelector: "#h5Src"}); err != nil {
+ t.Fatalf("a drag onto its own source was refused: %v", err)
+ }
+
+ if _, err := f.b.actionDrag(f.ctx, ActionRequest{Kind: ActionDrag, Selector: "#h5Src"}); err == nil {
+ t.Error("the offset form accepted a drag with no offset and no destination, so a caller who forgot both reads it as success")
+ }
+}
+
+// The individual pointer primitives are fine for what they are, and the fix must not have
+// changed what they do for everyone else: four requests still move the pointer in one jump,
+// which is exactly why `drag` can no longer be assembled out of them.
+func TestThePointerPrimitivesStillDispatchOneJumpEach(t *testing.T) {
+ f := newDragFixture(t)
+
+ f.reload(t)
+ f.handAssembledDrag(t, "#mouseSrc", "#mouseDst")
+ if _, mouse := f.state(t); mouse != "DROPPED" {
+ t.Errorf("the primitives stopped completing a mouse-driven drop: mouse=%q", mouse)
+ }
+
+ f.reload(t)
+ f.handAssembledDrag(t, "#h5Src", "#h5Dst")
+ if html5, _ := f.state(t); html5 != "no" {
+ t.Errorf("hand-assembled pointer steps recorded h5=%q; if they now complete an HTML5 drop, this test records an obsolete limitation and `drag` no longer needs to own the sequence", html5)
+ }
+}
+
+// handAssembledDrag is the sequence the CLI used to post as four independent requests: one
+// move to the source, a press, ONE move to the destination, a release.
+func (f *dragFixture) handAssembledDrag(t *testing.T, from, to string) {
+ t.Helper()
+
+ srcX, srcY := f.point(t, from)
+ dstX, dstY := f.point(t, to)
+ for _, step := range []struct {
+ kind string
+ action ActionFunc
+ req ActionRequest
+ }{
+ {kind: ActionMouseMove, action: f.b.actionMouseMove, req: ActionRequest{X: srcX, Y: srcY, HasXY: true}},
+ {kind: ActionMouseDown, action: f.b.actionMouseDown, req: ActionRequest{X: srcX, Y: srcY, HasXY: true}},
+ {kind: ActionMouseMove, action: f.b.actionMouseMove, req: ActionRequest{X: dstX, Y: dstY, HasXY: true}},
+ {kind: ActionMouseUp, action: f.b.actionMouseUp, req: ActionRequest{X: dstX, Y: dstY, HasXY: true}},
+ } {
+ if _, err := step.action(f.ctx, step.req); err != nil {
+ t.Fatalf("%s: %v", step.kind, err)
+ }
+ }
+}
diff --git a/internal/bridge/action_request.go b/internal/bridge/action_request.go
index 5b5edb84..7ad61cc3 100644
--- a/internal/bridge/action_request.go
+++ b/internal/bridge/action_request.go
@@ -87,6 +87,19 @@ type ActionRequest struct {
DragX int `json:"dragX"`
DragY int `json:"dragY"`
+ // ToSelector is a drag's DESTINATION, in the same unified selector vocabulary as
+ // Selector (ref, CSS, text:), resolved to ToNodeID by the handler. It is what makes
+ // "drag this onto that" one action: assembling it from four pointer requests moved the
+ // pointer in a single jump, which Chrome never reads as the start of a drag, so an
+ // HTML5 draggable saw nothing while every request answered OK. ToX/ToY are the same
+ // destination given as a point, and HasToXY separates an absent destination from the
+ // legitimate one at (0,0).
+ ToSelector string `json:"toSelector,omitempty"`
+ ToNodeID int64 `json:"toNodeId,omitempty"`
+ ToX float64 `json:"toX,omitempty"`
+ ToY float64 `json:"toY,omitempty"`
+ HasToXY bool `json:"hasToXY,omitempty"`
+
WaitNav bool `json:"waitNav"`
Fast bool `json:"fast"`
Owner string `json:"owner"`
@@ -149,6 +162,7 @@ func (r *ActionRequest) UnmarshalJSON(data []byte) error {
*r = ActionRequest(alias)
r.Kind = CanonicalActionKind(r.Kind)
r.HasXY = r.HasXY || hasJSONKey(raw, "x") || hasJSONKey(raw, "y")
+ r.HasToXY = r.HasToXY || hasJSONKey(raw, "toX") || hasJSONKey(raw, "toY")
r.HasText = r.HasText || hasJSONKey(raw, "text") || hasJSONKey(raw, "value")
if hasJSONKey(raw, "deltaX") {
if err := json.Unmarshal(raw["deltaX"], &r.DeltaX); err != nil {
diff --git a/internal/bridge/cdpops/pointer.go b/internal/bridge/cdpops/pointer.go
index 1fe5296b..fd7707d7 100644
--- a/internal/bridge/cdpops/pointer.go
+++ b/internal/bridge/cdpops/pointer.go
@@ -361,10 +361,19 @@ func DragByNodeID(ctx context.Context, nodeID int64, dx, dy int) error {
if err != nil {
return err
}
+ return DragBetweenPoints(ctx, x, y, x+float64(dx), y+float64(dy))
+}
- endX := x + float64(dx)
- endY := y + float64(dy)
- dist := math.Sqrt(float64(dx*dx + dy*dy))
+// DragBetweenPoints is the one drag implementation, and the interpolation is the whole of
+// it: Chrome starts an HTML5 drag only after sustained movement follows the press, so a
+// single jump from source to destination never fires dragstart. That is why the CLI's
+// four-request from->to sequence — move, down, ONE move, up — silently did nothing on a
+// draggable element while reporting success, and why the destination has to be resolved
+// into one action rather than assembled from the pointer primitives.
+func DragBetweenPoints(ctx context.Context, x, y, endX, endY float64) error {
+ dx := endX - x
+ dy := endY - y
+ dist := math.Sqrt(dx*dx + dy*dy)
steps := int(dist / 20)
if steps < 3 {
steps = 3
@@ -386,9 +395,7 @@ func DragByNodeID(ctx context.Context, nodeID int64, dx, dy int) error {
}
for i := 1; i <= steps; i++ {
t := float64(i) / float64(steps)
- mx := x + t*float64(dx)
- my := y + t*float64(dy)
- if err := dispatchMouseMove(ctx, mx, my, input.Left, 1); err != nil {
+ if err := dispatchMouseMove(ctx, x+t*dx, y+t*dy, input.Left, 1); err != nil {
return err
}
}
diff --git a/internal/bridge/cdpops_facade.go b/internal/bridge/cdpops_facade.go
index 851946b4..1d04bb82 100644
--- a/internal/bridge/cdpops_facade.go
+++ b/internal/bridge/cdpops_facade.go
@@ -106,6 +106,10 @@ func DragByNodeID(ctx context.Context, nodeID int64, dx, dy int) error {
return bridgecdpops.DragByNodeID(ctx, nodeID, dx, dy)
}
+func DragBetweenPoints(ctx context.Context, x, y, endX, endY float64) error {
+ return bridgecdpops.DragBetweenPoints(ctx, x, y, endX, endY)
+}
+
func HoverByCoordinate(ctx context.Context, x, y float64) error {
return bridgecdpops.HoverByCoordinate(ctx, x, y)
}
diff --git a/internal/cli/actions/actions_element.go b/internal/cli/actions/actions_element.go
index b9585f25..569f54ef 100644
--- a/internal/cli/actions/actions_element.go
+++ b/internal/cli/actions/actions_element.go
@@ -358,12 +358,9 @@ func actionBodyForTarget(kind string, target dragTarget) map[string]any {
}
func Drag(client *http.Client, base, token string, args []string, cmd *cobra.Command) {
- // Two modes:
- // 1. pinchtab drag <selector> --drag-x N --drag-y N
- // → single HTTP "drag" action with pixel offsets (dragX/dragY).
- // 2. pinchtab drag <from> <to>
- // → synthesized mouse-move → mouse-down → mouse-move → mouse-up
- // sequence. Each target may be "selector" or "x,y" coords.
+ // Two modes, both ONE "drag" action: the offset form (dragX/dragY) and the target form
+ // (toSelector/toX/toY). The target form used to be four separate pointer requests, which
+ // moved the pointer in a single jump — never a drag as far as Chrome is concerned.
hasDragX := cmd.Flags().Changed("drag-x")
hasDragY := cmd.Flags().Changed("drag-y")
@@ -388,20 +385,21 @@ func Drag(client *http.Client, base, token string, args []string, cmd *cobra.Com
cli.Fatal("Usage: pinchtab drag <from> <to> or pinchtab drag <selector> --drag-x <n> --drag-y <n>")
}
- from := parseDragTarget(args[0])
- to := parseDragTarget(args[1])
-
- mouseDown := map[string]any{"kind": bridge.ActionMouseDown}
- mouseUp := map[string]any{"kind": bridge.ActionMouseUp}
+ body := actionBodyForTarget(bridge.ActionDrag, parseDragTarget(args[0]))
+ setDragDestinationBody(body, parseDragTarget(args[1]))
if button, _ := cmd.Flags().GetString("button"); button != "" {
- mouseDown["button"] = button
- mouseUp["button"] = button
+ body["button"] = button
}
+ postAction(client, base, token, cmd, body)
+}
- postAction(client, base, token, cmd, actionBodyForTarget(bridge.ActionMouseMove, from))
- postAction(client, base, token, cmd, mouseDown)
- postAction(client, base, token, cmd, actionBodyForTarget(bridge.ActionMouseMove, to))
- postAction(client, base, token, cmd, mouseUp)
+func setDragDestinationBody(body map[string]any, target dragTarget) {
+ if target.hasXY {
+ body["toX"] = target.x
+ body["toY"] = target.y
+ return
+ }
+ body["toSelector"] = target.selector
}
func ActionSimple(client *http.Client, base, token, kind string, args []string, cmd *cobra.Command) {
diff --git a/internal/cli/actions/actions_element_test.go b/internal/cli/actions/actions_element_test.go
index ca3a8da4..a92ca69d 100644
--- a/internal/cli/actions/actions_element_test.go
+++ b/internal/cli/actions/actions_element_test.go
@@ -398,35 +398,49 @@ func TestMouseWheelSupportsPositionalDeltaY(t *testing.T) {
}
}
-func TestDragPostsMouseSequence(t *testing.T) {
+// The from->to form used to post four independent pointer requests, and four requests
+// cannot interpolate: the pointer jumped from source to destination in one move, which
+// Chrome never reads as the start of a drag, so an HTML5 draggable saw nothing while all
+// four answered OK. The destination has to travel WITH the source in one action.
+func TestDragPostsOneActionCarryingItsDestination(t *testing.T) {
m := newMockServer()
defer m.close()
client := m.server.Client()
cmd := newActionCmd()
- Drag(client, m.base(), "", []string{"e5", "400,320"}, cmd)
+ Drag(client, m.base(), "", []string{"e5", "e9"}, cmd)
- if len(m.requests) != 4 {
- t.Fatalf("expected 4 requests, got %d", len(m.requests))
+ if len(m.requests) != 1 {
+ t.Fatalf("expected 1 request, got %d; a drag assembled from several requests cannot interpolate the pointer", len(m.requests))
}
-
- var bodies []map[string]any
- for _, req := range m.requests {
- var body map[string]any
- _ = json.Unmarshal([]byte(req.Body), &body)
- bodies = append(bodies, body)
+ var body map[string]any
+ _ = json.Unmarshal([]byte(m.lastBody), &body)
+ if body["kind"] != "drag" {
+ t.Errorf("kind = %v, want drag", body["kind"])
}
- if bodies[0]["kind"] != "mouse-move" || bodies[0]["ref"] != "e5" {
- t.Fatalf("unexpected first request: %+v", bodies[0])
+ if body["ref"] != "e5" {
+ t.Errorf("source = %+v, want ref e5", body)
}
- if bodies[1]["kind"] != "mouse-down" {
- t.Fatalf("unexpected second request: %+v", bodies[1])
+ if body["toSelector"] != "e9" {
+ t.Errorf("destination = %+v, want toSelector e9", body)
}
- if bodies[2]["kind"] != "mouse-move" || bodies[2]["x"] != float64(400) || bodies[2]["y"] != float64(320) {
- t.Fatalf("unexpected third request: %+v", bodies[2])
+}
+
+func TestDragToCoordinatesPostsThemAsTheDestination(t *testing.T) {
+ m := newMockServer()
+ defer m.close()
+ client := m.server.Client()
+
+ cmd := newActionCmd()
+ Drag(client, m.base(), "", []string{"e5", "400,320"}, cmd)
+
+ var body map[string]any
+ _ = json.Unmarshal([]byte(m.lastBody), &body)
+ if body["toX"] != float64(400) || body["toY"] != float64(320) {
+ t.Errorf("destination = %+v, want toX=400 toY=320", body)
}
- if bodies[3]["kind"] != "mouse-up" {
- t.Fatalf("unexpected fourth request: %+v", bodies[3])
+ if _, ok := body["toSelector"]; ok {
+ t.Errorf("destination = %+v, want no toSelector when the target is a coordinate pair", body)
}
}
diff --git a/internal/handlers/action_drag_destination_test.go b/internal/handlers/action_drag_destination_test.go
new file mode 100644
index 00000000..695fbf73
--- /dev/null
+++ b/internal/handlers/action_drag_destination_test.go
@@ -0,0 +1,89 @@
+package handlers
+
+import (
+ "context"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+// A drag destination is a selector in the same vocabulary as the source, so it goes through
+// the same resolver — refs are what a snapshot hands an agent, and a destination that is
+// never resolved is a drag to (0,0) reported as success.
+func TestADragDestinationIsResolvedThroughTheSourceResolver(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := bridge.ActionRequest{Kind: bridge.ActionDrag, Selector: "#src", ToSelector: "e9"}
+
+ _, err := h.resolveActionRequestDestination(context.Background(), "tab1", &req)
+
+ if err == nil {
+ t.Fatal("a destination selector resolved without any CDP context, so it was not routed through the resolver at all")
+ }
+ if !strings.Contains(err.Error(), "drag destination") {
+ t.Errorf("error = %v, want it to name the end of the drag that failed", err)
+ }
+ if req.ToNodeID != 0 {
+ t.Errorf("ToNodeID = %d, want 0 when resolution failed", req.ToNodeID)
+ }
+}
+
+// Resolution is idempotent: the tab-scoped handler forwards an already-resolved request to
+// the generic one, and re-resolving there would spend the node id it was given.
+func TestAnAlreadyResolvedDragDestinationIsLeftAlone(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+
+ for _, req := range []bridge.ActionRequest{
+ {Kind: bridge.ActionDrag, Selector: "#src", ToSelector: "e9", ToNodeID: 77},
+ {Kind: bridge.ActionDrag, Selector: "#src"},
+ {Kind: bridge.ActionDrag, Selector: "#src", ToX: 400, ToY: 320, HasToXY: true},
+ } {
+ before := req
+ if _, err := h.resolveActionRequestDestination(context.Background(), "tab1", &req); err != nil {
+ t.Errorf("%+v: %v", before, err)
+ }
+ if req.ToNodeID != before.ToNodeID {
+ t.Errorf("%+v: ToNodeID became %d", before, req.ToNodeID)
+ }
+ }
+}
+
+// The wiring, not the helper: a drag whose destination never reaches the resolver drags to
+// (0,0) and answers dragged:true, which is the silent-success shape this card is about. A
+// resolved source (nodeId) is what lets this reach the destination step without a browser.
+func TestTheActionHandlerResolvesADragDestination(t *testing.T) {
+ h := New(&mockBridge{availableActions: []string{bridge.ActionDrag}}, &config.RuntimeConfig{}, nil, nil, nil)
+ body := `{"kind":"drag","nodeId":42,"toSelector":"e9","tabId":"tab1"}`
+ w := httptest.NewRecorder()
+
+ h.HandleAction(w, httptest.NewRequest("POST", "/action", strings.NewReader(body)))
+
+ if w.Code == 200 {
+ t.Fatalf("the handler answered 200 for a destination it never resolved: %s", w.Body.String())
+ }
+ if !strings.Contains(w.Body.String(), "drag destination") {
+ t.Errorf("response = %s, want it to name the drag destination; without that the destination was skipped and the drag went to (0,0)", w.Body.String())
+ }
+}
+
+// The GET form of /action is a documented convenience, and a destination it drops is the
+// same silent no-op in a second place: the drag would run to (0,0) and answer dragged:true.
+func TestTheQueryFormOfActionCarriesTheDragDestination(t *testing.T) {
+ req, ok := decodeActionRequest(httptest.NewRecorder(), httptest.NewRequest("GET", "/action?kind=drag&selector=%23src&toSelector=e9", nil))
+ if !ok {
+ t.Fatal("decode refused a drag query")
+ }
+ if req.ToSelector != "e9" {
+ t.Errorf("ToSelector = %q, want e9", req.ToSelector)
+ }
+
+ point, ok := decodeActionRequest(httptest.NewRecorder(), httptest.NewRequest("GET", "/action?kind=drag&selector=%23src&toX=400&toY=320", nil))
+ if !ok {
+ t.Fatal("decode refused a drag query with a coordinate destination")
+ }
+ if point.ToX != 400 || point.ToY != 320 || !point.HasToXY {
+ t.Errorf("destination = (%v,%v) hasToXY=%v, want (400,320) hasToXY=true", point.ToX, point.ToY, point.HasToXY)
+ }
+}
diff --git a/internal/handlers/action_selector_resolution.go b/internal/handlers/action_selector_resolution.go
index 56e64e10..5d6bbfb4 100644
--- a/internal/handlers/action_selector_resolution.go
+++ b/internal/handlers/action_selector_resolution.go
@@ -79,6 +79,25 @@ func (h *Handlers) resolveActionRequestSelector(ctx context.Context, tabID strin
return actionSelectorResolution{status: http.StatusConflict}, fmt.Errorf("topmost dialog changed twice during selector resolution; retry after the page settles")
}
+// resolveActionRequestDestination resolves a drag's ToSelector the same way the source is
+// resolved, so a destination can be a ref, a CSS selector or a semantic one — the target
+// form exists to say "onto that element", and refs are what a snapshot hands an agent. It
+// runs a scratch request through the source resolver rather than reimplementing the frame
+// and modal scoping, and reports refMissing so the caller answers 404 with the recovery
+// hint instead of dragging to (0,0).
+func (h *Handlers) resolveActionRequestDestination(ctx context.Context, tabID string, req *bridge.ActionRequest) (actionSelectorResolution, error) {
+ if req.ToSelector == "" || req.ToNodeID != 0 {
+ return actionSelectorResolution{}, nil
+ }
+ destination := bridge.ActionRequest{Kind: req.Kind, TabID: req.TabID, Selector: req.ToSelector}
+ resolution, err := h.resolveActionRequestSelector(ctx, tabID, &destination)
+ if err != nil {
+ return resolution, fmt.Errorf("drag destination: %w", err)
+ }
+ req.ToNodeID = destination.NodeID
+ return resolution, nil
+}
+
func (h *Handlers) resolveActionRequestSelectorInScope(
ctx context.Context,
tabID, frameID string,
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index 7b1019e6..d276204c 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -198,6 +198,15 @@ func decodeActionRequest(w http.ResponseWriter, r *http.Request) (bridge.ActionR
var hasXYParam bool
d.Bool("hasXY", &hasXYParam)
req.HasXY = req.HasXY || hasXYParam
+ req.ToSelector = q.Get("toSelector")
+ if d.present("toX") {
+ d.Float("toX", &req.ToX)
+ req.HasToXY = true
+ }
+ if d.present("toY") {
+ d.Float("toY", &req.ToY)
+ req.HasToXY = true
+ }
req.Button = q.Get("button")
d.Bool("dismissBanners", &req.DismissBanners)
d.Bool("dismissKnownInterstitials", &req.DismissKnownInterstitials)
@@ -335,6 +344,15 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
h.errorWithCrashContext(w, selectorResolution.httpStatus(), err)
return
}
+ destinationResolution, err := h.resolveActionRequestDestination(tCtx, resolvedTabID, &req)
+ if err != nil {
+ h.errorWithCrashContext(w, destinationResolution.httpStatus(), err)
+ return
+ }
+ if destinationResolution.refMissing {
+ httpx.Error(w, 404, fmt.Errorf("drag destination %s not found - take a /snapshot first", req.ToSelector))
+ return
+ }
refMissing := selectorResolution.refMissing
submitClick := bridge.IsSubmitClick(req.Kind, req)
if submitClick && refMissing {