Skip to content

Commit 9ad507d

Browse files
authored
doc(test): pass through test docs (#6914)
1 parent ec2b6a7 commit 9ad507d

File tree

3 files changed

+104
-30
lines changed

3 files changed

+104
-30
lines changed

docs/src/test-annotations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: test-annotations
33
title: "Annotations"
44
---
55

6-
Sadly, tests do not always pass. Playwright Test supports test annotations to deal with failures, flakiness and tests that are not yet ready.
6+
Sadly, tests do not always pass. Luckily, Playwright Test supports test annotations to deal with failures, flakiness and tests that are not yet ready.
77

88
```js js-flavor=js
99
// example.spec.js

docs/src/test-intro.md

Lines changed: 98 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@ test('my test', async ({ page }) => {
232232
expect(await page.getAttribute('text=Get Started', 'href')).toBe('/docs/intro');
233233

234234
// Expect an element "to be visible".
235-
expect(await page.isVisible('[aria-label="GitHub repository"]')).toBe(true);
235+
expect(await page.isVisible('text=Learn more')).toBeTruthy();
236236

237237
await page.click('text=Get Started');
238238
// Expect some text to be visible on the page.
239-
expect(await page.waitForSelector('text=Getting Started')).toBeTruthy();
239+
expect(await page.waitForSelector('text=System requirements')).toBeTruthy();
240240

241241
// Compare screenshot with a stored reference.
242242
expect(await page.screenshot()).toMatchSnapshot('get-started.png');
@@ -257,17 +257,39 @@ test('my test', async ({ page }) => {
257257
expect(await page.getAttribute('text=Get Started', 'href')).toBe('/docs/intro');
258258

259259
// Expect an element "to be visible".
260-
expect(await page.isVisible('[aria-label="GitHub repository"]')).toBe(true);
260+
expect(await page.isVisible('[aria-label="GitHub repository"]')).toBeTruthy();
261261

262262
await page.click('text=Get Started');
263263
// Expect some text to be visible on the page.
264-
expect(await page.waitForSelector('text=Getting Started')).toBeTruthy();
264+
expect(await page.waitForSelector('text=System requirements')).toBeTruthy();
265265

266266
// Compare screenshot with a stored reference.
267267
expect(await page.screenshot()).toMatchSnapshot('get-started.png');
268268
});
269269
```
270270
271+
Notice how running this test is saying:
272+
273+
```
274+
Error: example.spec.ts-snapshots/get-started-chromium-darwin.png is missing in snapshots, writing actual.
275+
```
276+
277+
That's because there was no golden file for your `get-started.png` snapshot. It is now created and is ready to be added to the repository. The name of the folder with the golden expectations starts with the name of your test file:
278+
279+
```bash
280+
drwxr-xr-x 5 user group 160 Jun 4 11:46 .
281+
drwxr-xr-x 6 user group 192 Jun 4 11:45 ..
282+
-rw-r--r-- 1 user group 231 Jun 4 11:16 example.spec.ts
283+
drwxr-xr-x 3 user group 96 Jun 4 11:46 example.spec.ts-snapshots
284+
```
285+
286+
To update your golden files, you can use the `--update-snapshots` parameter.
287+
288+
```bash
289+
npx playwright test -c tests --update-snapshots
290+
```
291+
292+
271293
## Learn the command line
272294
273295
Here are the most common options available in the [command line](./test-cli.md).
@@ -335,37 +357,87 @@ Here are the most common options available in the [command line](./test-cli.md).
335357
336358
So far, we've looked at the zero-config operation of Playwright Test. For a real world application, it is likely that you would want to use a config.
337359
338-
Create `playwright.config.js` (or `playwright.config.ts`) to configure your tests. You can specify browser launch options, run tests in multiple browsers and much more with the config. Here is an example configuration that runs every test in Chromium, Firefox and WebKit. Look for more options in the [configuration section](./test-configuration.md).
360+
Create `playwright.config.ts` (or `playwright.config.js`) to configure your tests. You can specify browser launch options, run tests in multiple browsers and much more with the config. Here is an example configuration that runs every test in Chromium, Firefox and WebKit, both Desktop and Mobile versions. Look for more options in the [configuration section](./test-configuration.md).
339361
340362
```js js-flavor=js
341-
module.exports = {
342-
// Each test is given 30 seconds.
343-
timeout: 30000,
363+
// playwright.config.js
364+
const { devices } = require('@playwright/test');
344365

345-
use: {
346-
// Run browsers in the headless mode.
347-
headless: true,
348-
349-
// Specify the viewport size.
350-
viewport: { width: 1280, height: 720 },
351-
},
366+
module.exports = {
367+
projects: [
368+
{
369+
name: 'Desktop Chromium',
370+
use: {
371+
browserName: 'chromium',
372+
// Test against Chrome Beta channel.
373+
channel: 'chrome-beta',
374+
},
375+
},
376+
{
377+
name: 'Desktop Safari',
378+
use: {
379+
browserName: 'webkit',
380+
viewport: { width: 1200, height: 750 },
381+
}
382+
},
383+
// Test against mobile viewports.
384+
{
385+
name: 'Mobile Chrome',
386+
use: devices['Pixel 5'],
387+
},
388+
{
389+
name: 'Mobile Safari',
390+
use: devices['iPhone 12'],
391+
},
392+
{
393+
name: 'Desktop Firefox',
394+
use: {
395+
browserName: 'firefox',
396+
viewport: { width: 800, height: 600 },
397+
}
398+
},
399+
],
352400
};
353401
```
354402
355403
```js js-flavor=ts
356-
import { PlaywrightTestConfig } from '@playwright/test';
404+
// playwright.config.ts
405+
import { PlaywrightTestConfig, devices } from '@playwright/test';
357406

358407
const config: PlaywrightTestConfig = {
359-
// Each test is given 30 seconds.
360-
timeout: 30000,
361-
362-
use: {
363-
// Run browsers in the headless mode.
364-
headless: true,
365-
366-
// Specify the viewport size.
367-
viewport: { width: 1280, height: 720 },
368-
},
408+
projects: [
409+
{
410+
name: 'Desktop Chromium',
411+
use: {
412+
browserName: 'chromium',
413+
// Test against Chrome Beta channel.
414+
channel: 'chrome-beta',
415+
},
416+
},
417+
{
418+
name: 'Desktop Safari',
419+
use: {
420+
browserName: 'webkit',
421+
viewport: { width: 1200, height: 750 },
422+
}
423+
},
424+
// Test against mobile viewports.
425+
{
426+
name: 'Mobile Chrome',
427+
use: devices['Pixel 5'],
428+
},
429+
{
430+
name: 'Mobile Safari',
431+
use: devices['iPhone 12'],
432+
},
433+
{
434+
name: 'Desktop Firefox',
435+
use: {
436+
browserName: 'firefox',
437+
viewport: { width: 800, height: 600 },
438+
}
439+
},
440+
],
369441
};
370442
export default config;
371443
```

docs/src/test-parallel.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Playwright Test runs tests in parallel by default, using multiple worker process
1111

1212
Each worker process creates a new environment to run tests. By default, Playwright Test reuses the worker as much as it can to make testing faster.
1313

14-
However, test runner will create a new worker when retrying tests, after any test failure, to initialize a new environment, or just to speed up test execution if the worker limit is not reached.
14+
Should any test fail, Playwright will discard entire worker process along with the browsers used and will start a new one. That way failing tests can't affect healthy ones.
1515

16-
You can control the maximum number of worker processes via [command line](./test-cli.md) or in the [configuration file](./test-configuration.md).
16+
You can control the maximum number of parallel worker processes via [command line](./test-cli.md) or in the [configuration file](./test-configuration.md).
1717

1818
- Run in parallel by default
1919
```bash
@@ -50,7 +50,7 @@ You can control the maximum number of worker processes via [command line](./test
5050
export default config;
5151
```
5252

53-
Each worker process is assigned a unique sequential index that is accessible through the [`workerInfo`](./test-advanced.md#workerinfo) object.
53+
Each worker process is assigned a unique sequential index that is accessible through the [`workerInfo`](./test-advanced.md#workerinfo) object. Since each worker is a process, there also is a process-wide environment variable `process.env.TEST_WORKER_INDEX` that has the same value.
5454

5555
## Shards
5656

@@ -61,3 +61,5 @@ npx playwright test --shard=1/3
6161
npx playwright test --shard=2/3
6262
npx playwright test --shard=3/3
6363
```
64+
65+
That way your test suite completes 3 times faster.

0 commit comments

Comments
 (0)