-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathhelpers.ts
More file actions
78 lines (65 loc) · 2.2 KB
/
helpers.ts
File metadata and controls
78 lines (65 loc) · 2.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
76
77
import { expect, type Locator, type Page } from '@playwright/test';
const cookieButtonNames = [
/accept all/i,
/accept/i,
/i understand/i,
/got it/i,
/allow all/i,
];
const tryClick = async (locator: Locator): Promise<boolean> => {
try {
if ((await locator.count()) === 0) {
return false;
}
await locator.first().click({ timeout: 2000 });
return true;
} catch {
return false;
}
};
export const dismissCookieBannerIfPresent = async (
page: Page,
): Promise<void> => {
for (const name of cookieButtonNames) {
const clicked = await tryClick(page.getByRole('button', { name }));
if (clicked) {
return;
}
}
};
export const expectFeedToBeVisible = async (page: Page): Promise<void> => {
await expect(page.getByTestId('posts-feed')).toBeVisible();
};
export const gotoAndExpectOk = async (
page: Page,
path: string,
): Promise<void> => {
const response = await page.goto(path, { waitUntil: 'domcontentloaded' });
expect(response, `No response while navigating to ${path}`).not.toBeNull();
expect(response?.ok(), `Navigation to ${path} returned ${response?.status()}`).toBeTruthy();
};
export const expectAppShellToBeVisible = async (page: Page): Promise<void> => {
const nextData = page.locator('script#__NEXT_DATA__');
await expect(nextData).toHaveCount(1);
const payload = await nextData.textContent();
expect(payload, 'Missing __NEXT_DATA__ payload').toBeTruthy();
expect(payload).toContain('"page"');
};
export const login = async (page: Page): Promise<void> => {
if (!process.env.USER_NAME || !process.env.PASSWORD) {
throw new Error('Missing USER_NAME/PASSWORD for authenticated tests');
}
await page.goto('/');
await dismissCookieBannerIfPresent(page);
await page.getByRole('button', { name: /^log in$/i }).first().click();
await page.getByRole('textbox', { name: /email/i }).fill(process.env.USER_NAME);
await page
.getByRole('textbox', { name: /password/i })
.fill(process.env.PASSWORD);
await page.getByRole('button', { name: /^log in$/i }).first().click();
await expect(
page
.getByRole('link', { name: /profile/i })
.or(page.getByRole('button', { name: /profile settings/i })),
).toBeVisible();
};