Skip to content

Commit e03e39e

Browse files
authored
Merge pull request #162 from replayio/filip/dev-511-add-doc-on-how-to-setup-replay-workflow-with-nextjs
move navigation items and add icons
2 parents 7121db2 + 0992981 commit e03e39e

File tree

10 files changed

+424
-3
lines changed

10 files changed

+424
-3
lines changed

.github/workflows/playwright.yml

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
name: Playwright Tests
2-
on: [deployment_status]
2+
on: [pull_request]
3+
34
jobs:
5+
wait-for-vercel:
6+
name: Wait for vercel
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Wait for Vercel preview deployment to be ready
10+
uses: patrickedqvist/[email protected]
11+
id: wait
12+
with:
13+
token: ${{ secrets.GITHUB_TOKEN }}
14+
max_timeout: 120
15+
outputs:
16+
preview_url: ${{ steps.wait.outputs.url }}
417
test:
5-
if: github.event.deployment_status.state == 'success'
618
timeout-minutes: 60
719
runs-on: ubuntu-latest
20+
needs: [wait-for-vercel]
821
steps:
922
- uses: actions/checkout@v4
1023
- uses: actions/setup-node@v4
@@ -17,5 +30,5 @@ jobs:
1730
- name: Run Playwright tests
1831
run: npx playwright test --project replay-chromium
1932
env:
20-
BASE_URL: ${{ github.event.deployment_status.target_url }}
33+
BASE_URL: ${{ needs.wait-for-vercel.outputs.preview_url }}
2134
REPLAY_API_KEY: ${{ secrets.REPLAY_API_KEY }}
Loading

src/app/basics/test-suites/pr-comments/page.md

+13
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ jobs:
3434
## Run your tests
3535
After you run your test, PR comment bot will collect links from your test step, and show them as a comment in your pull request conversation tab on GitHub.
3636

37+
{% figure
38+
alt="PR Comment bot in GitHub"
39+
src="/images/pull-request-comments.png"
40+
height=440
41+
width=870
42+
/%}
43+
44+
{% callout %}
45+
PR comment bot currently only listens to `pull_request` event, which means that comments will not appear on events such as `push` or `workflow_dispatch`. If you are running your tests against Vercel preview deployments, you can [check out this page](/learn/examples/vercel) to learn how to set up your workflow file.
46+
{% /callout %}
47+
3748
{% /steps %}
49+
50+

src/app/learn/examples/nextjs/page.md

