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

Conversation

Eric-Arellano
Copy link
Collaborator

@Eric-Arellano Eric-Arellano commented May 26, 2023

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:

Screenshot 2023-05-26 at 12 09 19 PM

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 run npm test (after originally running npm 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.

@Eric-Arellano Eric-Arellano force-pushed the playwright branch 2 times, most recently from f97142f to 80fd17d Compare May 26, 2023 19:28
@Eric-Arellano Eric-Arellano changed the title [wip] Add visual regression tests with Playwright Add visual regression tests with Playwright May 26, 2023
@Eric-Arellano Eric-Arellano marked this pull request as ready for review May 26, 2023 19:52
with:
name: html_docs
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.

@Eric-Arellano Eric-Arellano force-pushed the playwright branch 2 times, most recently from 146f6b7 to 1839db1 Compare June 5, 2023 14:56
Eric-Arellano added a commit that referenced this pull request Jun 5, 2023
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.
Copy link

@vabarbosa vabarbosa left a 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 Show resolved Hide resolved
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.
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.

CONTRIBUTING.md Outdated Show resolved Hide resolved
CONTRIBUTING.md Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@javabster javabster left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@Eric-Arellano Eric-Arellano merged commit f5b9bbc into Qiskit:main Jun 9, 2023
@Eric-Arellano Eric-Arellano deleted the playwright branch June 9, 2023 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Jest Snapshot Testing
3 participants