Skip to content

Commit 95ddb4d

Browse files
committed
Start last tutorial section
1 parent 40a6ec1 commit 95ddb4d

6 files changed

Lines changed: 157 additions & 34 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@
4545
"@biomejs/biome": "^2.2.2",
4646
"@playwright/test": "^1.55.0",
4747
"@types/bun": "^1.2.21",
48-
"endform": "^0.35.3"
48+
"endform": "^0.35.4"
4949
}
5050
}

‎pnpm-lock.yaml‎

Lines changed: 55 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tutorial.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const tutorialConfig: TutorialConfig = {
5151
name: "stage-4-completed",
5252
title: "Understanding test results with the Endform dashboard",
5353
order: 4,
54-
newPaths: [],
54+
newPaths: ["tests/flaky-test.spec.ts"],
5555
},
5656
],
5757
};
395 KB
Loading
301 KB
Loading

‎tutorial/stage-4-completed/understanding-test-results.mdx‎

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ sidebar:
55
order: 5
66
---
77

8-
import { Aside } from '@astrojs/starlight/components';
8+
import { Aside, Tabs, TabItem } from '@astrojs/starlight/components';
9+
import { Image } from "astro:assets";
10+
import HtmlReportTrace from "../../../assets/html-report-trace.png";
11+
import ExampleTrace from "../../../assets/example-trace.png";
912

1013
In this guide, we will use the endform dashboard to understand the test results.
1114

@@ -14,6 +17,99 @@ To start from this step, checkout the [`stage-3-endform-integration` branch](htt
1417
For more information about getting started with the tutorial repository, go [here](/docs/tutorial/playwright-e2e-from-cowboy-to-confidence#getting-started-with-the-tutorial-repository).
1518
</Aside>
1619

17-
<Aside type="caution">
18-
This tutorial is a work in progress! More coming soon.
19-
</Aside>
20+
Real world tests don't always pass the first time you write them or forever more.
21+
There is near no point in writing or running end-to-end tests if we don't know how to debug them quickly and easily.
22+
23+
We covered in part one of the tutorial how to use the debug flag with Playwright `pnpm playwright test --debug`.
24+
This is a great option for understanding what's going on in a test while you're developing it.
25+
26+
In many cases we want to be able to look at test runs after they've completed.
27+
Let's take a look at the most powerful options we have available to us to debug Playwright end to end tests after they've completed.
28+
29+
30+
## Playwright traces
31+
32+
Start by setting the `trace` option to `on` in your `playwright.config.ts` file.
33+
34+
```ts
35+
export default defineConfig({
36+
// Create a HTML report of the test run
37+
reporter: [["html", { open: "never" }]],
38+
use: {
39+
// Always create a trace of the test run even if the test passes.
40+
trace: "on",
41+
},
42+
});
43+
```
44+
45+
Then kick off a run of your suite. `pnpm playwright test` and boot the html report server `pnpm playwright show-report`
46+
47+
<Image src={HtmlReportTrace} alt="Playwright HTML reporter with trace" width={900} />
48+
49+
We now click on one of the tests since we had trace set to on we should have a full trace file for each of the successful tests in our suite.
50+
51+
<Image src={ExampleTrace} alt="Example trace file" width={900} />
52+
53+
Here we can navigate the different steps that the test took.
54+
We can look at screenshots of what was going on while the test was running.
55+
Then we can also inspect parts of the browser while the test was running, including things like the network tab, console logs from the browser.
56+
57+
## Add a flaky test
58+
59+
One of the most common problems you'll have with your suite are tests that only sometimes fail, otherwise known as flaky tests.
60+
Let's see what it's like to work with the flaky test in our suite.
61+
62+
<Tabs>
63+
<TabItem label="tests/flaky-test.spec.ts">
64+
```ts
65+
import { expect, test } from "@playwright/test";
66+
67+
test.describe("Flaky Test", () => {
68+
test("should pass most of the time, but sometimes fail", async ({ page }) => {
69+
await page.goto("/dashboard");
70+
await expect(
71+
page.getByRole("heading", { name: "Team Settings" }),
72+
).toBeVisible();
73+
74+
// Let's make this test flaky by randomly failing 30% of the time
75+
const randomNumber = Math.random();
76+
expect(randomNumber).toBeLessThan(0.7);
77+
});
78+
});
79+
```
80+
</TabItem>
81+
</Tabs>
82+
83+
Let's set the trace setting back to retain on failure.
84+
85+
```ts
86+
export default defineConfig({
87+
use: {
88+
// Keep traces for failed tests
89+
trace: "retain-on-failure",
90+
},
91+
});
92+
```
93+
94+
Now let's try running the tests again. We will have to run it many times to simulate the flakiness.
95+
96+
```bash
97+
pnpm playwright test --repeat-each=10 flaky-test.spec.ts
98+
```
99+
100+
Take a look at the HTML report. You have at least one failing test, otherwise you're very lucky!
101+
102+
## Results in the Endform dashboard
103+
104+
Normally the problem with flaky tests is that you won't get the failures nice and put together in a single report,
105+
but they'll be spread out across lots of reports in lots of different runs.
106+
107+
This is where the Endform dashboard can really shine, because it can show you lots of results over weeks and months of historical data.
108+
109+
Let's run the flaky test with Endform.
110+
111+
```bash
112+
pnpm endform test --repeat-each=10 flaky-test.spec.ts
113+
```
114+
115+
There you'll get a link to the Endform dashboard where you can also look at the results.

0 commit comments

Comments
 (0)