How to dockerize your Playwright test
If you are looking to learn #Playwright with TypeScript, try my latest book Mastering Playwright: Comprehensive Web Automation for End-to-End, API, and Accessibility Testing
You want to containerize your Playwright tests, to run scalable and efficient test. DockerFile is a blueprint to build self-contained environment for your tests to run flawlessly within a Docker container. Here are steps:
Step 1: Create .DockerFile file in your project and add following code
# Use the official Playwright Docker image (version may vary based on Playwright used in your project)
FROM mcr.microsoft.com/playwright:v1.40.0
# Set the working directory inside the container
WORKDIR /app
# Copy package.json and install dependencies
COPY package.json package-lock.json ./
RUN npm install
# Copy the entire project inside the container
COPY . .
# Install Playwright dependencies and browsers
RUN npx playwright install
# Install Playwright dependencies and browsers
RUN npx playwright install --with-deps
# Command to execute Playwright tests
CMD ["npx", "playwright", "test"]
Step 2: Create .DockerIgnore file in your project and add following code. .DockerIgnore file is not mandatory, it helps to keep your docker cleaner.
node_modulesStep 3: Now, let’s create a docker image & run the test
docker build -t playwrighttest .Step 4: Run our test
docker run -d --restart unless-stopped --name runplaywrighteest playwrighttestStep 5: Delete docker running container
Docker rm -f runplaywrighteesthttps://abhaybharti.gumroad.com/l/ydhowv?layout=profile

