Page History
Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Info | ||
---|---|---|
| ||
|
Overview
Playwright is a recent browser automation tool that provides an alternative to Selenium.
Docker is a containerisation containerization technology that lets you create an image with the same operative system, characteristics, versions, code, and configurations that you can run everywhere and have the same experience.
In the example below example , we are using use Playwright code to to test an application and Xray API to ship the results back to Xray. All of this This is encapsulated in a Node container with all the dependencies installed.
Prerequisites
Expand |
---|
For this example we will use the code from the Xray Playwright Tutorial and create a customized Docker image that will run the tests and ship the results back to Xray. What you need:
|
Build a Custom Docker Image
Creating custom Docker images allows testers to tailor their testing environments to specific project requirements.
For this example, we are going to will use Playwright code that we have created for one of our tutorials tutorials here and and create a Docker container with Node, install Playwright, and copy the code into it. Once the container is ready, we will launch it, execute the tests, and ship the results back to Xray.
Here's a step-by-step guide on building a new Docker image.
Define a Dockerfile
Start with a base image. In our case, Node, to support Playwright.
Use the FROM instruction to specify the base image.
Code Block | ||||
---|---|---|---|---|
| ||||
FROM node:20-bookworm |
Info | ||
---|---|---|
| ||
Notes:
|
Install Dependencies
Use RUN instructions to install necessary dependencies. For instance, if your tests require Playwright, add:
Code Block | ||||
---|---|---|---|---|
| ||||
RUN npx -y playwright@1.42.0 install --with-deps |
Info | ||
---|---|---|
| ||
Notes:
|
Copy Test Code
Use COPY instructions to add your test code to the image. For example, COPY . /app to copy the local directory into the /app directory in the image.
Code Block | ||||
---|---|---|---|---|
| ||||
COPY . /app |
Info | ||
---|---|---|
| ||
Notes:
|
Set the Work directory
Use WORKDIR to define the working directory, it's the directory from where the scripts will be executed.
Code Block | ||||
---|---|---|---|---|
| ||||
WORKDIR /app |
Expose Ports
If your tests involve services running on specific ports, use the EXPOSE instruction to make these ports accessible.
Build the Image
The final Dockerfile will look like this:
Code Block | ||||
---|---|---|---|---|
| ||||
# Get the base image of Node FROM node:21-bookworm # Install Playwright, browsers and browser system dependencies RUN npx -y playwright@1.42.0 install --with-deps # Set the work directory for the application WORKDIR /app # Set the environment path to node_modules/.bin ENV PATH /app/node_modules/.bin:$PATH # COPY the needed files to the app folder in Docker image COPY . /app # Install the dependencies in Node environment RUN npm install |
Run the docker build command to build your custom image. Use a meaningful tag to differentiate this image from others you may have on your machine, this tag will be used to run the container.
For example:
Code Block | ||||
---|---|---|---|---|
| ||||
docker build -t playwright-tutorial . |
Info | ||
---|---|---|
| ||
Notes:
|
Run the Container
Once built, you can run a container based on your custom image using the docker run command. Specify any additional parameters needed, such as environment variables or volume mounts.
For this example we have built a shell script that executes the tests and ships the results back to Xray:
Code Block | ||||
---|---|---|---|---|
| ||||
#!/bin/sh npm run test curl -H "Content-Type: multipart/form-data" -u <USERNAME>:<PASSWORD> -F "file=@xray-report.xml" http://<YOUR_SERVER>/rest/raven/2.0/import/execution/junit?projectKey=<YOUR_PROJECT_KEY> |
We can now run the container and invoke that script to run the tests and ship the result back to Xray like so:
Code Block | ||||
---|---|---|---|---|
| ||||
docker run -it playwright-tutorial ./shipResultsToXray.sh |
Info | ||
---|---|---|
| ||
Notes:
|
By combining existing Docker images with your customizations, you can create versatile and tailored testing environments that meet the specific needs of your projects.
This approach ensures consistency and reproducibility in testing, whether running unit tests, integration tests, or end-to-end tests within Docker containers. These containers are ready to be used anywhere, in a local machine or in your CI/CD environment ensuring that all test dependencies are met.
Tips
- After results are imported in Jira, and if you havent'd already done so directly in the test code, Tests can be linked to existing requirements/user stories, so you can track the impact of their coverage.
- Results from multiple builds can be linked to an existing Test Plan in order to facilitate the analysis of test result trends across builds.
- Results can be associated with a Test Environment, in case you want to analyze coverage and test results by that environment later on. A Test Environment can be a testing stage (e.g. dev, staging, prepod, prod) or an identifier of the device/application used to interact with the system (e.g. browser, mobile OS).
References
- https://playwright.dev/docs/test-intro/
- https://playwright.dev/
- https://hub.docker.com/
- https://www.docker.com/
Table of Contents | ||
---|---|---|
|
CSS Stylesheet |
---|
.toc-btf { position: fixed; } |
Table of Contents | ||
---|---|---|
|
CSS Stylesheet |
---|
.toc-btf { position: fixed; } |