Skip to content

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

refactor(e2e): add find_ref_by_role/name + assert_ref_found helpers, simplify actions + type-ref tests

Boschc8eb71159fd2
diff --git a/tests/e2e/scenarios-cli/12-type-ref.sh b/tests/e2e/scenarios-cli/12-type-ref.sh
index a90bdb2a..f203348b 100755
--- a/tests/e2e/scenarios-cli/12-type-ref.sh
+++ b/tests/e2e/scenarios-cli/12-type-ref.sh
@@ -7,17 +7,12 @@ source "$(dirname "$0")/common.sh"
start_test "pinchtab type <ref> <text>"
pt_ok nav "${FIXTURES_URL}/form.html"
-
-# Get snapshot to find an input ref
pt_ok snap
-USERNAME_REF=$(echo "$PT_OUT" | jq -r '.nodes[] | select(.name == "Username:") | .ref' | head -1)
-if [ -n "$USERNAME_REF" ] && [ "$USERNAME_REF" != "null" ]; then
+USERNAME_REF=$(find_ref_by_name "Username:" "$PT_OUT")
+if assert_ref_found "$USERNAME_REF" "username input ref"; then
pt_ok type "$USERNAME_REF" "typed-via-ref"
assert_output_contains "typed" "confirms text was typed"
-else
- echo -e " ${YELLOW}⚠${NC} Could not find username ref, skipping type test"
- ((ASSERTIONS_PASSED++)) || true
fi
end_test
@@ -26,17 +21,12 @@ end_test
start_test "pinchtab click <ref>"
pt_ok nav "${FIXTURES_URL}/buttons.html"
-
-# Get snapshot to find a button ref
pt_ok snap
-BUTTON_REF=$(echo "$PT_OUT" | jq -r '.nodes[] | select(.role == "button") | .ref' | head -1)
-if [ -n "$BUTTON_REF" ] && [ "$BUTTON_REF" != "null" ]; then
+BUTTON_REF=$(find_ref_by_role "button" "$PT_OUT")
+if assert_ref_found "$BUTTON_REF" "button ref"; then
pt_ok click "$BUTTON_REF"
assert_output_contains "clicked" "confirms click by ref"
-else
- echo -e " ${YELLOW}⚠${NC} Could not find button ref, skipping click test"
- ((ASSERTIONS_PASSED++)) || true
fi
end_test
diff --git a/tests/e2e/scenarios/05-actions.sh b/tests/e2e/scenarios/05-actions.sh
index f306e23b..81e8d86a 100755
--- a/tests/e2e/scenarios/05-actions.sh
+++ b/tests/e2e/scenarios/05-actions.sh
@@ -38,7 +38,6 @@ start_test "pinchtab click (CSS selector)"
pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/buttons.html\"}"
sleep 1
-# Click using CSS selector instead of ref
pt_post /action -d '{"kind":"click","selector":"#increment"}'
assert_ok "click by selector"
@@ -50,7 +49,6 @@ start_test "pinchtab type (CSS selector)"
pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/form.html\"}"
sleep 1
-# Type using CSS selector
pt_post /action -d '{"kind":"type","selector":"#username","text":"selectortest"}'
assert_ok "type by selector"
@@ -62,7 +60,6 @@ start_test "pinchtab snapshot (CSS selector filter)"
pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/form.html\"}"
sleep 1
-# Get snapshot scoped to a specific element
pt_get "/snapshot?selector=#username"
assert_ok "snapshot with selector"
@@ -85,10 +82,10 @@ start_test "pinchtab hover (ref)"
pt_post /navigate "{\"url\":\"${FIXTURES_URL}/buttons.html\"}"
assert_ok "navigate"
-# Get snapshot, find a button ref
pt_get /snapshot
assert_ok "snapshot"
-REF=$(echo "$RESULT" | jq -r '.nodes[] | select(.role == "button") | .ref' | head -1)
+REF=$(find_ref_by_role "button")
+assert_ref_found "$REF" "button ref"
pt_post /action "{\"kind\":\"hover\",\"ref\":\"${REF}\"}"
assert_ok "hover on button"
@@ -102,7 +99,8 @@ pt_post /navigate "{\"url\":\"${FIXTURES_URL}/form.html\"}"
assert_ok "navigate"
pt_get /snapshot
-REF=$(echo "$RESULT" | jq -r '.nodes[] | select(.role == "textbox") | .ref' | head -1)
+REF=$(find_ref_by_role "textbox")
+assert_ref_found "$REF" "textbox ref"
pt_post /action "{\"kind\":\"focus\",\"ref\":\"${REF}\"}"
assert_ok "focus on input"
@@ -116,7 +114,8 @@ pt_post /navigate "{\"url\":\"${FIXTURES_URL}/form.html\"}"
assert_ok "navigate"
pt_get /snapshot
-REF=$(echo "$RESULT" | jq -r '.nodes[] | select(.role == "combobox") | .ref' | head -1)
+REF=$(find_ref_by_role "combobox")
+assert_ref_found "$REF" "combobox ref"
pt_post /action "{\"kind\":\"select\",\"ref\":\"${REF}\",\"value\":\"uk\"}"
assert_ok "select option"
@@ -130,12 +129,12 @@ pt_post /navigate "{\"url\":\"${FIXTURES_URL}/form.html\"}"
assert_ok "navigate"
pt_get /snapshot
-REF=$(echo "$RESULT" | jq -r '.nodes[] | select(.role == "textbox") | .ref' | head -1)
+REF=$(find_ref_by_role "textbox")
+assert_ref_found "$REF" "textbox ref"
pt_post /action "{\"kind\":\"fill\",\"ref\":\"${REF}\",\"text\":\"e2e_fill_test\"}"
assert_ok "fill input"
-# Verify value was set
pt_post /evaluate '{"expression":"document.querySelector(\"#username\").value"}'
assert_ok "evaluate"
assert_json_contains "$RESULT" '.result' 'e2e_fill_test' "fill value persisted"
@@ -148,15 +147,13 @@ start_test "pinchtab click triggers navigation"
pt_post /navigate "{\"url\":\"${FIXTURES_URL}/index.html\"}"
assert_ok "navigate"
-# Get a link ref
pt_get "/snapshot?filter=interactive"
-REF=$(echo "$RESULT" | jq -r '.nodes[] | select(.role == "link") | .ref' | head -1)
+REF=$(find_ref_by_role "link")
-if [ -n "$REF" ] && [ "$REF" != "null" ]; then
+if assert_ref_found "$REF" "link ref"; then
pt_post /action "{\"kind\":\"click\",\"ref\":\"${REF}\",\"waitNav\":true}"
assert_ok "click link with waitNav"
else
- # No link on index.html — use CSS selector on an anchor if present
pt_post /action '{"kind":"click","selector":"a","waitNav":true}'
assert_ok "click anchor with waitNav"
fi
diff --git a/tests/e2e/scenarios/common.sh b/tests/e2e/scenarios/common.sh
index 43883dff..ceedd186 100755
--- a/tests/e2e/scenarios/common.sh
+++ b/tests/e2e/scenarios/common.sh
@@ -581,6 +581,42 @@ assert_tab_closed() {
fi
}
+# ================================================================
+# Ref extraction from snapshot
+# ================================================================
+
+# Find first ref by role from RESULT (curl) or PT_OUT (CLI)
+# Usage: REF=$(find_ref_by_role "button")
+find_ref_by_role() {
+ local role="$1"
+ local json="${2:-$RESULT}"
+ echo "$json" | jq -r ".nodes[] | select(.role == \"$role\") | .ref" | head -1
+}
+
+# Find first ref by name from RESULT or PT_OUT
+# Usage: REF=$(find_ref_by_name "Username:")
+find_ref_by_name() {
+ local name="$1"
+ local json="${2:-$RESULT}"
+ echo "$json" | jq -r ".nodes[] | select(.name == \"$name\") | .ref" | head -1
+}
+
+# Assert ref was found, with readable error
+# Usage: assert_ref_found "$REF" "button ref"
+assert_ref_found() {
+ local ref="$1"
+ local desc="${2:-ref}"
+ if [ -n "$ref" ] && [ "$ref" != "null" ]; then
+ echo -e " ${GREEN}✓${NC} found $desc: $ref"
+ ((ASSERTIONS_PASSED++)) || true
+ return 0
+ else
+ echo -e " ${YELLOW}⚠${NC} could not find $desc, skipping"
+ ((ASSERTIONS_PASSED++)) || true
+ return 1
+ fi
+}
+
# ================================================================
# Evaluate with polling (for stealth/async injection)
# ================================================================