Skip to content

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

fix: click zero-bbox fallback, press named keys, --css flag for click/hover/focus

Djain912698ec3adcb1e
diff --git a/cmd/pinchtab/cmd_cli.go b/cmd/pinchtab/cmd_cli.go
index 784403d0..ae1f305b 100644
--- a/cmd/pinchtab/cmd_cli.go
+++ b/cmd/pinchtab/cmd_cli.go
@@ -316,15 +316,30 @@ func cliAction(client *http.Client, base, token, kind string, args []string) {
switch kind {
case "click", "hover", "focus":
- if len(args) < 1 {
- fatal("Usage: pinchtab %s <ref> [--wait-nav]", kind)
- }
- body["ref"] = args[0]
- for _, a := range args[1:] {
- if a == "--wait-nav" {
+ var cssSelector string
+ var refArg string
+ for i := 0; i < len(args); i++ {
+ switch args[i] {
+ case "--css":
+ if i+1 < len(args) {
+ i++
+ cssSelector = args[i]
+ }
+ case "--wait-nav":
body["waitNav"] = true
+ default:
+ if refArg == "" {
+ refArg = args[i]
+ }
}
}
+ if cssSelector != "" {
+ body["selector"] = cssSelector
+ } else if refArg != "" {
+ body["ref"] = refArg
+ } else {
+ fatal("Usage: pinchtab %s <ref> [--css <selector>] [--wait-nav]", kind)
+ }
case "type":
if len(args) < 2 {
fatal("Usage: pinchtab type <ref> <text>")
diff --git a/cmd/pinchtab/cmd_cli_test.go b/cmd/pinchtab/cmd_cli_test.go
index f910ffba..b61a8226 100644
--- a/cmd/pinchtab/cmd_cli_test.go
+++ b/cmd/pinchtab/cmd_cli_test.go
@@ -258,6 +258,88 @@ func TestCLIPress(t *testing.T) {
}
}
+// TestCLIClickWithCSS verifies that --css <selector> is forwarded as the
+// "selector" field (not "ref") so that the bridge performs a CSS-based click.
+func TestCLIClickWithCSS(t *testing.T) {
+ m := newMockServer()
+ defer m.close()
+ client := m.server.Client()
+
+ cliAction(client, m.base(), "", "click", []string{"--css", "button.submit"})
+ var body map[string]any
+ _ = json.Unmarshal([]byte(m.lastBody), &body)
+ if body["selector"] != "button.submit" {
+ t.Errorf("expected selector=button.submit, got %v", body["selector"])
+ }
+ if _, hasRef := body["ref"]; hasRef {
+ t.Error("should not set ref when --css is provided")
+ }
+}
+
+// TestCLIClickWithCSS_AndWaitNav verifies that --css and --wait-nav can be
+// combined in any order.
+func TestCLIClickWithCSS_AndWaitNav(t *testing.T) {
+ m := newMockServer()
+ defer m.close()
+ client := m.server.Client()
+
+ cliAction(client, m.base(), "", "click", []string{"--wait-nav", "--css", "#login-btn"})
+ var body map[string]any
+ _ = json.Unmarshal([]byte(m.lastBody), &body)
+ if body["selector"] != "#login-btn" {
+ t.Errorf("expected selector=#login-btn, got %v", body["selector"])
+ }
+ if body["waitNav"] != true {
+ t.Error("expected waitNav=true")
+ }
+}
+
+// TestCLIHoverWithCSS verifies that hover accepts --css <selector>.
+func TestCLIHoverWithCSS(t *testing.T) {
+ m := newMockServer()
+ defer m.close()
+ client := m.server.Client()
+
+ cliAction(client, m.base(), "", "hover", []string{"--css", ".nav-item"})
+ var body map[string]any
+ _ = json.Unmarshal([]byte(m.lastBody), &body)
+ if body["selector"] != ".nav-item" {
+ t.Errorf("expected selector=.nav-item, got %v", body["selector"])
+ }
+}
+
+// TestCLIFocusWithCSS verifies that focus accepts --css <selector>.
+func TestCLIFocusWithCSS(t *testing.T) {
+ m := newMockServer()
+ defer m.close()
+ client := m.server.Client()
+
+ cliAction(client, m.base(), "", "focus", []string{"--css", "input[name='email']"})
+ var body map[string]any
+ _ = json.Unmarshal([]byte(m.lastBody), &body)
+ if body["selector"] != "input[name='email']" {
+ t.Errorf("expected selector=input[name='email'], got %v", body["selector"])
+ }
+}
+
+// TestCLIClickRefStillWorks verifies that positional <ref> args still work
+// when --css is not passed (backwards compatibility).
+func TestCLIClickRefStillWorks(t *testing.T) {
+ m := newMockServer()
+ defer m.close()
+ client := m.server.Client()
+
+ cliAction(client, m.base(), "", "click", []string{"e42"})
+ var body map[string]any
+ _ = json.Unmarshal([]byte(m.lastBody), &body)
+ if body["ref"] != "e42" {
+ t.Errorf("expected ref=e42, got %v", body["ref"])
+ }
+ if _, hasSelector := body["selector"]; hasSelector {
+ t.Error("should not set selector when using ref")
+ }
+}
+
func TestCLIFill(t *testing.T) {
m := newMockServer()
defer m.close()
diff --git a/internal/bridge/actions.go b/internal/bridge/actions.go
index 87bbffdc..0243ce06 100644
--- a/internal/bridge/actions.go
+++ b/internal/bridge/actions.go
@@ -70,7 +70,7 @@ func (b *Bridge) InitActionRegistry() {
if req.Key == "" {
return nil, fmt.Errorf("key required for press")
}
- return map[string]any{"pressed": req.Key}, chromedp.Run(ctx, chromedp.KeyEvent(req.Key))
+ return map[string]any{"pressed": req.Key}, DispatchNamedKey(ctx, req.Key)
},
ActionFocus: func(ctx context.Context, req ActionRequest) (map[string]any, error) {
if req.Selector != "" {
diff --git a/internal/bridge/cdp.go b/internal/bridge/cdp.go
index 19e6bf8d..5f0adcfc 100644
--- a/internal/bridge/cdp.go
+++ b/internal/bridge/cdp.go
@@ -133,9 +133,155 @@ func getElementCenter(ctx context.Context, backendNodeID int64) (x, y float64, e
x = (box.Model.Content[0] + box.Model.Content[2] + box.Model.Content[4] + box.Model.Content[6]) / 4
y = (box.Model.Content[1] + box.Model.Content[3] + box.Model.Content[5] + box.Model.Content[7]) / 4
+ // Some nodes (e.g. Svelte5 snippet child nodes) have a zero-size bounding box
+ // in the accessibility tree. Fall back to getBoundingClientRect() for accurate
+ // viewport coordinates.
+ if x == 0 && y == 0 {
+ return getElementCenterJS(ctx, backendNodeID)
+ }
+
return x, y, nil
}
+// getElementCenterJS resolves the DOM node and evaluates getBoundingClientRect()
+// to determine the centre of its rendered area. It is used as a fallback when
+// DOM.getBoxModel returns a zero bounding box.
+func getElementCenterJS(ctx context.Context, backendNodeID int64) (float64, float64, error) {
+ var resolveResult json.RawMessage
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.resolveNode", map[string]any{
+ "backendNodeId": backendNodeID,
+ }, &resolveResult)
+ })); err != nil {
+ return 0, 0, fmt.Errorf("DOM.resolveNode: %w", err)
+ }
+
+ var resolved struct {
+ Object struct {
+ ObjectID string `json:"objectId"`
+ } `json:"object"`
+ }
+ if err := json.Unmarshal(resolveResult, &resolved); err != nil {
+ return 0, 0, err
+ }
+ if resolved.Object.ObjectID == "" {
+ return 0, 0, fmt.Errorf("element not found in DOM (backendNodeId=%d)", backendNodeID)
+ }
+
+ const rectFn = `function() {
+ var r = this.getBoundingClientRect();
+ return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
+ }`
+
+ var callResult json.RawMessage
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return chromedp.FromContext(ctx).Target.Execute(ctx, "Runtime.callFunctionOn", map[string]any{
+ "functionDeclaration": rectFn,
+ "objectId": resolved.Object.ObjectID,
+ "returnByValue": true,
+ }, &callResult)
+ })); err != nil {
+ return 0, 0, fmt.Errorf("getBoundingClientRect: %w", err)
+ }
+
+ var callRes struct {
+ Result struct {
+ Value struct {
+ X float64 `json:"x"`
+ Y float64 `json:"y"`
+ } `json:"value"`
+ } `json:"result"`
+ }
+ if err := json.Unmarshal(callResult, &callRes); err != nil {
+ return 0, 0, err
+ }
+
+ return callRes.Result.Value.X, callRes.Result.Value.Y, nil
+}
+
+// namedKeyDefs maps friendly key names (as accepted by the CLI "press" command)
+// to their CDP Input.dispatchKeyEvent parameters. Keys not in this table fall
+// through to chromedp.KeyEvent so that single printable characters still work.
+var namedKeyDefs = map[string]struct {
+ code string
+ virtualKey int64
+ insertText string // non-empty for keys that produce a character (Enter→\r, Tab→\t)
+}{
+ "Enter": {"Enter", 13, "\r"},
+ "Return": {"Enter", 13, "\r"},
+ "Tab": {"Tab", 9, "\t"},
+ "Escape": {"Escape", 27, ""},
+ "Backspace": {"Backspace", 8, ""},
+ "Delete": {"Delete", 46, ""},
+ "ArrowLeft": {"ArrowLeft", 37, ""},
+ "ArrowRight": {"ArrowRight", 39, ""},
+ "ArrowUp": {"ArrowUp", 38, ""},
+ "ArrowDown": {"ArrowDown", 40, ""},
+ "Home": {"Home", 36, ""},
+ "End": {"End", 35, ""},
+ "PageUp": {"PageUp", 33, ""},
+ "PageDown": {"PageDown", 34, ""},
+ "Insert": {"Insert", 45, ""},
+ "F1": {"F1", 112, ""},
+ "F2": {"F2", 113, ""},
+ "F3": {"F3", 114, ""},
+ "F4": {"F4", 115, ""},
+ "F5": {"F5", 116, ""},
+ "F6": {"F6", 117, ""},
+ "F7": {"F7", 118, ""},
+ "F8": {"F8", 119, ""},
+ "F9": {"F9", 120, ""},
+ "F10": {"F10", 121, ""},
+ "F11": {"F11", 122, ""},
+ "F12": {"F12", 123, ""},
+}
+
+// DispatchNamedKey sends proper CDP keyDown / keyUp events for well-known key
+// names (e.g. "Enter", "Tab", "Escape", "ArrowLeft") so that JavaScript event
+// handlers receive a KeyboardEvent with the correct key property.
+//
+// Unlike chromedp.KeyEvent, which treats multi-character strings as text
+// sequences and would type "Enter" as five separate characters, this function
+// consults namedKeyDefs and emits a single logical keystroke. Unrecognised keys
+// fall back to chromedp.KeyEvent so that single printable characters still work.
+func DispatchNamedKey(ctx context.Context, key string) error {
+ def, ok := namedKeyDefs[key]
+ if !ok {
+ return chromedp.Run(ctx, chromedp.KeyEvent(key))
+ }
+
+ // Normalise "Return" → "Enter" for the W3C key value.
+ w3cKey := key
+ if key == "Return" {
+ w3cKey = "Enter"
+ }
+
+ dispatchEvent := func(evType string) chromedp.ActionFunc {
+ return chromedp.ActionFunc(func(ctx context.Context) error {
+ return chromedp.FromContext(ctx).Target.Execute(ctx, "Input.dispatchKeyEvent", map[string]any{
+ "type": evType,
+ "key": w3cKey,
+ "code": def.code,
+ "windowsVirtualKeyCode": def.virtualKey,
+ "nativeVirtualKeyCode": def.virtualKey,
+ }, nil)
+ })
+ }
+
+ actions := chromedp.Tasks{dispatchEvent("rawKeyDown")}
+ if def.insertText != "" {
+ insertText := def.insertText
+ actions = append(actions, chromedp.ActionFunc(func(ctx context.Context) error {
+ return chromedp.FromContext(ctx).Target.Execute(ctx, "Input.insertText", map[string]any{
+ "text": insertText,
+ }, nil)
+ }))
+ }
+ actions = append(actions, dispatchEvent("keyUp"))
+
+ return chromedp.Run(ctx, actions...)
+}
+
func TypeByNodeID(ctx context.Context, nodeID int64, text string) error {
return chromedp.Run(ctx,
chromedp.ActionFunc(func(ctx context.Context) error {
diff --git a/internal/bridge/cdp_test.go b/internal/bridge/cdp_test.go
index f6b8fae2..481b8bcc 100644
--- a/internal/bridge/cdp_test.go
+++ b/internal/bridge/cdp_test.go
@@ -66,3 +66,104 @@ func TestGetElementCenter_ParsesBoxModel(t *testing.T) {
t.Errorf("expected Y=125, got %f", expectedY)
}
}
+
+// TestGetElementCenterJS_ContextCancelled verifies that getElementCenterJS
+// returns an error when the context is already cancelled (no browser panic).
+func TestGetElementCenterJS_ContextCancelled(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ _, _, err := getElementCenterJS(ctx, 1)
+ if err == nil {
+ t.Error("expected error for cancelled context, got nil")
+ }
+}
+
+// TestDispatchNamedKey_RecognisedKeys checks that namedKeyDefs contains the
+// keys most commonly used in automation scripts.
+func TestDispatchNamedKey_RecognisedKeys(t *testing.T) {
+ mustBeKnown := []string{
+ "Enter", "Return", "Tab", "Escape", "Backspace", "Delete",
+ "ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown",
+ "Home", "End", "PageUp", "PageDown",
+ "F1", "F5", "F12",
+ }
+ for _, k := range mustBeKnown {
+ if _, ok := namedKeyDefs[k]; !ok {
+ t.Errorf("namedKeyDefs is missing key %q", k)
+ }
+ }
+}
+
+// TestDispatchNamedKey_EnterInsertText verifies that the Enter key definition
+// produces a "\r" insertText payload so that form submissions and textareas
+// receive a newline rather than the literal string "Enter".
+func TestDispatchNamedKey_EnterInsertText(t *testing.T) {
+ def := namedKeyDefs["Enter"]
+ if def.insertText != "\r" {
+ t.Errorf("Enter key should insert \\r, got %q", def.insertText)
+ }
+ if def.code != "Enter" {
+ t.Errorf("Enter key code should be \"Enter\", got %q", def.code)
+ }
+ if def.virtualKey != 13 {
+ t.Errorf("Enter virtual key should be 13, got %d", def.virtualKey)
+ }
+}
+
+// TestDispatchNamedKey_TabInsertText verifies that Tab produces the "\t"
+// insert-text payload so that focus advances in form fields.
+func TestDispatchNamedKey_TabInsertText(t *testing.T) {
+ def := namedKeyDefs["Tab"]
+ if def.insertText != "\t" {
+ t.Errorf("Tab key should insert \\t, got %q", def.insertText)
+ }
+}
+
+// TestDispatchNamedKey_NonPrintableNoInsertText verifies that non-printable
+// keys (Escape, ArrowLeft, F5 …) do NOT carry an insertText payload.
+func TestDispatchNamedKey_NonPrintableNoInsertText(t *testing.T) {
+ nonPrintable := []string{"Escape", "Backspace", "Delete", "ArrowLeft", "F5"}
+ for _, k := range nonPrintable {
+ def := namedKeyDefs[k]
+ if def.insertText != "" {
+ t.Errorf("key %q should have empty insertText, got %q", k, def.insertText)
+ }
+ }
+}
+
+// TestDispatchNamedKey_ReturnAlias verifies that "Return" is an alias for
+// Enter and produces the same CDP parameters.
+func TestDispatchNamedKey_ReturnAlias(t *testing.T) {
+ enter := namedKeyDefs["Enter"]
+ ret := namedKeyDefs["Return"]
+ if enter != ret {
+ t.Error("\"Return\" keyDef should equal \"Enter\" keyDef")
+ }
+}
+
+// TestDispatchNamedKey_FallbackOnCancelledCtx verifies that an unrecognised key
+// ("a") falls back to chromedp.KeyEvent and returns an error on a cancelled
+// context rather than silently succeeding.
+func TestDispatchNamedKey_FallbackOnCancelledCtx(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ // "a" is not in namedKeyDefs → falls back to chromedp.KeyEvent
+ err := DispatchNamedKey(ctx, "a")
+ if err == nil {
+ t.Error("expected error dispatching key on cancelled context")
+ }
+}
+
+// TestDispatchNamedKey_KnownKeyOnCancelledCtx verifies that a known named key
+// ("Enter") also returns an error on a cancelled context.
+func TestDispatchNamedKey_KnownKeyOnCancelledCtx(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ err := DispatchNamedKey(ctx, "Enter")
+ if err == nil {
+ t.Error("expected error dispatching Enter on cancelled context")
+ }
+}