-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeneric.ts
More file actions
161 lines (130 loc) · 6.15 KB
/
Copy pathgeneric.ts
File metadata and controls
161 lines (130 loc) · 6.15 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { chromium, expect } from '@playwright/test'
import 'dotenv/config'
export const idfy = (text: string) => {
return text.trim().replace(/\s+/g, '-').toLowerCase()
}
// Dismiss cookie-consent banner then welcome modal.
// Cookie button targeted by id to avoid strict-mode clash with the modal's "Ok".
export async function dismissPopups(page: any) {
const cookieOk = page.locator('#cookies-accept')
if (await cookieOk.isVisible()) await cookieOk.click()
const welcomeOk = page.getByRole('button', { name: 'Ok', exact: true })
if (await welcomeOk.isVisible()) await welcomeOk.click()
}
export async function newWindow() {
const browser = await chromium.launch()
const Context = await browser.newContext()
const Page = await Context.newPage()
return Page
}
export async function login(
page: any,
{ username = 'a', password = 'a' } = {},
) {
await page.goto(`${process.env.LINK}/login`)
await expect(page.locator('#login-page')).toBeVisible()
await page.waitForTimeout(700)
await page.fill('input[name="username"]', username)
await page.fill('input[name="password"]', password)
await page.click('button[type="submit"]')
await expect(page).toHaveURL(`${process.env.LINK}/home`)
await dismissPopups(page)
}
export async function loginEnter(
page: any,
{ username = 'a', password = 'a' } = {},
) {
await page.goto(`${process.env.LINK}/login`)
await expect(page.locator('#login-page')).toBeVisible()
await page.waitForTimeout(700)
await page.fill('input[name="username"]', username)
await page.fill('input[name="password"]', password)
await page.getByLabel('Password').press('Enter')
await expect(page).toHaveURL(`${process.env.LINK}/home`)
}
// Minimal registration for other functions
export async function register(page: any) {
const randomUsername = randomString()
const randomEmail = `${randomUsername}@flowback.test`
await page.goto(`${process.env.LINK}/login`)
// Generous timeout: under heavy parallel load the frontend can be slow to render
await expect(page.locator('#login-page')).toBeVisible({ timeout: 30000 })
await page.getByRole('button', { name: 'Register' }).click()
await page.getByLabel('Email').click()
await page.getByLabel('Email').fill(randomEmail)
await page.getByLabel('Yes').check()
// Wait deterministically for the register response so we always capture the code
const registerResponsePromise = page.waitForResponse(
(response: any) => response.url().includes('register') && !response.url().includes('verify'),
)
await page.getByRole('button', { name: 'Send' }).click()
const registrationCode = (await (await registerResponsePromise).text()).slice(1, -1)
await expect(page.getByText('Email Sent')).toBeVisible()
await page.goto(`${process.env.LINK}/login/create`)
// Verify form is rendered inline (selectedPage='Verify'), no navigation needed
await expect(page.getByLabel('Verification Code')).toBeVisible({ timeout: 30000 })
await page.getByLabel('Verification Code').fill(registrationCode)
await page.getByLabel('Username').fill(randomUsername)
await page.getByLabel('Choose a Password').fill(process.env.TEST_PASS)
await page.getByRole('button', { name: 'Send' }).click()
await expect(page.getByText('Success')).toBeVisible()
await page.waitForTimeout(2000)
await expect(page).toHaveURL(`${process.env.LINK}/home`)
await dismissPopups(page)
return { username: randomUsername, email: randomEmail, password: process.env.TEST_PASS }
}
// Tests registring a user
// Only works if PUBLIC_EMAIL_REGISTRATION=FALSE in .env in the flowback-backend repository
// TODO: Automated Email testing
export async function registerTest(page: any) {
const randomUsername = randomString()
const randomEmail = `${randomUsername}@flowback.test`
await page.goto(`${process.env.LINK}/login`)
await expect(page.locator('#login-page')).toBeVisible()
await page.waitForTimeout(500)
await page.getByRole('button', { name: 'Register' }).click()
await page.getByLabel('Email').click()
await page.getByLabel('Email').fill(randomEmail)
// await page.getByLabel('Email').fill('a@a.se')
await page.getByRole('button', { name: 'Send' }).click()
await expect(page.getByText('You must accept terms of')).toBeVisible()
await page.getByLabel('Yes').check()
await page.getByRole('button', { name: 'Send' }).click()
// await expect(page.getByText('ail already exists.')).toBeVisible()
// await page.getByLabel('Email').fill(randomEmail)
let registrationCode = ''
await page.on('response', async (response: any) => {
if (response.url().includes('register') && !response.url().includes('verify')) {
registrationCode = (await response.text()).slice(1, -1)
}
})
// await page.getByRole('button', { name: 'Send' }).click()
await expect(page.getByText('Email Sent')).toBeVisible()
await page.goto(`${process.env.LINK}/login/create`)
await page.getByLabel('Verification Code').click()
await page.getByLabel('Verification Code').fill('geageageadgea')
await page.getByLabel('Username').click()
await page.getByLabel('Username').fill(randomUsername)
await page.getByLabel('Choose a Password').click()
await page.getByLabel('Choose a Password').fill(process.env.TEST_PASS)
await page.getByRole('button', { name: 'Send' }).click()
await expect(page.getByText('Wrong verification code')).toBeVisible()
await page.getByLabel('Verification Code').click()
await page.getByLabel('Verification Code').press('Control+a')
await page.getByLabel('Verification Code').fill(registrationCode.replace('"', '').replace('"', ''))
await page.getByRole('button', { name: 'Send' }).click()
await expect(page.getByText('Success')).toBeVisible()
await page.waitForTimeout(2000)
await expect(page).toHaveURL(`${process.env.LINK}/home`)
await dismissPopups(page)
return { username: randomUsername, email: randomEmail, password: process.env.TEST_PASS }
}
export async function logout(page: any) {
await page.locator("#side-header-icon").click()
await page.getByRole('button', { name: 'Log Out', exact: true }).click()
await expect(page.getByRole('img', { name: 'flowback logo' })).toBeVisible()
}
export function randomString() {
const rand = Math.random().toString(36).slice(2, 10)
return rand
}