Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add visual regression tests with Playwright #345

Merged
merged 9 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ jobs:
name: Sample Docs Build
runs-on: ubuntu-latest
if: github.repository_owner == 'Qiskit'
container:
# Keep in sync with tests/js/Dockerfile's base image.
image: mcr.microsoft.com/playwright:v1.34.0-jammy
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -15,23 +18,25 @@ jobs:
node-version: 18
- name: Install Node.js dependencies
run: npm ci
- name: Run Node.js tests
run: npm test
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Install Ubuntu deps
run: sudo apt-get install -y pandoc graphviz
run: apt-get update && apt-get install -y pandoc graphviz
- name: Install tox
run: python -m pip install -U tox

- name: Run lint
run: tox run -e lint
- name: Run Pytest
run: tox run -e py
# For some reason, the Jupyter Extension fails to build properly if one theme has already
# been built with a custom BUILD_DIR. So, we eagerly upload the artifacts for each theme
# build, and then delete the BUILD_DIR before proceeding to the next theme.
#
# But, we build Furo with the default BUILD_DIR because our JavaScript Snapshot tests start
# the server at that standard location.
- name: Create artifacts/ folder
run: mkdir artifacts
- name: Build Legacy theme
Expand All @@ -42,13 +47,21 @@ jobs:
rm -rf example_docs/_build_legacy
- name: Build Furo theme
run: |
THEME=_qiskit_furo BUILD_DIR=example_docs/_build_furo/html tox run -e docs
tar -zcvf furo_html_docs.tar.gz example_docs/_build_furo/html
THEME=_qiskit_furo tox run -e docs
tar -zcvf furo_html_docs.tar.gz example_docs/docs/_build/html
mv furo_html_docs.tar.gz artifacts/.
rm -rf example_docs/_build_furo
- name: Upload Sphinx builds
uses: actions/upload-artifact@v3
if: always()
with:
name: html_docs
path: artifacts
path: artifacts

- name: Run JavaScript and snapshot tests
run: npm run _run-tests
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this isn't the standard npm test, which will use Docker. I optimized npm test to be the ideal experience for local developers.

- name: Upload snapshot results
if: failure()
uses: actions/upload-artifact@v3
with:
name: snapshot_results
path: snapshot_results/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ dmypy.json

# JavaScript
/node_modules
/snapshot_results
26 changes: 21 additions & 5 deletions CONTRIBUTING.md
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,32 @@ Sometimes Sphinx's caching can get in a bad state. First, try running `tox -e do
We are in the process of migrating our theme from Pytorch to Furo (see https://github.com/Qiskit/qiskit_sphinx_theme/issues/232). To build the local docs using the Furo theme, use `THEME=_qiskit_furo` in front of the command, e.g. `THEME=_qiskit_furo tox -e docs`.

------
## Run JavaScript tests
## Visual regression testing

We write some tests in JavaScript (Node.js) to have automated checks of the theme, e.g. that certain components render properly.
We use visual regression testing via [Playwright](https://playwright.dev/docs/test-snapshots) to take screenshots of the site and check that every change we make is intentional. If a screenshot has changed, the test will fail. You can inspect the folder `snapshot_results` to compare the before and after. If the change was intentional, then we update the expected screenshot:

To run these tests, you first need to install Node.js on your machine. If you expect to use JavaScript in other projects, we recommend using [NVM](https://github.com/nvm-sh/nvm). Otherwise, consider using [Homebrew](https://formulae.brew.sh/formula/node) or installing [Node.js directly](https://nodejs.org/en).
1. Find the "actual" snapshot for the failing test, such as `footer-snapshot-has-not-changed-1-actual.png`.
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved
2. Copy that snapshot into the folder `tests/js/snapshots.test.js-snapshots`. Rename the `-actual.png` file ending to be `-linux.png` and overwrite the prior file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to rename the file from actual to linux?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because snapshot_results will call it -actual.png, but when it's in the folder snapshots.test.js-snapshots, Playwright will expect it to end in -linux.png.

This is how Playwright works. Not something we can adjust.


Then:
We upload `snapshot_results` in CI. So, you can get the changed snapshot from GitHub Actions:

1. Navigate to the GitHub Actions page for the "Tests" action.
2. Open the "Summary" page with the house icon.
3. Under the "Artifacts" section, there should be a "snapshot_results" entry. Download it.
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved

You can also run the tests locally. First, you need to install:

1. [Node.js](https://nodejs.org/en). If you expect to use JavaScript in other projects, consider using [NVM](https://github.com/nvm-sh/nvm). Otherwise, consider using [Homebrew](https://formulae.brew.sh/formula/node) or installing [Node.js directly](https://nodejs.org/en).
2. [Docker](https://www.docker.com). You must also ensure that it is running.
* If you cannot install Docker Desktop (such as IBM contributors), you can use [Rancher Desktop](https://rancherdesktop.io). When installing, choose Moby/Dockerd as the engine, rather than nerdctl. To ensure it's running, open up the app "Rancher Desktop".

Then, to run the tests locally:

1. `npm install`
2. `npm test`
2. Build the docs with Furo, `THEME=_qiskit_furo tox -e docs`
3. `npm test`

You must rebuild the docs with `THEME=_qiskit_furo tox -e docs` whenever you make changes to the theme or docs folder. The docs will not automatically rebuild.
Eric-Arellano marked this conversation as resolved.
Show resolved Hide resolved

------
## Updating bundled web components
Expand Down
4 changes: 3 additions & 1 deletion example_docs/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"qiskit_sphinx_theme",
]

html_last_updated_fmt = "%Y/%m/%d"
# Usually this would be something like "%Y/%m/%d", but we need a deterministic value for our
# Playwright visual regression tests.
html_last_updated_fmt = "2020/01/01"

# This allows us to test both the Furo and Pytorch themes. In normal repositories, `html_theme`
# would be set to one specific theme.
Expand Down
Loading