|
| 1 | +# Headless tests with Playwright |
| 2 | + |
| 3 | +There are currently 3 general automated tests: |
| 4 | + |
| 5 | +1. `jest` tests against real English content (and some code) |
| 6 | +1. `jest` tests against fixture content |
| 7 | +1. `playwright` tests against fixture content (What this document is about!) |
| 8 | + |
| 9 | +## Quickstart |
| 10 | + |
| 11 | +Just like with regular `jest` tests, if you haven't already done so... |
| 12 | + |
| 13 | +```sh |
| 14 | +npm run build |
| 15 | +``` |
| 16 | + |
| 17 | +Now, to run all the tests: |
| 18 | + |
| 19 | +```sh |
| 20 | +npm run playwright-test |
| 21 | +``` |
| 22 | + |
| 23 | +That command will automatically start a server (on `localhost:4000`) for |
| 24 | +the duration of test suite. It then finds all `tests/**/*.spec.ts` |
| 25 | +files and run them by using `Google Chrome` as the underlying browser. |
| 26 | + |
| 27 | +If you have [set up a local Elasticsearch server](../search/elasticsearch-locally.md) (`localhost:9200`) the |
| 28 | +headless tests will test doing site-searches if you've set up |
| 29 | +an `ELASTICSEARCH_URL` environment variable. |
| 30 | + |
| 31 | +## Introduction |
| 32 | + |
| 33 | +The best documentation is <https://playwright.dev/> and this documentation |
| 34 | +here is merely an introduction to it. |
| 35 | + |
| 36 | +Refer to it when writing tests and trying to figure out how to use certain |
| 37 | +[locators](https://playwright.dev/docs/locators) which is important |
| 38 | +things, like `page.getByAltText()`, which you'll need for tying the browsing |
| 39 | +to your assertions. |
| 40 | + |
| 41 | +### What to test |
| 42 | + |
| 43 | +Beyond some basic happy path tests, **only test what `jest` can't test**. |
| 44 | +In particular this means client-side JavaScript interactions. For example, |
| 45 | +`jest` can fetch the HTML over HTTP and assert against the `cheerio` parsed |
| 46 | +HTML, but it can't test what happens when you click a client-side routing |
| 47 | +link that triggers some sort of user agent interaction. |
| 48 | + |
| 49 | +`jest` is always faster. Playwright tests can test things like displaying |
| 50 | +different things depending on cookies or `localStorage`. Playwright tests |
| 51 | +can test the visual presence of something. For example, if something |
| 52 | +like `<div style="display:none">Text here</div>` is in the DOM only |
| 53 | +Playwright can understand that it's not actually present in the page since |
| 54 | +`jest` and Cheerio can't understand CSS. |
| 55 | + |
| 56 | +Think of your headless tests as "What would a human QA person do?" |
| 57 | +The imaginary QA person can be you. If there's something you find yourself |
| 58 | +doing to make sure your functionality doesn't regress as it's changing, |
| 59 | +consider that to be motivation enough to write a headless test. |
| 60 | + |
| 61 | +## VSCode |
| 62 | + |
| 63 | +["Playwright Test for VSCode"](https://marketplace.visualstudio.com/items?itemName=ms-playwright.playwright) |
| 64 | +is a great extension for people who use VSCode. Once installed, open the |
| 65 | +file `playwright-rendering.spec.ts` and start the command |
| 66 | +palette (`Cmd`+`Shift`+`p`) and type "Testing: Focus |
| 67 | +on Playwright View" which will display the "TESTING" sidebar. It finds |
| 68 | +all the file's tests as a tree and for each test there's a Play button. |
| 69 | +You can either play a specific single test or you can make it run all |
| 70 | +tests. |
| 71 | + |
| 72 | +Note, that failiure is often the result of the Playwright test waiting |
| 73 | +very patiently for something to be present but it can't be found. I.e. |
| 74 | +failures are often the same thing as Playwright reaching a waiting timeout. |
| 75 | +This can make it feel like nothing's happening. |
| 76 | + |
| 77 | +Near the bottom of the "TESTING" sidebar is an extra menu specifically for |
| 78 | +Playwright. One very useful option is the "[ ] Show browser" which means a |
| 79 | +browser window will appear when tests run. |
| 80 | + |
| 81 | +## CLI |
| 82 | + |
| 83 | +The most basic command is: |
| 84 | + |
| 85 | +```sh |
| 86 | +npm run playwright-test -- --help |
| 87 | +``` |
| 88 | + |
| 89 | +This will guide you to all the options possible. For example, |
| 90 | + |
| 91 | +```sh |
| 92 | +npm run playwright-test -- --headed |
| 93 | +``` |
| 94 | + |
| 95 | +...will open a browser flickering through the tests. |
| 96 | + |
| 97 | +```sh |
| 98 | +npm run playwright-test -- playwright-rendering.spec.ts |
| 99 | +``` |
| 100 | + |
| 101 | +...will only run the tests in a file by that name. |
| 102 | + |
| 103 | +```sh |
| 104 | +npm run playwright-test -- playwright-rendering.spec.ts:16 |
| 105 | +``` |
| 106 | + |
| 107 | +...will run that specific `test('description here', async ({ page }))` on |
| 108 | +line 16. |
| 109 | + |
| 110 | +```sh |
| 111 | +npm run playwright-test -- -g "view home page" |
| 112 | +``` |
| 113 | + |
| 114 | +...will only run tests whose description contains that text. |
| 115 | + |
| 116 | +## Debugging |
| 117 | + |
| 118 | +Writing tests can be difficult until all the locators feel like |
| 119 | +second nature. You might be struggling with finding something in the |
| 120 | +page which you're not sure is there or you don't know exactly |
| 121 | +how to refer to it. |
| 122 | + |
| 123 | +The first thing to do is familiarize yourself with how to run the CLI |
| 124 | +that only opens the one specific test you're debugging. Then, you |
| 125 | +run the CLI with `--debug --headed`. For example: |
| 126 | + |
| 127 | +```sh |
| 128 | +npm run playwright-test -- -g "view home page" --debug --headed |
| 129 | +``` |
| 130 | + |
| 131 | +Now, it should open an additional debugger window next to a browser |
| 132 | +window and you can press the play button there. When it gets stuck you can |
| 133 | +use the browser window to do things like right-clicking and "Inspect..." |
| 134 | +to understand what's in the DOM. |
| 135 | + |
| 136 | +Another thing that can help debugging is to open the browser just like |
| 137 | +the script does. Run: |
| 138 | + |
| 139 | +```sh |
| 140 | +npm run start-for-playwright |
| 141 | +``` |
| 142 | + |
| 143 | +and open your regular browser window on <http://localhost:4000>. |
| 144 | +When you're done, don't forgot to stop the server otherwise |
| 145 | +the `npm run playwright-test` command won't work. |
| 146 | + |
| 147 | +## Codegen |
| 148 | + |
| 149 | +Codegen is when Playwright starts a browser and a debugger window. In the |
| 150 | +debugger window it generates TypeScript code which you can copy-and-paste |
| 151 | +into your editor/IDE when you're done. To use codegen you need to |
| 152 | +first manually start the server. In the **first terminal**: |
| 153 | + |
| 154 | +```sh |
| 155 | +npm run build && npm run start-for-playwright |
| 156 | +``` |
| 157 | + |
| 158 | +In a **second terminal**: |
| 159 | + |
| 160 | +```sh |
| 161 | +npx playwright codegen |
| 162 | +``` |
| 163 | + |
| 164 | +Now type in `localhost:4000` in the browser window and click around. |
| 165 | +Note how the TypeScript code gets written. It's definitely not perfect |
| 166 | +but it can save you a lot of time writing selectors. |
| 167 | + |
| 168 | +Note that the codegen code will not have any assertions other than |
| 169 | +sheer presence. It might also contain things like |
| 170 | +`await page.goto('http://localhost:4000')` which you can later |
| 171 | +correct to `await page.goto('/')`. |
| 172 | + |
| 173 | +When you have pasted over the TypeScript code from the debugger window, |
| 174 | +you can click into that second terminal and press `Ctrl`+`C` to stop |
| 175 | +the codegen debugger. |
| 176 | + |
| 177 | +## More browsers |
| 178 | + |
| 179 | +At the moment (March 2023) we don't test more browsers in Actions. |
| 180 | +The primary use case at the moment is testing that client-side |
| 181 | +interactions work at all. Actual cross-browser testing is not a priority |
| 182 | +at the current time. |
| 183 | + |
| 184 | +## Tips on writing tests |
| 185 | + |
| 186 | +- What would a human be able to assert? If you find yourself testing things |
| 187 | +that you expect in the DOM that a human wouldn't be able to test, the |
| 188 | +test might not be a good test. For example, to make an assertion that |
| 189 | +a certain div has `class="blabla"` if you click on a certain thing. Either |
| 190 | +test something visual or perhaps don't bother testing it with Playwright. |
| 191 | + |
| 192 | +- *Combine* codegen tests and manual editing is a great combination. |
| 193 | +Use the codegen output but familiarize yourself with the Playwright |
| 194 | +documentation how to do things like locators and/or assertions. |
| 195 | + |
| 196 | +- When you use the codegen, it's clever in that it can attach to `data-testid` |
| 197 | +nodes in your DOM. That's a good thing. If it's unable to do that, |
| 198 | +consider going into the React code and add some more. |
0 commit comments