Skip to content

Commit 0c54548

Browse files
committed
add upload strategy examples
1 parent 09eded2 commit 0c54548

File tree

2 files changed

+191
-1
lines changed

2 files changed

+191
-1
lines changed

src/app/ci-workflows/upload-strategies/page.md

+190-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,196 @@ While uploading just failed test is good for saving resources, our recommendatio
88
{% /callout %}
99

1010
## Upload failed tests only
11-
By default, all test replays are uploaded no matter the result. If you want upload only the failed recordings, you can define the step to do so using `filter` property:
11+
By default, all test replays are uploaded no matter the result. If you want to upload only the failed recordings, you can use the `filter` property in the plugin configuration:
12+
13+
{% tabs labels=["cypress", "playwright"] %}
14+
{% tab %}
15+
```js
16+
export default defineConfig({
17+
e2e: {
18+
setupNodeEvents(on, config) {
19+
replayPlugin(on, config, {
20+
upload: true,
21+
apiKey: process.env.REPLAY_API_KEY,
22+
filter: function (recording) {
23+
// upload runtime crashes and any failed tests
24+
return (
25+
recording.status === "crashed" ||
26+
recording.metadata.test.result === "failed"
27+
);
28+
}
29+
})
30+
return config
31+
},
32+
},
33+
})
34+
```
35+
{% /tab %}
36+
{% tab %}
37+
```js
38+
const config: PlaywrightTestConfig = {
39+
reporter: [["@replayio/playwright/reporter", {
40+
apiKey: process.env.REPLAY_API_KEY,
41+
upload: true
42+
filter: function (recording) {
43+
// upload runtime crashes and any failed tests
44+
return (
45+
recording.status === "crashed" ||
46+
recording.metadata.test.result === "failed"
47+
);
48+
}
49+
}], ['line']],
50+
projects: [
51+
{
52+
name: "replay-chromium",
53+
use: { ...replayDevices["Replay Chromium"] as any },
54+
}
55+
],
56+
};
57+
export default config;
58+
```
59+
{% /tab %}
60+
{% /tabs %}
61+
62+
63+
## Upload failed and flaky Cypress tests
64+
By default, all test replays are uploaded no matter the result. If you want to upload failed and flaky tests, you can use the `filter` property in the plugin configuration:
65+
66+
```js
67+
export default defineConfig({
68+
e2e: {
69+
setupNodeEvents(on, config) {
70+
replayPlugin(on, config, {
71+
upload: true,
72+
apiKey: process.env.REPLAY_API_KEY,
73+
filter: function (recording) {
74+
// upload runtime crashes and recordings with any tests that failed
75+
return (
76+
recording.status === "crashed" ||
77+
recording.metadata.test.tests.some(test => test.result === "failed")
78+
);
79+
}
80+
})
81+
return config
82+
},
83+
},
84+
})
85+
```
86+
87+
## Upload only for the primary branch
88+
89+
The recording metadata includes some details about the source control including the repository and branch name which can also be used to filter your uploads. The example below uploads all recordings from the `main` branch:
90+
91+
{% tabs labels=["cypress", "playwright"] %}
92+
{% tab %}
93+
```js
94+
export default defineConfig({
95+
e2e: {
96+
setupNodeEvents(on, config) {
97+
replayPlugin(on, config, {
98+
upload: true,
99+
apiKey: process.env.REPLAY_API_KEY,
100+
filter: function (recording) {
101+
return recording.metadata.source.branch === "main";
102+
}
103+
})
104+
return config
105+
},
106+
},
107+
})
108+
```
109+
{% /tab %}
110+
{% tab %}
111+
```js
112+
const config: PlaywrightTestConfig = {
113+
reporter: [["@replayio/playwright/reporter", {
114+
apiKey: process.env.REPLAY_API_KEY,
115+
upload: true
116+
filter: function (recording) {
117+
return recording.metadata.source.branch === "main";
118+
}
119+
}], ['line']],
120+
projects: [
121+
{
122+
name: "replay-chromium",
123+
use: { ...replayDevices["Replay Chromium"] as any },
124+
}
125+
],
126+
};
127+
export default config;
128+
```
129+
{% /tab %}
130+
{% /tabs %}
131+
132+
133+
## Upload some passing runs
134+
135+
If you've adopted one the configurations above but would also like to periodically upload all replays for a test run, you can add a condition to the filter that returns `true` for a given test run id. This is only one possible implementation of this approach and you're welcome to adopt others such as using external environment variables.
136+
137+
{% tabs labels=["cypress", "playwright"] %}
138+
{% tab %}
139+
```js
140+
const convertStringToInt = string =>
141+
string.split("").reduce((acc, char) => acc + char.charCodeAt(0), 0);
142+
143+
export default defineConfig({
144+
e2e: {
145+
setupNodeEvents(on, config) {
146+
replayPlugin(on, config, {
147+
upload: true,
148+
apiKey: process.env.REPLAY_API_KEY,
149+
filter: function (recording) {
150+
// randomly upload 10% of all test runs
151+
if (convertStringToInt(r.metadata.test.run.id) % 10 === 1) {
152+
return true;
153+
}
154+
155+
// upload runtime crashes and any failed tests
156+
return (
157+
recording.status === "crashed" ||
158+
recording.metadata.test.result === "failed"
159+
);
160+
}
161+
})
162+
return config
163+
},
164+
},
165+
})
166+
```
167+
{% /tab %}
168+
{% tab %}
169+
```js
170+
const convertStringToInt = string =>
171+
string.split("").reduce((acc, char) => acc + char.charCodeAt(0), 0);
172+
173+
const config: PlaywrightTestConfig = {
174+
reporter: [["@replayio/playwright/reporter", {
175+
apiKey: process.env.REPLAY_API_KEY,
176+
upload: true
177+
filter: function (recording) {
178+
// randomly upload 10% of all test runs
179+
if (convertStringToInt(r.metadata.test.run.id) % 10 === 1) {
180+
return true;
181+
}
182+
183+
// upload runtime crashes and any failed tests
184+
return (
185+
recording.status === "crashed" ||
186+
recording.metadata.test.result === "failed"
187+
);
188+
}
189+
}], ['line']],
190+
projects: [
191+
{
192+
name: "replay-chromium",
193+
use: { ...replayDevices["Replay Chromium"] as any },
194+
}
195+
],
196+
};
197+
export default config;
198+
```
199+
{% /tab %}
200+
{% /tabs %}
12201

13202
## Using GitHub Action
14203
```yml {% fileName=".github/workflows/e2e.yml" lineNumbers=true highlight=[19] %}

src/videos/example.mp4.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"status":"pending","originalFilePath":"src/videos/example.mp4","provider":"mux","providerMetadata":{},"createdAt":1714169187729,"updatedAt":1714169187729,"size":1570024}

0 commit comments

Comments
 (0)