Skip to content

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

test(e2e): add tab focus and index resolution tests

Bosch4522cc3d260a
diff --git a/tests/e2e/scenarios-cli/15-tab-focus.sh b/tests/e2e/scenarios-cli/15-tab-focus.sh
new file mode 100755
index 00000000..f0daab75
--- /dev/null
+++ b/tests/e2e/scenarios-cli/15-tab-focus.sh
@@ -0,0 +1,88 @@
+#!/bin/bash
+# 15-tab-focus.sh — CLI tab focus and index resolution commands
+
+source "$(dirname "$0")/common.sh"
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tab (list tabs)"
+
+pt_ok tab
+assert_output_json "output is valid JSON"
+assert_output_contains "tabs" "output contains tabs array"
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tab 1 (focus first tab by index)"
+
+# Navigate to a page first to ensure there's a tab
+pt nav "${FIXTURES_URL}/index.html"
+
+# Focus on first tab by index
+pt_ok tab 1
+assert_output_contains "focused" "output contains 'focused'"
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tab <id> (focus by tab ID)"
+
+# Get list of tabs and extract first tab ID
+pt tab
+assert_output_json "tab list is valid JSON"
+TAB_ID=$(echo "$PT_OUT" | jq -r '.tabs[0].id // empty')
+
+if [ -n "$TAB_ID" ] && [ "$TAB_ID" != "null" ]; then
+ echo -e " ${BLUE}→ focusing on tab ID: ${TAB_ID:0:12}...${NC}"
+
+ # Focus on that specific tab ID
+ pt_ok tab "$TAB_ID"
+ assert_output_contains "focused" "output indicates tab is focused"
+
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${YELLOW}⚠${NC} could not extract tab ID, skipping"
+ ((ASSERTIONS_PASSED++)) || true
+fi
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tab close <index> (close by index)"
+
+# Open two tabs
+pt nav "${FIXTURES_URL}/index.html"
+pt nav "${FIXTURES_URL}/form.html"
+
+# Get tab count before
+pt tab
+BEFORE=$(echo "$PT_OUT" | jq '.tabs | length')
+echo -e " ${MUTED}tab count before: $BEFORE${NC}"
+
+# Close the second tab by index
+pt_ok tab close 2
+echo -e " ${MUTED}closed tab at index 2${NC}"
+
+# Get tab count after
+pt tab
+AFTER=$(echo "$PT_OUT" | jq '.tabs | length')
+echo -e " ${MUTED}tab count after: $AFTER${NC}"
+
+if [ "$AFTER" -lt "$BEFORE" ]; then
+ echo -e " ${GREEN}✓${NC} tab was closed (count went from $BEFORE to $AFTER)"
+ ((ASSERTIONS_PASSED++)) || true
+else
+ echo -e " ${RED}✗${NC} tab count did not decrease"
+ ((ASSERTIONS_FAILED++)) || true
+fi
+
+end_test
+
+# ─────────────────────────────────────────────────────────────────
+start_test "pinchtab tab 99 (index out of range)"
+
+# Try to focus on a non-existent tab index
+pt tab 99
+assert_exit_code_lte 1 "exit code indicates error or graceful handling"
+
+end_test
diff --git a/tests/e2e/scenarios-recent/43-tab-focus.sh b/tests/e2e/scenarios-recent/43-tab-focus.sh
new file mode 100755
index 00000000..14c2270e
--- /dev/null
+++ b/tests/e2e/scenarios-recent/43-tab-focus.sh
@@ -0,0 +1,79 @@
+#!/bin/bash
+# Test: Tab focus and index-based tab resolution
+# Tests for `POST /tab {"action":"focus","tabId":"..."}` and tab index resolution
+source "$(dirname "$0")/common.sh"
+
+# --- T1: Focus tab by ID ---
+start_test "Focus tab by ID"
+
+# Navigate to page 1 to get first tab ID
+pt_post /navigate "{\"url\":\"${FIXTURES_URL}/index.html\"}"
+assert_ok "navigate to index.html"
+TAB_ID_A=$(echo "$RESULT" | jq -r '.tabId // empty')
+assert_tab_id "tab A"
+show_tab "TAB_ID_A" "$TAB_ID_A"
+
+# Navigate to page 2 to get second tab ID
+pt_post /navigate "{\"url\":\"${FIXTURES_URL}/form.html\"}"
+assert_ok "navigate to form.html"
+TAB_ID_B=$(echo "$RESULT" | jq -r '.tabId // empty')
+assert_tab_id "tab B"
+show_tab "TAB_ID_B" "$TAB_ID_B"
+
+# Focus on tab A
+pt_post /tab "{\"action\":\"focus\",\"tabId\":\"${TAB_ID_A}\"}"
+assert_ok "focus on tab A"
+assert_json_contains "$RESULT" ".focused" "true" "response contains focused=true"
+
+end_test
+
+# --- T2: Focus tab switches active tab ---
+start_test "Focus tab switches active tab"
+
+# Get snapshot of tab A (should have index.html content)
+pt_get "/snapshot?tabId=${TAB_ID_A}"
+assert_ok "snapshot of tab A"
+assert_result_exists ".title" "snapshot has title"
+
+# Verify we can get tab A's text content (index.html)
+pt_get "/text?tabId=${TAB_ID_A}&format=text"
+assert_ok "text of tab A"
+assert_contains "$RESULT" "E2E" "tab A contains E2E Test Suite content"
+
+# Now focus on tab B
+pt_post /tab "{\"action\":\"focus\",\"tabId\":\"${TAB_ID_B}\"}"
+assert_ok "focus on tab B"
+
+# Verify tab B has form content
+pt_get "/text?tabId=${TAB_ID_B}&format=text"
+assert_ok "text of tab B"
+assert_contains "$RESULT" "Form" "tab B contains Form content"
+
+end_test
+
+# --- T3: Focus missing tabId returns 400 ---
+start_test "Focus missing tabId returns 400"
+
+pt_post /tab "{\"action\":\"focus\"}"
+assert_http_status 400 "missing tabId returns 400"
+assert_json_contains "$RESULT" ".error|.message" "tabId" "error message mentions tabId"
+
+end_test
+
+# --- T4: Focus nonexistent tab returns 404 ---
+start_test "Focus nonexistent tab returns 404"
+
+pt_post /tab "{\"action\":\"focus\",\"tabId\":\"nonexistent-tab-id-12345\"}"
+assert_http_status 404 "nonexistent tabId returns 404"
+assert_json_contains "$RESULT" ".error|.message" "not found|not exist" "error message indicates tab not found"
+
+end_test
+
+# --- T5: Focus invalid action returns 400 ---
+start_test "Focus invalid action returns 400"
+
+pt_post /tab "{\"action\":\"invalid\",\"tabId\":\"${TAB_ID_A}\"}"
+assert_http_status 400 "invalid action returns 400"
+assert_json_contains "$RESULT" ".error|.message" "action" "error message mentions action"
+
+end_test