webapp/tests/4-auth-author-actions.spec.ts

139 lines
5.4 KiB
TypeScript
Raw Normal View History

2024-07-03 22:33:50 +00:00
// biome-ignore lint/correctness/noNodejsModules: <explanation>
import https from 'node:https'
import { type Page, expect, test } from '@playwright/test'
2024-07-03 22:33:50 +00:00
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
let context: any
let page: Page
/* Global starting test config */
function httpsGet(url: string): Promise<void> {
return new Promise((resolve, reject) => {
2024-07-03 22:33:50 +00:00
https
.get(
url,
{
rejectUnauthorized: false // Ignore SSL certificate errors
},
(res) => {
if (res.statusCode === 200) {
resolve()
} else {
reject(new Error(`Request failed with status code ${res.statusCode}`))
}
}
)
.on('error', (error) => {
reject(error)
})
})
}
2024-07-03 22:33:50 +00:00
async function waitForServer(url: string, timeout = 150000) {
const start = Date.now()
while (Date.now() - start < timeout) {
try {
2024-07-03 22:33:50 +00:00
await httpsGet(url)
return true
} catch (error) {
// Ignore errors and try again
2024-07-03 22:33:50 +00:00
console.log(`Error fetching ${url}: ${error}`)
}
2024-07-03 22:33:50 +00:00
await new Promise((resolve) => setTimeout(resolve, 1000))
}
2024-07-03 22:33:50 +00:00
throw new Error(`Server at ${url} did not start within ${timeout} ms`)
}
2024-07-30 21:52:25 +00:00
test.beforeAll(async ({ browser }) => {
2024-07-03 22:33:50 +00:00
console.log('Waiting for the server to start...')
await new Promise((resolve) => setTimeout(resolve, 5000))
const baseURL = process.env.BASE_URL || 'https://localhost:3000'
console.log('Base URL:', baseURL)
2024-07-03 22:33:50 +00:00
await waitForServer(baseURL)
context = await browser.newContext()
page = await context.newPage()
test.setTimeout(150000)
await page.goto(baseURL)
2024-09-15 18:43:35 +00:00
// biome-ignore lint/performance/useTopLevelRegex: <explanation>
2024-07-03 22:33:50 +00:00
await expect(page).toHaveTitle(/Дискурс/)
await page.getByRole('link', { name: 'Войти' }).click()
console.log('Localhost server started successfully!')
await page.close()
})
/* TESTS section */
/* Random Generator */
function generateRandomString(length = 10) {
2024-07-03 22:33:50 +00:00
let result = ''
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length
for (let i = 0; i < length; i++) {
2024-07-03 22:33:50 +00:00
result += characters.charAt(Math.floor(Math.random() * charactersLength))
}
2024-07-03 22:33:50 +00:00
return result
}
2024-07-30 21:52:25 +00:00
const randomstring = generateRandomString(4)
test('Sign up', async ({ page }) => {
await page.goto('/')
/* test.setTimeout(80000); */
await page.getByRole('link', { name: 'Войти' }).click()
2024-07-30 22:39:24 +00:00
await page.getByRole('link', { name: 'У меня еще нет аккаунта' }).click()
2024-07-30 21:52:25 +00:00
await page.getByPlaceholder('Имя и фамилия').click()
await page.getByPlaceholder('Имя и фамилия').fill('Тестируем Разработку')
await page.getByPlaceholder('Почта').click()
await page.getByPlaceholder('Почта').fill(`guests+${randomstring}@discours.io`)
await page.getByPlaceholder('Пароль').click()
await page.getByPlaceholder('Пароль').fill('Gue$tP@ss')
await page.getByRole('button', { name: 'Присоединиться' }).click()
})
2024-07-03 22:33:50 +00:00
test.beforeEach(async ({ page }) => {
await page.goto('/')
/* test.setTimeout(80000); */
2024-07-03 22:33:50 +00:00
await page.getByRole('link', { name: 'Войти' }).click()
await page.getByPlaceholder('Почта').click()
2024-07-30 21:52:25 +00:00
await page.getByPlaceholder('Почта').fill(`guests+${randomstring}@discours.io`)
2024-07-03 22:33:50 +00:00
await page.getByPlaceholder('Пароль').click()
await page.getByPlaceholder('Пароль').fill('Gue$tP@ss')
await page.getByRole('button', { name: 'Войти' }).click()
})
2024-07-30 21:52:25 +00:00
test.describe('Author Actions', () => {
test('Author sandwitch menu', async ({ page }) => {
await page.getByRole('button', { name: 'Т.Р.' }).click()
2024-07-03 22:33:50 +00:00
await expect(page.getByRole('link', { name: 'Профиль' })).toBeVisible()
2024-07-30 21:52:25 +00:00
await page.getByRole('button', { name: 'Т.Р.' }).click()
2024-07-03 22:33:50 +00:00
})
2024-07-30 21:52:25 +00:00
test('Follow author', async ({ page }) => {
2024-07-03 22:33:50 +00:00
await page.getByRole('link', { name: 'авторы', exact: true }).click()
await page.getByRole('link', { name: 'Дискурс На сайте c 16 июня' }).click()
await page.getByRole('button', { name: 'Подписаться' }).click()
await expect(page.getByRole('main').getByRole('button', { name: 'Вы подписаны' })).toBeVisible()
})
2024-07-30 21:52:25 +00:00
test('Unfollow author', async ({ page }) => {
2024-07-03 22:33:50 +00:00
await page.getByRole('link', { name: 'авторы', exact: true }).click()
await page.getByRole('link', { name: 'Дискурс На сайте c 16 июня' }).click()
await page.getByRole('button', { name: 'Вы подписаны' }).click()
await expect(page.getByRole('main').getByRole('button', { name: 'Подписаться' })).toBeVisible()
})
2024-07-30 21:52:25 +00:00
test('Change author profile', async ({ page }) => {
await page.getByRole('button', { name: 'Т.Р.' }).click()
2024-07-03 22:33:50 +00:00
await page.getByRole('link', { name: 'Профиль' }).click()
await page.getByRole('button', { name: 'Редактировать профиль' }).click()
await page.locator('.tiptap').click()
const randomString = generateRandomString()
const currentDate = new Date()
await page.locator('.tiptap').fill(`test: ${randomString} ${currentDate}`)
try {
2024-07-03 22:33:50 +00:00
const button = await page.getByRole('button', { name: 'Сохранить настройки' })
await button.click()
} catch (error) {
2024-07-03 22:33:50 +00:00
console.warn('Button has disappeared', error)
}
2024-07-03 22:33:50 +00:00
await expect(page.getByText(`test: ${randomString} ${currentDate}`)).toBeVisible()
})
})