-
|
PEST 4 introduces some amazing browser testing capabilities. I have tried installing it Anyone have any suggestions how to integrate PEST browser testing with a project which uses Spin? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
|
OK. After a few hours of troubleshooting, I got it to work. So here is a guide on how to do it.
Setting Up Pest 4 Browser Testing with Playwright in Laravel + ServerSideUp SpinThis guide documents the complete setup process for implementing Pest 4's browser testing functionality using Playwright in a Laravel application running with ServerSideUp's Spin Docker environment. OverviewThis setup enables browser testing using:
Prerequisites
Step 1: Update Docker ConfigurationDockerfile ChangesThe key changes needed in your
Why These Changes?
After making these changes, rebuild your Docker containers: spin build
spin up -dStep 2: Update PHP DependenciesUpdate your {
"require-dev": {
"pestphp/pest": "^4.0",
"pestphp/pest-plugin-browser": "^4.1",
"pestphp/pest-plugin-laravel": "^4.0"
}
}Install the updated dependencies: spin exec php composer updateStep 3: Add Node.js DependenciesUpdate your {
"dependencies": {
"playwright": "^1.55.0"
}
}Install Node dependencies: spin run node npm installStep 4: Create and Run Browser TestsExecute your browser tests using Spin: spin exec php ./vendor/bin/pestOr run specific tests: spin exec php ./vendor/bin/pest --filter=BrowseRouteTestTroubleshootingCommon Issues
Debugging Commands# Check Playwright installation
spin exec php npx playwright --version
# Verify Node.js in container
spin exec php node --version
# Check if browsers are installed
spin exec php npx playwright install --dry-run |
Beta Was this translation helpful? Give feedback.
-
|
Thank you so much for publishing your solution! 🙏 |
Beta Was this translation helpful? Give feedback.
-
Update: Running separate testing container and CIBased on feedback on my previous post about increased attack surface area by having PHP and Node in the container which is used in production, I decided to create a separate testing container. Also I got it to work with CI as described below. Would love feedback — especially if anyone has integrated this or similar setup into a CI pipeline with Spin. Here is what I did now: DockerfileI created a specific image for the php-testing container: ############################################
# Testing image with Browser Testing Support
############################################
FROM serversideup/php:8.4-fpm-nginx AS php-testing
# Accept same build args as development stage
ARG USER_ID
ARG GROUP_ID
USER root
# Install Node.js 20.x (required for Playwright)
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y nodejs git
# Install PHP extensions (including sockets for Pest browser testing)
RUN install-php-extensions bcmath gd imagick intl exif sockets
# Set working directory
WORKDIR /var/www/html
# Configure www-data permissions
RUN docker-php-serversideup-set-id www-data $USER_ID:$GROUP_ID && \
docker-php-serversideup-set-file-permissions --owner $USER_ID:$GROUP_ID --service nginx
# Copy package.json files first for caching
COPY --chown=$USER_ID:$GROUP_ID package*.json ./
# Install npm dependencies as www-data
USER www-data
RUN npm install
# Switch to root to install system-level Playwright deps
USER root
RUN npm install -g playwright@latest
RUN npx playwright install-deps chromium
# Switch back to www-data for local Playwright browser installation
USER www-data
RUN npx playwright install chromium
# Switch back to root for cleanup and permissions
USER root
RUN chown -R $USER_ID:$GROUP_ID /var/www/html/node_modules
RUN chown -R $USER_ID:$GROUP_ID /var/www/.cache
# Create temp directories for Pest browser testing
RUN mkdir -p /var/www/html/vendor/pestphp/pest/.temp && \
mkdir -p /var/www/html/vendor/pestphp/pest-plugin-browser/.temp && \
chown -R $USER_ID:$GROUP_ID /var/www/html/vendor/pestphp/pest/.temp && \
chown -R $USER_ID:$GROUP_ID /var/www/html/vendor/pestphp/pest-plugin-browser/.temp
# Add entrypoint scripts
COPY --chmod=755 ./entrypoint.d/ /etc/entrypoint.d/
# Drop privileges back to www-data
USER www-data
docker-compose.dev.ymlI added the following service definition: # Dedicated PHP testing container with Node.js and Playwright for browser testing
php-testing:
build:
target: php-testing
args:
USER_ID: ${SPIN_USER_ID}
GROUP_ID: ${SPIN_GROUP_ID}
stop_signal: SIGTERM
volumes:
- .:/var/www/html/
networks:
- development
depends_on:
postgrestest:
condition: service_started
environment:
# Base testing environment
- APP_ENV=testing
# DB config handled via phpunit.xml / phpunit.pgsql.xmlRebuild containers:Run: spin build
spin up -dRun tests in php-testing containerspin exec php-testing ./vendor/bin/pest tests/BrowserTechnically you can use this container for all your tests. For CI I added the following to my workflows pull-request.yml browser-tests-sqlite:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.4"
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Composer Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Install NPM Dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps chromium
- name: Build Assets
run: npm run build
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute Browser Tests (SQLite)
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
DB_FOREIGN_KEYS: true
APP_ENV: testing
run: ./vendor/bin/pest tests/Browser --compact |
Beta Was this translation helpful? Give feedback.
Update: Running separate testing container and CI
Based on feedback on my previous post about increased attack surface area by having PHP and Node in the container which is used in production, I decided to create a separate testing container. Also I got it to work with CI as described below.
Would love feedback — especially if anyone has integrated this or similar setup into a CI pipeline with Spin.
Here is what I did now:
Dockerfile
I created a specific image for the php-testing container: