-
Notifications
You must be signed in to change notification settings - Fork 29
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
Conversation
f97142f
to
80fd17d
Compare
.github/workflows/main.yml
Outdated
with: | ||
name: html_docs | ||
path: artifacts | ||
|
||
- name: Run JavaScript and snapshot tests | ||
run: npm run _run-tests |
There was a problem hiding this comment.
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.
146f6b7
to
1839db1
Compare
When we added our tests for web components, we only had Jest/JavaScript test infrastructure. No Pytest/Python test infrastructure. So, I wrote the tests in JavaScript. But the tests can easily be written in Python instead. Why make this change? If we land #345, then it will require now using Docker to run the tests locally (but you can use CI to get the updated snapshot files). So, I want to have our JavaScript tests only be things that truly require JavaScript: our visual regression tests, and possibly in the future tests of our JavaScript code. If we decide to not land #345, then we may want to remove this JavaScript test infrastructure entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Eric-Arellano look good from my perspective 👍🏽
CONTRIBUTING.md
Outdated
|
||
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`. | ||
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🚀
Closes #208.
How snapshots work
Playwright snapshot tests take screenshots of certain portions of our site, such as the footer. They save that screenshot to Git, so that we all have the exact copy. Then, for every PR, our tests will retake a new screenshot and make sure it's the exact same as before. That way, we always know when we make a change.
When there was a change, Playwright will output this:
As stated there, Playwright will generate a before, after, and diff photo. In CI, we upload those as GitHub artifacts.
If the change was intentional, then the author copies the updated screenshot to overwrite the old file.
Uses Docker for consistency
Websites render slightly differently between operating systems, e.g. Linux and macOS. Naively, this would cause visual regression tests to fail when running the tests locally on a mac.
So, we use Docker to make sure we always use the same environment.
If users don't want to run Docker locally, they can rely on CI to get the updated snapshots via GitHub Actions Artifacts.
More advanced users can also run the tests locally. They need to install Docker and have the Docker daemon running. But otherwise, our scripts automate everything. They don't need to know how to use Docker. They only need to run
npm test
.How to run locally
(Reminder that contributors can rely on CI for visual regression testing.)
The user has to first build the docs with
THEME=_qiskit_furo tox -e docs
. Then, they only runnpm test
(after originally runningnpm install
& starting Docker).Playwright will start up a server for us.
If the user changed the theme, they must rebuild the docs with Tox.
Switches from Jest to Playwright
I originally tried doing this change via Jest, a common JavaScript test runner. But I had major issues getting the browser automation library Puppeteer #341 to work properly on my M1.
So, this changes our test runner to Playwright, a test runner from Microsoft optimized for visual regression testing like we're doing. It can do things like click buttons, e.g. our Translations menu.
Only Furo
To keep things simple, this only adds infrastructure to snapshot test Furo. Given that our goal is to migrate ASAP, this seemed okay to me.