+292
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
---
2+
title: Next.js example
3+
description: How to set up your development flow on a Next.js project. You can follow this tutorial step by step if you are starting a new project or jump to a section that is relevant for you.
4+
---
5+
6+
## Setup a new Next.js project
7+
To set up a new Next.js project you refer to the [example project in Next.js docs](https://nextjs.org/docs/pages/building-your-application/testing/playwright). This example contains Playwright as an end-to-end testing framework.
8+
9+
```sh
10+
npx create-next-app@latest --example with-playwright my-app
11+
```
12+
13+
## Debugging in dev mode
14+
### Setting up your environment
15+
Replay is a great development companion. Whenever you stumble upon an error, you can back up and replay what happened. To set up your development environment install Replay CLI:
16+
17+
{% tabs labels=["npm", "yarn", "pnpm", "bun"] %}
18+
{% tab %}
19+
20+
```sh
21+
npm i -g replayio
22+
```
23+
24+
{% /tab %}
25+
{% tab %}
26+
27+
```sh
28+
yarn add -g replayio
29+
```
30+
31+
{% /tab %}
32+
{% tab %}
33+
34+
```sh
35+
pnpm i -g replayio
36+
```
37+
38+
{% /tab %}
39+
{% tab %}
40+
41+
```sh
42+
bun i -g replayio
43+
```
44+
45+
{% /tab %}
46+
{% /tabs %}
47+
48+
### Recording your application
49+
Let’s assume you have started your dev server on `localhost:3000`.
50+
51+
Once Replay CLI is installed, you can create *a replay* by typing the following command in your terminal:
52+
53+
```sh
54+
replayio record http://localhost:3000
55+
```
56+
This command opens Replay Browser and starts recording your application running on localhost. Once you have caught the issue in Replay Browser, you can go back to the terminal and press any key to stop recording.
57+
58+
{% callout type="note" title="Running for the first time" %}
59+
If you are running Replay CLI for the first time, you may be prompted to log in to Replay App. To learn more about Replay CLI, [jump to this page](/reference/replay-cli/commands).
60+
{% /callout %}
61+
62+
Recording an issue will help you capture the root cause. Once you have a replay, you can jump into time-travelling DevTools, explore source files and add `console.log` to explore the runtime.
63+
64+
### Debugging your application
65+
66+
After you record your application, you will be prompted to upload your replay. Command line interface will generate a link which you can view in any browser.
67+
68+
```sh {% highlight=[9,10] %}
69+
~ replayio record http://localhost:3000
70+
Recording... (press any key to stop recording)
71+
72+
✔ New replays found. Would you like to upload it? (Y/n)
73+
74+
Uploaded replays
75+
✔ 6a464a3a… localhost:3000 11s ago 4.7s (uploaded)
76+
77+
View replays at:
78+
https://app.replay.io/recording/6a464a3a-37c5-482a-8743-3b758c9fb6f3
79+
```
80+
81+
Take a look at [this public replay](https://app.replay.io/recording/6a464a3a-37c5-482a-8743-3b758c9fb6f3) to see an example of how Replay DevTools look.
82+
83+
### What to debug
84+
You can create replays any time you encounter an issue. Replays are very effective when facing intermittent issues, race conditions, router problems, or any issue that either happens too quickly or is hard to identify.
85+
86+
We recommend creating shorter replays for better performance and clarity.
87+
88+
## Debugging Playwright tests
89+
90+
### Installing the Playwright Plugin package into your project
91+
The Next.js `with-playwright` project is initialized with a `playwright.config.ts` file. This contains many useful defaults for running your tests locally. To add Replay, you need to install the Replay plugin for Playwright and make updates to the config file.
92+
93+
{% tabs labels=["npm", "yarn", "pnpm", "bun"] %}
94+
{% tab %}
95+
96+
```sh
97+
npm install @replayio/playwright -D
98+
```
99+
100+
{% /tab %}
101+
{% tab %}
102+
103+
```sh
104+
yarn add @replayio/playwright -D
105+
```
106+
107+
{% /tab %}
108+
{% tab %}
109+
110+
```sh
111+
pnpm install @replayio/playwright -D
112+
```
113+
114+
{% /tab %}
115+
{% tab %}
116+
117+
```sh
118+
bun install @replayio/playwright -D
119+
```
120+
121+
{% /tab %}
122+
{% /tabs %}
123+
124+
After the installation, you’ll need to modify two attributes in the `playwright.config.ts` file:
125+
1. `reporter`
126+
2. `projects`
127+
128+
Your modified file will look like this (we removed the comments that are added by default)
129+
130+
```js {% fileName="playwright.config.ts" highlight=["24-30","33-36"] lineNumbers=true %}
131+
import { defineConfig, devices } from "@playwright/test";
132+
import path from "path";
133+
134+
const PORT = process.env.PORT || 3000;
135+
const baseURL = `http://localhost:${PORT}`;
136+
137+
export default defineConfig({
138+
timeout: 30 * 1000,
139+
testDir: path.join(__dirname, "e2e"),
140+
retries: 2,
141+
outputDir: "test-results/",
142+
webServer: {
143+
command: "npm run dev",
144+
url: baseURL,
145+
timeout: 120 * 1000,
146+
reuseExistingServer: !process.env.CI,
147+
},
148+
149+
use: {
150+
baseURL,
151+
trace: "retry-with-trace",
152+
},
153+
154+
reporter: [
155+
createReplayReporterConfig({
156+
apiKey: process.env.REPLAY_API_KEY,
157+
upload: true,
158+
}),
159+
['line'],
160+
],
161+
162+
projects: [
163+
{
164+
name: 'replay-chromium',
165+
use: { ...replayDevices['Replay Chromium'] },
166+
},
167+
{
168+
name: "Desktop Chrome",
169+
use: {
170+
...devices["Desktop Chrome"],
171+
},
172+
}, {
173+
name: "Mobile Chrome",
174+
use: {
175+
...devices["Pixel 5"],
176+
},
177+
},
178+
{
179+
name: "Mobile Safari",
180+
use: devices["iPhone 12"],
181+
},
182+
],
183+
});
184+
```
185+
186+
### Recording your tests
187+
If you are experiencing issues with your tests, such as random failures or flakiness, you can record your tests simply by running the `replay-chromium` project. The `upload: true` attribute will take care of uploading every test replay after the test run.
188+
189+
You can choose to set this setting to `false` and use `replayio upload` command from CLI to upload your test recordings manually.
190+
191+
For examples on how to record your tests on a CI pipeline, check out [reference docs](/reference/test-runners/playwright/github-actions) or examples on how to record a [Vercel preview deployment](/learn/examples/vercel).
192+
193+
{% callout title="Generating API key" %}
194+
In order to upload your replays, an API key needs to be set up in your environment. Check out the reference guide to learn how to [generate an API key](/reference/ci-workflows/generate-api-key)
195+
{% /callout %}
196+
197+
### Debugging your tests
198+
199+
{% figure
200+
src="/images/playwright-devtools-screenshot.png"
201+
width=870
202+
height=870
203+
/%}
204+
205+
Replay DevTools bring you the power of browser DevTools, but with time traveling capability. This becomes incredibly useful for cases like shown here.
206+
207+
This example shows an ecommerce site, where two API calls are called. One checks availability of items, and other one adds it to cart. The availability gets fetched and a `quantity` state is updated.
208+
209+
```js {% fileName="ProductDetail.tsx (abbreviated)" lineNumbers=true highlight=[1,21] %}
210+
const [quantity, setQuantity] = useState(0);
211+
212+
// check availability API call
213+
const fetchAvailability = async () => {
214+
const response = await fetch(`/api/check-availability`);
215+
if (!response.ok) {
216+
throw new Error('Network response was not ok');
217+
}
218+
return response.json();
219+
};
220+
221+
// get data from /check-availability response
222+
const { data } = useQuery({
223+
queryKey: ['availability'],
224+
queryFn: fetchAvailability,
225+
});
226+
227+
// update quantity and availability
228+
useEffect(() => {
229+
if (data) {
230+
setQuantity(data.qty);
231+
}
232+
}, [data]);
233+
```
234+
235+
The `quantity` state is then used as payload in the "add to cart" request.
236+
237+
```js {% fileName="ProductDetail.tsx (abbreviated)" lineNumbers=true highlight=["8-10"] %}
238+
// add to cart
239+
const addToCart = async () => {
240+
const response = await fetch('/api/add-to-cart', {
241+
method: 'POST',
242+
headers: {
243+
'Content-Type': 'application/json',
244+
},
245+
body: JSON.stringify({
246+
qty: quantity,
247+
}),
248+
});
249+
250+
if (response.ok) {
251+
setError(false);
252+
setMessage('Product added to cart!');
253+
} else {
254+
setError(true);
255+
const res = await response.json();
256+
setMessage(res.message);
257+
}
258+
};
259+
```
260+
261+
However, depending on the speed of response from `/api/check-availability` endpoint, the `quantity` will either be 0 (the default value) or 1 (fetched value).
262+
263+
This problem can be revealed by adding console.logs into the replay. These will reveal the race condition happening in the failed test vs. passing test.
264+
265+
> [Check out this replay](https://replay.help/playwright-flake-debug) for a detailed walkthrough on debugging a flaky Playwright test.
266+
267+
### Comparison with trace-viewer
268+
Trace-viewer is a powerful tool for reviewing your Playwright test run. It contains screenshots, network logs, step details and a timeline of DOM snapshots. It can help you zoom into each test step and examine visuals and logs from the application.
269+
270+
Replay takes this concept further. You are getting all the advantages of trace-viewer with the added capability of exploring your source code adding console.logs to reveal the flow of your data and ability to jump to any line of code. You can stop at a perfect moment that is not captured by any DOM snapshot from trace-viewer, or is simply happening in between these snapshots.
271+
272+
When facing flaky tests, reproducing an issue locally is no longer needed, because all of the information is already captured.
273+
274+
You can read more about [comparison with Playwright’s trace-viewer here](/learn/comparisons/playwright).
275+
276+
{% quick-links title="Read more" description="Learn how to record your tests, manage your test suite and debug flaky tests using Replay DevTools" %}
277+
278+
{% quick-link
279+
title="Debugging Vercel branch preview deployments"
280+
icon="vercel"
281+
href="/learn/examples/vercel"
282+
description="Learn how to run debug tests from your Vercel preview deployments"
283+
/%}
284+
285+
{% quick-link
286+
title="PR comments"
287+
icon="github"
288+
href="/basics/test-suites/pr-comments"
289+
description="The Replay GitHub PR comment makes it easy to view a PR's recent runs."
290+
/%}
291+
292+
{% /quick-links %}

0 commit comments

Comments
 (0)