# Elementor Testing Environment Setup Script
# Usage: npm run setup:testing [--restart]
set -e # Exit on any error
# Check if restart flag is provided
if [[ "$1" == "--restart" ]]; then
echo "๐ Restarting Elementor Testing Environment..."
echo "๐ Setting up Elementor Testing Environment..."
# Function to print colored output
echo -e "${GREEN}โ
$1${NC}"
echo -e "${YELLOW}โ ๏ธ $1${NC}"
echo -e "${RED}โ $1${NC}"
echo "๐ Checking prerequisites..."
# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
print_error "Docker is not running. Please start Docker Desktop and try again."
print_status "Docker is running"
# Check if Node.js is installed
if ! command -v node > /dev/null 2>&1; then
print_error "Node.js is not installed. Please install Node.js 16+ and try again."
print_status "Node.js is installed ($(node --version))"
# Check if npm is installed
if ! command -v npm > /dev/null 2>&1; then
print_error "npm is not installed. Please install npm and try again."
print_status "npm is installed ($(npm --version))"
# Create .env file if it doesn't exist
echo "๐ Setting up environment configuration..."
# WordPress Admin Credentials
# WordPress URLs for testing
BASE_URL=http://localhost:8888
ELEMENTS_REGRESSION_BASE_URL=http://localhost:8889
# Optional: Elementor Experiments (uncomment to enable)
# ELEMENTOR_SHOW_HIDDEN_EXPERIMENTS=true
# Optional: Debug Settings (uncomment to enable)
print_status "Created .env file with default configuration"
print_warning ".env file already exists, skipping creation"
# Install npm dependencies
echo "๐ฆ Installing dependencies..."
print_status "Dependencies installed"
# Clean up any existing containers (always do this for both modes)
echo "๐งน Cleaning up existing containers..."
docker stop $(docker ps -q) 2>/dev/null || true
docker rm $(docker ps -aq) 2>/dev/null || true
# Clean storage state files if restarting
if [ "$RESTART_MODE" = true ]; then
rm -f test-results/.storageState-*.json
print_status "Cleaned up containers and storage"
print_status "Cleaned up existing containers"
# Start WordPress environment
echo "๐ Starting WordPress environment..."
npm run start-local-server
print_status "WordPress environment started"
# Wait for WordPress to be ready
echo "โณ Waiting for WordPress to be ready..."
# Check if WordPress is accessible
while [ $attempt -le $max_attempts ]; do
if curl -f http://localhost:8888 > /dev/null 2>&1; then
echo "Attempt $attempt/$max_attempts: WordPress not ready yet, waiting..."
if [ $attempt -gt $max_attempts ]; then
print_error "WordPress failed to start after $max_attempts attempts"
print_status "WordPress is accessible"
# Clean WordPress content
echo "๐งน Cleaning WordPress content..."
docker exec port8888-cli-1 wp post delete $(docker exec port8888-cli-1 wp post list --post_type=post --format=ids) --force 2>/dev/null || true
docker exec port8888-cli-1 wp post delete $(docker exec port8888-cli-1 wp post list --post_type=page --format=ids) --force 2>/dev/null || true
docker exec port8888-cli-1 wp comment delete $(docker exec port8888-cli-1 wp comment list --format=ids) --force 2>/dev/null || true
docker exec port8889-cli-1 wp post delete $(docker exec port8889-cli-1 wp post list --post_type=post --format=ids) --force 2>/dev/null || true
docker exec port8889-cli-1 wp post delete $(docker exec port8889-cli-1 wp post list --post_type=page --format=ids) --force 2>/dev/null || true
docker exec port8889-cli-1 wp comment delete $(docker exec port8889-cli-1 wp comment list --format=ids) --force 2>/dev/null || true
print_status "WordPress content cleaned"
# Install/Fix Elementor plugin
echo "๐ Setting up Elementor plugin..."
# Always try to fix mount issues by downloading stable version
print_warning "Ensuring Elementor is properly installed..."
curl -L -o /tmp/elementor.zip https://downloads.wordpress.org/plugin/elementor.latest-stable.zip
unzip -q /tmp/elementor.zip -d /tmp/
cp -r /tmp/elementor/* build/
rm -rf /tmp/elementor.zip /tmp/elementor
docker exec port8888-cli-1 wp plugin activate elementor 2>/dev/null || docker exec port8888-cli-1 wp plugin install elementor --activate
docker exec port8889-cli-1 wp plugin activate elementor 2>/dev/null || docker exec port8889-cli-1 wp plugin install elementor --activate
print_status "Elementor plugin installed and activated"
echo "๐ Verifying setup..."
# Check WordPress accessibility
if ! curl -f http://localhost:8888 > /dev/null 2>&1; then
print_error "WordPress on port 8888 is not accessible"
if ! curl -f http://localhost:8889 > /dev/null 2>&1; then
print_error "WordPress on port 8889 is not accessible"
if ! docker exec port8888-cli-1 wp plugin list | grep -q "elementor.*active"; then
print_error "Elementor plugin is not active on port 8888"
if ! docker exec port8889-cli-1 wp plugin list | grep -q "elementor.*active"; then
print_error "Elementor plugin is not active on port 8889"
print_status "Setup verification completed successfully"
echo "๐ Elementor Testing Environment Setup Complete!"
echo "๐ What's been set up:"
echo " โข WordPress running on http://localhost:8888 and http://localhost:8889"
echo " โข Admin credentials: admin / password"
echo " โข Elementor plugin installed and activated"
echo " โข Environment configured for testing"
echo " โข Run tests: npm run test:elements-regression"
echo " โข Run Playwright tests: npm run test:playwright"
echo " โข View setup guide: cat TESTING_SETUP.md"
echo "๐ ๏ธ Useful commands:"
echo " โข Clean restart: npm run restart:testing"
echo " โข Start environment: npm run start-local-server"
echo " โข View setup guide: cat TESTING_SETUP.md"