Skip to content

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

refactor(e2e): extract snapshot and action helpers

Boschc610dd2af4bc
diff --git a/tests/e2e/scenarios/23-human-type.sh b/tests/e2e/scenarios/23-human-type.sh
index 7987ad04..868010db 100755
--- a/tests/e2e/scenarios/23-human-type.sh
+++ b/tests/e2e/scenarios/23-human-type.sh
@@ -2,93 +2,49 @@
# 23-human-type.sh — humanType and humanClick actions
source "$(dirname "$0")/common.sh"
+source "$(dirname "$0")/helpers/snapshot.sh"
+source "$(dirname "$0")/helpers/actions.sh"
# ─────────────────────────────────────────────────────────────────
start_test "humanClick: click input by ref"
-pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/human-type.html\"}"
-sleep 1
+navigate_fixture "human-type.html"
+fresh_snapshot
-# Get snapshot to populate ref cache
-pt_get /snapshot
-assert_ok "snapshot loaded"
-
-# Find the email input ref
-EMAIL_REF=$(echo "$RESULT" | jq -r '[.nodes[] | select(.role=="textbox" and .name=="Email")][0].ref')
-if [ -z "$EMAIL_REF" ] || [ "$EMAIL_REF" = "null" ]; then
- echo -e " ${RED}✗${NC} could not find Email textbox ref"
- ((ASSERTIONS_FAILED++)) || true
-else
- echo -e " ${GREEN}✓${NC} found Email ref: $EMAIL_REF"
- ((ASSERTIONS_PASSED++)) || true
-
- # humanClick on the input
- pt_post /action -d "{\"kind\":\"humanClick\",\"ref\":\"$EMAIL_REF\"}"
- assert_ok "humanClick on email input"
-fi
+require_ref "textbox" "Email" EMAIL_REF && \
+ action_human_click "$EMAIL_REF"
end_test
# ─────────────────────────────────────────────────────────────────
start_test "humanType: type into input by ref"
-# Re-fetch snapshot for fresh refs
-pt_get /snapshot
-EMAIL_REF=$(echo "$RESULT" | jq -r '[.nodes[] | select(.role=="textbox" and .name=="Email")][0].ref')
-
-if [ -z "$EMAIL_REF" ] || [ "$EMAIL_REF" = "null" ]; then
- echo -e " ${RED}✗${NC} could not find Email textbox ref"
- ((ASSERTIONS_FAILED++)) || true
-else
- # humanType into the email field
- pt_post /action -d "{\"kind\":\"humanType\",\"ref\":\"$EMAIL_REF\",\"text\":\"[email protected]\"}"
- assert_ok "humanType into email input"
-
- # Verify the text was typed
- pt_get /snapshot
- TYPED_VALUE=$(echo "$RESULT" | jq -r '[.nodes[] | select(.role=="textbox" and .name=="Email")][0].value // empty')
- if echo "$TYPED_VALUE" | grep -q "[email protected]"; then
- echo -e " ${GREEN}✓${NC} value is: $TYPED_VALUE"
- ((ASSERTIONS_PASSED++)) || true
- else
- echo -e " ${RED}✗${NC} expected '[email protected]', got: '$TYPED_VALUE'"
- ((ASSERTIONS_FAILED++)) || true
- fi
-fi
+fresh_snapshot
+require_ref "textbox" "Email" EMAIL_REF && {
+ action_human_type "$EMAIL_REF" "[email protected]"
+
+ fresh_snapshot
+ assert_value "textbox" "Email" "[email protected]"
+}
end_test
# ─────────────────────────────────────────────────────────────────
start_test "humanType: type into second input by ref"
-# Get fresh snapshot
-pt_get /snapshot
-NAME_REF=$(echo "$RESULT" | jq -r '[.nodes[] | select(.role=="textbox" and .name=="Name")][0].ref')
-
-if [ -z "$NAME_REF" ] || [ "$NAME_REF" = "null" ]; then
- echo -e " ${RED}✗${NC} could not find Name textbox ref"
- ((ASSERTIONS_FAILED++)) || true
-else
- pt_post /action -d "{\"kind\":\"humanType\",\"ref\":\"$NAME_REF\",\"text\":\"John Doe\"}"
- assert_ok "humanType into name input"
-
- pt_get /snapshot
- TYPED_VALUE=$(echo "$RESULT" | jq -r '[.nodes[] | select(.role=="textbox" and .name=="Name")][0].value // empty')
- if echo "$TYPED_VALUE" | grep -q "John Doe"; then
- echo -e " ${GREEN}✓${NC} value is: $TYPED_VALUE"
- ((ASSERTIONS_PASSED++)) || true
- else
- echo -e " ${RED}✗${NC} expected 'John Doe', got: '$TYPED_VALUE'"
- ((ASSERTIONS_FAILED++)) || true
- fi
-fi
+fresh_snapshot
+require_ref "textbox" "Name" NAME_REF && {
+ action_human_type "$NAME_REF" "John Doe"
+
+ fresh_snapshot
+ assert_value "textbox" "Name" "John Doe"
+}
end_test
# ─────────────────────────────────────────────────────────────────
start_test "humanType: type with CSS selector"
-pt_post /action -d "{\"kind\":\"humanType\",\"selector\":\"#name\",\"text\":\" Jr.\"}"
-assert_ok "humanType with CSS selector"
+action_human_type_selector "#name" " Jr."
end_test
diff --git a/tests/e2e/scenarios/helpers/actions.sh b/tests/e2e/scenarios/helpers/actions.sh
new file mode 100644
index 00000000..c7643b25
--- /dev/null
+++ b/tests/e2e/scenarios/helpers/actions.sh
@@ -0,0 +1,63 @@
+#!/bin/bash
+# helpers/actions.sh — Action helpers (click, type, humanClick, humanType)
+#
+# Higher-level wrappers around pt_post /action for readable tests.
+
+# Click an element by ref.
+# Usage: action_click "$ref"
+action_click() {
+ local ref="$1"
+ pt_post /action -d "{\"kind\":\"click\",\"ref\":\"$ref\"}" > /dev/null
+ assert_ok "click ref=$ref"
+}
+
+# Type text into an element by ref (standard CDP type).
+# Usage: action_type "$ref" "hello"
+action_type() {
+ local ref="$1"
+ local text="$2"
+ pt_post /action -d "{\"kind\":\"type\",\"ref\":\"$ref\",\"text\":\"$text\"}" > /dev/null
+ assert_ok "type '$text' into ref=$ref"
+}
+
+# Human-like click by ref (uses mouse events).
+# Usage: action_human_click "$ref"
+action_human_click() {
+ local ref="$1"
+ pt_post /action -d "{\"kind\":\"humanClick\",\"ref\":\"$ref\"}" > /dev/null
+ assert_ok "humanClick ref=$ref"
+}
+
+# Human-like type by ref (character-by-character key events).
+# Usage: action_human_type "$ref" "hello"
+action_human_type() {
+ local ref="$1"
+ local text="$2"
+ pt_post /action -d "{\"kind\":\"humanType\",\"ref\":\"$ref\",\"text\":\"$text\"}" > /dev/null
+ assert_ok "humanType '$text' into ref=$ref"
+}
+
+# Human-like type by CSS selector.
+# Usage: action_human_type_selector "#email" "hello"
+action_human_type_selector() {
+ local selector="$1"
+ local text="$2"
+ pt_post /action -d "{\"kind\":\"humanType\",\"selector\":\"$selector\",\"text\":\"$text\"}" > /dev/null
+ assert_ok "humanType '$text' into $selector"
+}
+
+# Navigate to a fixture page and wait for load.
+# Usage: navigate_fixture "human-type.html"
+navigate_fixture() {
+ local page="$1"
+ local wait="${2:-1}"
+ pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/$page\"}" > /dev/null
+ sleep "$wait"
+}
+
+# Take a fresh snapshot (sets $RESULT).
+# Usage: fresh_snapshot
+fresh_snapshot() {
+ pt_get /snapshot > /dev/null
+ assert_ok "snapshot"
+}
diff --git a/tests/e2e/scenarios/helpers/snapshot.sh b/tests/e2e/scenarios/helpers/snapshot.sh
new file mode 100644
index 00000000..2e49e606
--- /dev/null
+++ b/tests/e2e/scenarios/helpers/snapshot.sh
@@ -0,0 +1,76 @@
+#!/bin/bash
+# helpers/snapshot.sh — Snapshot ref helpers
+#
+# Provides functions to find and interact with elements from snapshot results.
+# All functions operate on $RESULT (set by pt_get /snapshot).
+
+# Find a ref by role and name. Returns empty string if not found.
+# Usage: ref=$(find_ref "textbox" "Email")
+find_ref() {
+ local role="$1"
+ local name="$2"
+ echo "$RESULT" | jq -r "[.nodes[] | select(.role==\"$role\" and .name==\"$name\")][0].ref // empty"
+}
+
+# Find a ref by name only (any role).
+# Usage: ref=$(find_ref_by_name "Submit")
+find_ref_by_name() {
+ local name="$1"
+ echo "$RESULT" | jq -r "[.nodes[] | select(.name==\"$name\")][0].ref // empty"
+}
+
+# Find a ref by role only (first match).
+# Usage: ref=$(find_ref_by_role "textbox")
+find_ref_by_role() {
+ local role="$1"
+ echo "$RESULT" | jq -r "[.nodes[] | select(.role==\"$role\")][0].ref // empty"
+}
+
+# Get the value of a node by role and name.
+# Usage: val=$(get_value "textbox" "Email")
+get_value() {
+ local role="$1"
+ local name="$2"
+ echo "$RESULT" | jq -r "[.nodes[] | select(.role==\"$role\" and .name==\"$name\")][0].value // empty"
+}
+
+# Assert a ref exists, set it to a variable, and log result.
+# Usage: require_ref "textbox" "Email" EMAIL_REF
+# Returns 1 if not found (caller should skip dependent tests).
+require_ref() {
+ local role="$1"
+ local name="$2"
+ local varname="$3"
+ local ref
+ ref=$(find_ref "$role" "$name")
+
+ if [ -z "$ref" ]; then
+ echo -e " ${RED}✗${NC} could not find $role '$name'"
+ ((ASSERTIONS_FAILED++)) || true
+ eval "$varname=''"
+ return 1
+ fi
+
+ echo -e " ${GREEN}✓${NC} found $role '$name' → $ref"
+ ((ASSERTIONS_PASSED++)) || true
+ eval "$varname='$ref'"
+ return 0
+}
+
+# Assert a field has the expected value (substring match).
+# Usage: assert_value "textbox" "Email" "[email protected]"
+assert_value() {
+ local role="$1"
+ local name="$2"
+ local expected="$3"
+ local actual
+ actual=$(get_value "$role" "$name")
+
+ if echo "$actual" | grep -qF "$expected"; then
+ echo -e " ${GREEN}✓${NC} $name = '$actual'"
+ ((ASSERTIONS_PASSED++)) || true
+ else
+ echo -e " ${RED}✗${NC} $name: expected '$expected', got '$actual'"
+ ((ASSERTIONS_FAILED++)) || true
+ fi
+}