Skip to content

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

fix(snapshot): github issue-list rows present in the DOM are missing from snap --full, so /snapshot, /find and /extract cannot see them (PIN-434)

Luigi Agosti16f14ecb336c
diff --git a/internal/bridge/observe/snapshot.go b/internal/bridge/observe/snapshot.go
index 9d669387..0fe34ab0 100644
--- a/internal/bridge/observe/snapshot.go
+++ b/internal/bridge/observe/snapshot.go
@@ -4,12 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
+ "log/slog"
"slices"
"strings"
+ "time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom"
+ "github.com/chromedp/cdproto/emulation"
"github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/cdptk"
)
// MaskedValue stands in for a sensitive field's content in snapshots. Its width
@@ -235,11 +239,42 @@ func FetchFrameContext(ctx context.Context) (FrameContext, error) {
}, nil
}
+const hiddenPageRenderWait = time.Second
+
+const awaitRenderedFrameJS = `new Promise(resolve => {
+ const fallback = setTimeout(resolve, 500);
+ requestAnimationFrame(() => requestAnimationFrame(() => { clearTimeout(fallback); resolve(); }));
+})`
+
+func renderHiddenPage(ctx context.Context, topFrameID string) error {
+ var visibility string
+ if err := cdptk.EvaluateInIsolatedWorld(ctx, topFrameID, `document.visibilityState`, &visibility); err != nil {
+ return fmt.Errorf("read visibility state: %w", err)
+ }
+ if visibility != "hidden" {
+ return nil
+ }
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return emulation.SetFocusEmulationEnabled(true).Do(ctx)
+ })); err != nil {
+ return fmt.Errorf("enable focus emulation: %w", err)
+ }
+ waitCtx, cancel := context.WithTimeout(ctx, hiddenPageRenderWait)
+ defer cancel()
+ if err := cdptk.EvaluateInIsolatedWorld(waitCtx, topFrameID, awaitRenderedFrameJS, nil); err != nil {
+ return fmt.Errorf("await rendered frame: %w", err)
+ }
+ return nil
+}
+
func FetchAXTree(ctx context.Context) ([]RawAXNode, error) {
fc, err := FetchFrameContext(ctx)
if err != nil {
return fetchAXTreeForFrame(ctx, "")
}
+ if err := renderHiddenPage(ctx, fc.Tree.Frame.ID); err != nil {
+ slog.Debug("render hidden page before accessibility read", "err", err)
+ }
frameMap := fc.Frames
ownerMap := fc.Owners
diff --git a/internal/bridge/observe/snapshot_test.go b/internal/bridge/observe/snapshot_test.go
new file mode 100644
index 00000000..be2cf35b
--- /dev/null
+++ b/internal/bridge/observe/snapshot_test.go
@@ -0,0 +1,89 @@
+package observe
+
+import (
+ "context"
+ "encoding/base64"
+ "testing"
+ "time"
+
+ "github.com/chromedp/cdproto/target"
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/testbrowser"
+)
+
+const hydratedListFixtureHTML = `<body>
+<style>.row { content-visibility: auto; contain-intrinsic-size: auto 64px; }</style>
+<h2 id="list-title">Search results</h2>
+<ul id="issues" role="list" aria-labelledby="list-title"></ul>
+<script>
+for (const title of ["Hydrated issue alpha", "Hydrated issue beta", "Hydrated issue gamma"]) {
+ const row = document.createElement("div");
+ row.className = "row";
+ row.innerHTML = '<li role="listitem"><h3><a href="#">' + title + '</a></h3></li>';
+ document.getElementById("issues").appendChild(row);
+}
+</script>
+</body>`
+
+func newBackgroundTabFixture(t *testing.T, html string) context.Context {
+ t.Helper()
+ chromePath := testbrowser.Path(t)
+
+ alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), append(
+ chromedp.DefaultExecAllocatorOptions[:],
+ chromedp.ExecPath(chromePath),
+ chromedp.UserDataDir(testbrowser.ProfileDir(t)),
+ chromedp.Flag("headless", true),
+ chromedp.Flag("no-sandbox", true),
+ )...)
+ browserCtx, cancelBrowser := chromedp.NewContext(alloc)
+ browserCtx, cancelTimeout := context.WithTimeout(browserCtx, 30*time.Second)
+ t.Cleanup(func() {
+ cancelTimeout()
+ cancelBrowser()
+ cancelAlloc()
+ })
+
+ var targetID target.ID
+ if err := chromedp.Run(browserCtx, chromedp.ActionFunc(func(ctx context.Context) error {
+ var err error
+ targetID, err = target.CreateTarget("about:blank").WithFocus(false).Do(ctx)
+ return err
+ })); err != nil {
+ t.Fatal(err)
+ }
+ tabCtx, cancelTab := chromedp.NewContext(browserCtx, chromedp.WithTargetID(targetID))
+ t.Cleanup(cancelTab)
+
+ dataURL := "data:text/html;base64," + base64.StdEncoding.EncodeToString([]byte(html))
+ var visibility string
+ if err := chromedp.Run(tabCtx,
+ chromedp.Navigate(dataURL),
+ chromedp.Evaluate(`document.visibilityState`, &visibility),
+ ); err != nil {
+ t.Fatal(err)
+ }
+ if visibility != "hidden" {
+ t.Fatalf("fixture tab visibilityState = %q, want hidden: the background-tab precondition no longer holds", visibility)
+ }
+ return tabCtx
+}
+
+func TestSnapshotOfAHiddenTabIncludesContentVisibilityAutoRows(t *testing.T) {
+ ctx := newBackgroundTabFixture(t, hydratedListFixtureHTML)
+
+ nodes := snapshotNodes(t, ctx)
+ want := map[string]bool{"Hydrated issue alpha": false, "Hydrated issue beta": false, "Hydrated issue gamma": false}
+ for _, n := range nodes {
+ if n.Role == "link" {
+ if _, ok := want[n.Name]; ok {
+ want[n.Name] = true
+ }
+ }
+ }
+ for name, seen := range want {
+ if !seen {
+ t.Errorf("snapshot of a background tab is missing link %q that sits in a content-visibility:auto row", name)
+ }
+ }
+}
diff --git a/tests/e2e/fixtures/snapshot-hydrated-list.html b/tests/e2e/fixtures/snapshot-hydrated-list.html
new file mode 100644
index 00000000..c263226a
--- /dev/null
+++ b/tests/e2e/fixtures/snapshot-hydrated-list.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title>Hydrated Issue List</title>
+ <style>
+ body { font-family: sans-serif; margin: 16px; }
+ .row { content-visibility: auto; contain-intrinsic-size: auto 64px; }
+ .row li { display: grid; padding: 8px; border-bottom: 1px solid #ddd; }
+ </style>
+</head>
+<body>
+ <main>
+ <h1>Issues</h1>
+ <h2 id="list-title">Search results</h2>
+ <ul id="issue-list" role="list" aria-labelledby="list-title"></ul>
+ </main>
+ <script>
+ const issues = [
+ { id: 101, title: "Hydrated issue alpha" },
+ { id: 102, title: "Hydrated issue beta" },
+ { id: 103, title: "Hydrated issue gamma" }
+ ];
+ const list = document.getElementById("issue-list");
+ for (const issue of issues) {
+ const row = document.createElement("div");
+ row.className = "row";
+ const li = document.createElement("li");
+ li.setAttribute("role", "listitem");
+ const heading = document.createElement("h3");
+ const link = document.createElement("a");
+ link.href = "#issue-" + issue.id;
+ link.dataset.testid = "issue-pr-title-link";
+ link.textContent = issue.title;
+ heading.appendChild(link);
+ li.appendChild(heading);
+ row.appendChild(li);
+ list.appendChild(row);
+ }
+ </script>
+</body>
+</html>
diff --git a/tests/e2e/scenarios/api/browser-basic.sh b/tests/e2e/scenarios/api/browser-basic.sh
index efa332a2..270ca8cf 100644
--- a/tests/e2e/scenarios/api/browser-basic.sh
+++ b/tests/e2e/scenarios/api/browser-basic.sh
@@ -137,6 +137,22 @@ assert_form_page "$RESULT"
end_test
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab snap (snapshot-hydrated-list.html): content-visibility:auto rows in a background tab"
+
+pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/snapshot-hydrated-list.html\"}"
+assert_ok "navigate to hydrated list"
+
+pt_get "/snapshot?filter=all"
+assert_ok "snapshot hydrated list"
+assert_json_eq "$RESULT" '[.nodes[] | select(.role == "link" and (.name | startswith("Hydrated issue")))] | length' '3' "all three rows are in the snapshot"
+
+pt_post /find -d '{"query":"Hydrated issue gamma"}'
+assert_ok "find hydrated row"
+assert_json_eq "$RESULT" '.matches[0].name' 'Hydrated issue gamma' "find sees the row"
+
+end_test
+
# ─────────────────────────────────────────────────────────────────
start_test "pinchtab text (table.html)"