-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathabort.spec.ts
More file actions
94 lines (76 loc) · 2.88 KB
/
abort.spec.ts
File metadata and controls
94 lines (76 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { expect, test } from "@playwright/test"
/**
* route.abort() drops a request entirely — the browser receives a network
* error for that request. Use it to block unwanted traffic or test fallbacks.
*/
test("block analytics calls so tests don't pollute real data", async ({ page }) => {
const blockedRequests: string[] = []
await page.route("**/analytics/**", async (route) => {
blockedRequests.push(route.request().url())
await route.abort()
})
await page.setContent(`
<button id="purchase">Buy now</button>
<p id="confirmation" hidden>Order confirmed!</p>
<script>
document.getElementById('purchase').onclick = async () => {
fetch('http://analytics.test/analytics/events', {
method: 'POST',
body: JSON.stringify({ event: 'purchase_clicked' })
}).catch(() => {})
document.getElementById('confirmation').hidden = false
}
</script>
`)
await page.getByRole("button", { name: "Buy now" }).click()
await expect(page.locator("#confirmation")).toBeVisible()
expect(blockedRequests.length).toBe(1)
expect(blockedRequests[0]).toContain("/analytics/events")
})
test("block image requests to focus on page text structure", async ({ page }) => {
let imageRequestCount = 0
await page.route("**/*.{png,jpg,jpeg,gif,webp,svg}", async (route) => {
imageRequestCount++
await route.abort()
})
await page.setContent(`
<h1>Product Catalog</h1>
<article>
<img src="http://cdn.test/images/product-1.jpg" alt="Wireless headphones" />
<h2>Wireless Headphones</h2>
<p>Premium sound quality</p>
</article>
<article>
<img src="http://cdn.test/images/product-2.png" alt="Mechanical keyboard" />
<h2>Mechanical Keyboard</h2>
<p>Tactile typing experience</p>
</article>
`)
await expect(page.getByRole("heading", { name: "Product Catalog" })).toBeVisible()
await expect(page.getByRole("heading", { name: "Wireless Headphones" })).toBeVisible()
await expect(page.getByRole("heading", { name: "Mechanical Keyboard" })).toBeVisible()
expect(imageRequestCount).toBe(2)
})
test("test app behavior when a required service is unavailable", async ({ page }) => {
await page.route("**/api/feature-flags", async (route) => {
await route.abort()
})
await page.setContent(`
<div id="app">Loading...</div>
<script>
async function init() {
let flags = { newDashboard: false }
try {
const res = await fetch('http://mock.test/api/feature-flags')
flags = await res.json()
} catch {
// Feature flag service is down — fall back to safe defaults
}
const app = document.getElementById('app')
app.textContent = flags.newDashboard ? 'New Dashboard (beta)' : 'Classic Dashboard'
}
init()
</script>
`)
await expect(page.locator("#app")).toHaveText("Classic Dashboard")
})