-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
75 lines (59 loc) · 2 KB
/
index.ts
File metadata and controls
75 lines (59 loc) · 2 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
import { test as base } from '@playwright/test';
import { PageManager } from '../pages/PageManager';
import { customerAuth } from './data/customer.data';
// 1. Type Definitions
type MenuGoFixtures = {
pm: PageManager;
customerWithCart: void;
};
export const test = base.extend<MenuGoFixtures>({
// --- Page Manager ---
pm: async ({ page }, use) => {
const pm = new PageManager(page);
await use(pm);
},
// --- Cart State Setup ---
customerWithCart: async ({ page }, use) => {
// A. Dynamic product fetch via API
const menuUrl = `https://api.menugo.demo/api/products/restaurant/${customerAuth.venueId}`;
const response = await page.request.get(menuUrl).catch(async (e) => {
console.log(`Connection failed on 1st attempt: ${e.message}. Retrying...`);
return await page.request.get(menuUrl);
});
if (!response.ok()) {
throw new Error(`Failed to fetch menu: ${response.status()} - ${response.statusText()}`);
}
const products = await response.json();
if (products.length === 0) {
throw new Error("The menu for this venue is empty! Cannot test cart.");
}
const dynamicProductId = products[0].id;
// B. Build cart payload with dynamic product
const fakeCart = {
state: {
items: [
{
product_id: dynamicProductId,
quantity: 3,
addons: [],
comment: "No onions !@#$% 123 - Automated Test"
}
]
},
version: 0
};
// C. LocalStorage Injection
await page.goto(customerAuth.loginUrl);
await page.evaluate((data) => {
localStorage.setItem('cart-storage', JSON.stringify(data));
}, fakeCart);
// D. Final navigation to cart page
await page.goto('https://app.menugo.demo/cart', {
waitUntil: 'domcontentloaded',
timeout: 60000
});
// End of setup
await use();
},
});
export { expect } from '@playwright/test';