2020-08-31 03:39:34 +02:00
|
|
|
# Web frontend black-box Puppeteer tests
|
|
|
|
|
2022-02-24 00:17:21 +01:00
|
|
|
While our [node test suite](testing-with-node.md) is the
|
2020-08-31 03:39:34 +02:00
|
|
|
preferred way to test most frontend code because they are easy to
|
|
|
|
write and maintain, some code is best tested in a real browser, either
|
|
|
|
because of navigation (E.g. login) or because we want to verify the
|
|
|
|
interaction between Zulip logic and browser behavior (E.g. copy/paste,
|
|
|
|
keyboard shortcuts, etc.).
|
|
|
|
|
|
|
|
## Running tests
|
|
|
|
|
|
|
|
You can run this test suite as follows:
|
2021-08-20 22:54:08 +02:00
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```bash
|
|
|
|
tools/test-js-with-puppeteer
|
2020-08-31 03:39:34 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
See `tools/test-js-with-puppeteer --help` for useful options,
|
|
|
|
especially running specific subsets of the tests to save time when
|
|
|
|
debugging.
|
|
|
|
|
2023-02-22 23:04:11 +01:00
|
|
|
The test files live in `web/e2e-tests` and make use
|
2020-08-31 03:39:34 +02:00
|
|
|
of various useful helper functions defined in
|
2023-02-22 23:04:11 +01:00
|
|
|
`web/e2e-tests/lib/common.ts`.
|
2020-08-31 03:39:34 +02:00
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
## How Puppeteer tests work
|
2020-08-31 03:39:34 +02:00
|
|
|
|
|
|
|
The Puppeteer tests use a real Chromium browser (powered by
|
|
|
|
[puppeteer](https://github.com/puppeteer/puppeteer)), connected to a
|
2021-08-20 21:53:28 +02:00
|
|
|
real Zulip development server. These are black-box tests: Steps in a
|
2020-08-31 03:39:34 +02:00
|
|
|
Puppeteer test are largely things one might do as a user of the Zulip
|
2021-05-14 00:16:30 +02:00
|
|
|
web app, like "Type this key", "Wait until this HTML element
|
2020-08-31 03:39:34 +02:00
|
|
|
appears/disappears", or "Click on this HTML element".
|
|
|
|
|
|
|
|
For example, this function might test the `x` keyboard shortcut to
|
|
|
|
open the compose box for a new private message:
|
|
|
|
|
2021-08-20 07:09:04 +02:00
|
|
|
```js
|
2020-08-31 03:39:34 +02:00
|
|
|
async function test_private_message_compose_shortcut(page) {
|
|
|
|
await page.keyboard.press("KeyX");
|
|
|
|
await page.waitForSelector("#private_message_recipient", {visible: true});
|
|
|
|
await common.pm_recipient.expect(page, "");
|
|
|
|
await close_compose_box(page);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The test function presses the `x` key, waits for the
|
|
|
|
`#private_message_recipient` input element to appear, verifies its
|
2021-08-20 21:53:28 +02:00
|
|
|
content is empty, and then closes the compose box. The
|
2020-08-31 03:39:34 +02:00
|
|
|
`waitForSelector` step here (and in most tests) is critical; tests
|
|
|
|
that don't wait properly often fail nonderministically, because the
|
|
|
|
test will work or not depending on whether the browser updates the UI
|
|
|
|
before or after executing the next step in the test.
|
|
|
|
|
|
|
|
Black-box tests are fantastic for ensuring the overall health of the
|
|
|
|
project, but are also slow, costly to maintain, and require care to
|
|
|
|
avoid nondeterministic failures, so we usually prefer to write a Node
|
|
|
|
test instead when both are options.
|
|
|
|
|
|
|
|
They also can be a bit tricky to understand for contributors not
|
|
|
|
familiar with [async/await][learn-async-await].
|
|
|
|
|
|
|
|
## Debugging Puppeteer tests
|
|
|
|
|
|
|
|
The following questions are useful when debugging Puppeteer test
|
|
|
|
failures you might see in [continuous
|
2022-02-24 00:17:21 +01:00
|
|
|
integration](continuous-integration.md):
|
2020-08-31 03:39:34 +02:00
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- Does the flow being tested work properly in the Zulip browser UI?
|
2020-08-31 03:39:34 +02:00
|
|
|
Test failures can reflect real bugs, and often it's easier and more
|
|
|
|
interactive to debug an issue in the normal Zulip development
|
|
|
|
environment than in the Puppeteer test suite.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Does the change being tested adjust the HTML structure in a way that
|
2021-08-20 21:53:28 +02:00
|
|
|
affects any of the selectors used in the tests? If so, the test may
|
2020-08-31 03:39:34 +02:00
|
|
|
just need to be updated for your changes.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Does the test fail deterministically when you run it locally using
|
2021-04-01 14:37:19 +02:00
|
|
|
E.g. `./tools/test-js-with-puppeteer compose.ts`? If so, you can
|
2020-08-31 03:39:34 +02:00
|
|
|
iteratively debug to see the failure.
|
2021-08-20 21:53:28 +02:00
|
|
|
- Does the test fail nondeterministically? If so, the problem is
|
2020-08-31 03:39:34 +02:00
|
|
|
likely that a `waitForSelector` statement is either missing or not
|
2021-08-20 21:53:28 +02:00
|
|
|
waiting for the right thing. Tests fail nondeterministically much
|
2020-08-31 03:39:34 +02:00
|
|
|
more often on very slow systems like those used for Continuous
|
|
|
|
Integration (CI) services because small races are amplified in those
|
|
|
|
environments; this often explains failures in CI that cannot be
|
|
|
|
easily reproduced locally.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Does the test fail when you are typing (filling the form) on a modal
|
2021-03-29 17:21:15 +02:00
|
|
|
or other just-opened UI widget? Puppeteer starts typing after focusing on
|
|
|
|
the text field, sending keystrokes one after another. So, if
|
|
|
|
application code explicitly focuses the modal after it
|
|
|
|
appears/animates, this could cause the text field that Puppeteer is
|
|
|
|
trying to type into to lose focus, resulting in partially typed data.
|
|
|
|
The recommended fix for this is to wait until the modal is focused before
|
|
|
|
starting typing, like this:
|
|
|
|
```JavaScript
|
|
|
|
await page.waitForFunction(":focus").attr("id") === modal_id);
|
|
|
|
```
|
2020-08-31 03:39:34 +02:00
|
|
|
|
|
|
|
These tools/features are often useful when debugging:
|
|
|
|
|
2021-08-20 21:45:39 +02:00
|
|
|
- You can use `console.log` statements both in Puppeteer tests and the
|
2020-08-31 03:39:34 +02:00
|
|
|
code being tested to print-debug.
|
2021-08-20 21:45:39 +02:00
|
|
|
- Zulip's Puppeteer tests are configured to generate screenshots of
|
2020-08-31 03:39:34 +02:00
|
|
|
the state of the test browser when an assert statement fails; these
|
|
|
|
are stored under `var/puppeteer/*.png` and are extremely helpful for
|
|
|
|
debugging test failures.
|
2021-08-20 21:45:39 +02:00
|
|
|
- TODO: Mention how to access Puppeteer screenshots in CI.
|
|
|
|
- TODO: Add an option for using the `headless: false` debugging mode
|
2020-10-23 02:43:28 +02:00
|
|
|
of Puppeteer so you can watch what's happening, and document how to
|
2020-08-31 03:39:34 +02:00
|
|
|
make that work with Vagrant.
|
2021-08-20 21:45:39 +02:00
|
|
|
- TODO: Document `--interactive`.
|
2022-02-08 00:13:33 +01:00
|
|
|
- TODO: Document how to run 100x in CI to check for nondeterministic
|
2020-08-31 03:39:34 +02:00
|
|
|
failures.
|
2021-08-20 21:45:39 +02:00
|
|
|
- TODO: Document any other techniques/ideas that were helpful when porting
|
2020-08-31 03:39:34 +02:00
|
|
|
the Casper suite.
|
2023-03-04 02:17:54 +01:00
|
|
|
- The Zulip server powering these tests is just `run-dev` with some
|
2020-08-31 03:39:34 +02:00
|
|
|
extra [Django settings](../subsystems/settings.md) from
|
|
|
|
`zproject/test_extra_settings.py` to configure an isolated database
|
|
|
|
so that the tests will not interfere/interact with a normal
|
2021-08-20 21:53:28 +02:00
|
|
|
development environment. The console output while running the tests
|
2020-08-31 03:39:34 +02:00
|
|
|
includes the console output for the server; any Python exceptions
|
|
|
|
are likely actual bugs in the changes being tested.
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
See also [Puppeteer upstream's debugging
|
2020-08-31 03:39:34 +02:00
|
|
|
tips](https://github.com/puppeteer/puppeteer#debugging-tips); some
|
|
|
|
tips may require temporary patches to functions like `run_test` or
|
2023-02-22 23:04:11 +01:00
|
|
|
`ensure_browser` in `web/e2e-tests/lib/common.ts`.
|
2020-08-31 03:39:34 +02:00
|
|
|
|
|
|
|
## Writing Puppeteer tests
|
|
|
|
|
|
|
|
Probably the easiest way to learn how to write Puppeteer tests is to
|
|
|
|
study some of the existing test files. There are a few tips that can
|
|
|
|
be useful for writing Puppeteer tests in addition to the debugging
|
|
|
|
notes above:
|
|
|
|
|
|
|
|
- Run just the file containing your new tests as described above to
|
|
|
|
have a fast debugging cycle.
|
|
|
|
- When you're done writing a test, run it 100 times in a loop to
|
2022-02-08 00:13:33 +01:00
|
|
|
verify it does not fail nondeterministically (see above for notes on
|
2020-08-31 03:39:34 +02:00
|
|
|
how to get CI to do it for you); this is important to avoid
|
|
|
|
introducing extremely annoying nondeterministic failures into
|
2021-09-01 03:13:20 +02:00
|
|
|
`main`.
|
2020-08-31 03:39:34 +02:00
|
|
|
- With black-box browser tests like these, it's very important to write your code
|
|
|
|
to wait for browser's UI to update before taking any action that
|
|
|
|
assumes the last step was processed by the browser (E.g. after you
|
|
|
|
click on a user's avatar, you need an explicit wait for the profile
|
|
|
|
popover to appear before you can try to click on a menu item in that
|
2021-08-20 21:53:28 +02:00
|
|
|
popover). This means that before essentially every action in your
|
2020-08-31 03:39:34 +02:00
|
|
|
Puppeteer tests, you'll want to use `waitForSelector` or similar
|
|
|
|
wait function to make sure the page or element is ready before you
|
2021-08-20 21:53:28 +02:00
|
|
|
interact with it. The [puppeteer docs site](https://pptr.dev/) is a
|
2020-08-31 03:39:34 +02:00
|
|
|
useful reference for the available wait functions.
|
2021-09-08 00:23:24 +02:00
|
|
|
- When using `waitForSelector`, you always want to use the
|
|
|
|
`{visible: true}` option; otherwise the test will stop waiting as
|
|
|
|
soon as the target selector is present in the DOM even if it's
|
2021-08-20 21:53:28 +02:00
|
|
|
hidden. For the common UI pattern of having an element always be
|
2021-09-08 00:23:24 +02:00
|
|
|
present in the DOM whose presence is managed via show/hide rather
|
|
|
|
than adding/removing it from the DOM, `waitForSelector` without
|
|
|
|
`visible: true` won't wait at all.
|
2020-08-31 03:39:34 +02:00
|
|
|
- The test suite uses a smaller set of default user accounts and other
|
|
|
|
data initialized in the database than the normal development
|
|
|
|
environment; specifically, it uses the same setup as the [backend
|
2022-02-24 00:17:21 +01:00
|
|
|
tests](testing-with-django.md). To see what differs from
|
2020-08-31 03:39:34 +02:00
|
|
|
the development environment, check out the conditions on
|
|
|
|
`options["test_suite"]` in
|
|
|
|
`zilencer/management/commands/populate_db.py`.
|
|
|
|
|
|
|
|
[learn-async-await]: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
|