#!/usr/bin/env bash # ============================================================================= # screenshot_batch.sh - Batch Screenshot Example # ============================================================================= # Takes screenshots of multiple pages and saves them to a specified directory. # Useful for visual regression testing or archiving web pages. # # Usage: # bash examples/screenshot_batch.sh [--headless] # # Output: # Screenshots are saved to ./screenshots/ directory. # ============================================================================= SCRIPT_DIR="$(cd -P "$(dirname "$(realpath "${BASH_SOURCE[0]:-${0}}")")" &>/dev/null && pwd)" source "${SCRIPT_DIR}/../lib/selenium.sh" _create_screenshot_pages() { local tmpdir tmpdir=$(mktemp -d /tmp/shellnium-screenshots-XXXXXX) cat > "${tmpdir}/homepage.html" << 'HTMLEOF' Home Page

Welcome Home

This is the landing page.

HTMLEOF cat > "${tmpdir}/dashboard.html" << 'HTMLEOF' Dashboard

Dashboard

Users

1,234

Revenue

$56K

Orders

892
HTMLEOF cat > "${tmpdir}/about.html" << 'HTMLEOF' About Us

About Us

We are a team building browser automation tools for the terminal.

Our mission is to make web testing accessible from any shell.

HTMLEOF echo "$tmpdir" } OUTPUT_DIR="${SCRIPT_DIR}/../tmp/screenshots" main() { local pages_dir pages_dir=$(_create_screenshot_pages) trap "rm -rf '$pages_dir'" EXIT local PAGES=( "file://${pages_dir}/homepage.html" "file://${pages_dir}/dashboard.html" "file://${pages_dir}/about.html" ) # Create output directory mkdir -p "$OUTPUT_DIR" echo "Screenshots will be saved to: ${OUTPUT_DIR}" echo "---" local count=0 local total=${#PAGES[@]} for url in "${PAGES[@]}"; do count=$((count + 1)) # Generate a filename from the HTML filename local filename filename=$(basename "$url" .html).png echo "[${count}/${total}] Capturing: ${url}" navigate_to "$url" # Wait for page to load sleep 1 local title title=$(get_title) echo " Title: ${title}" # Take screenshot screenshot "${OUTPUT_DIR}/${filename}" echo " Saved: ${OUTPUT_DIR}/${filename}" echo "" done echo "---" echo "Batch complete. ${count} screenshots saved to ${OUTPUT_DIR}/" # Clean up delete_session } main