import { test, expect, type Page } from '@playwright/test'; import https from 'https'; /* Global starting test config */ let page: Page; function httpsGet(url: string): Promise { return new Promise((resolve, reject) => { 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); }); }); } async function waitForServer(url: string, timeout: number = 150000) { const start = Date.now(); while (Date.now() - start < timeout) { try { await httpsGet(url); return true; } catch (error) { // Ignore errors and try again console.log (`Error fetching ${url}: ${error.message}`); } await new Promise((resolve) => setTimeout(resolve, 1000)); } throw new Error(`Server at ${url} did not start within ${timeout} ms`); } test.beforeAll(async ({ browser }) => { console.log('Waiting for the server to start...'); await new Promise((resolve) => setTimeout(resolve, 5000)); const baseURL = 'https://localhost:3000'; await waitForServer(baseURL); page = await browser.newPage(); test.setTimeout(150000); await page.goto(baseURL); await expect(page).toHaveTitle(/Дискурс/); await page.getByRole('link', { name: 'Войти' }).click(); console.log('Localhost server started successfully!'); await page.close(); }); test.afterAll(async () => { await page.close(); }); /* TESTS section */ /* Random Generator */ function generateRandomString(length = 10) { let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; for (let i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } test.beforeEach(async ({ page }) => { await page.goto(`/`); /* test.setTimeout(80000); */ await page.getByRole('link', { name: 'Войти' }).click(); await page.getByPlaceholder('Почта').click(); await page.getByPlaceholder('Почта').fill('guests@discours.io'); await page.getByPlaceholder('Пароль').click(); await page.getByPlaceholder('Пароль').fill('Gue$tP@ss'); await page.getByRole('button', { name: 'Войти' }).click(); }); test.describe('Topic Actions', () => { test('Follow topic', async ({ page }) => { await page.getByRole('link', { name: 'темы', exact: true }).click(); await page.getByRole('link', { name: 'Общество Статьи о политике, экономике и обществе, об актуальных событиях, людях, мнениях. Тексты про историю и современность, про то, что происходит в России и мире' }).click(); await page.getByRole('button', { name: 'Подписаться на тему' }).click(); await expect(page.getByRole('button', { name: 'Отписаться от темы' })).toBeVisible(); }); test('Unfollow topic', async ({ page }) => { await page.getByRole('link', { name: 'темы', exact: true }).click(); await page.getByRole('link', { name: 'Общество Статьи о политике, экономике и обществе, об актуальных событиях, людях, мнениях. Тексты про историю и современность, про то, что происходит в России и мире' }).click(); await page.getByRole('button', { name: 'Отписаться от темы' }).click(); await expect(page.getByRole('button', { name: 'Подписаться на тему' })).toBeVisible(); }); test('Add comment to topic', async ({ page }) => { const randomString = generateRandomString(); const currentDate = new Date(); await page.getByRole('button', { name: 'Т.Р' }).click(); await page.getByRole('link', { name: 'Профиль' }).click(); await page.getByRole('link', { name: 'Тестируем функционал' }).first().click(); await page.locator('.tiptap').click(); await page.locator('.tiptap').fill('Проверка Комментариев: ' + randomString + ' ' + currentDate); await page.getByRole('button', { name: 'Отправить' }).click(); await expect(page.getByText('Проверка Комментариев: ' + randomString + ' ' + currentDate)).toBeVisible(); }); test('Edit comment to topic', async ({ page }) => { await page.getByRole('button', { name: 'Т.Р' }).click(); await page.getByRole('link', { name: 'Комментарии' }).click(); await page.locator('[id^="comment_"]').filter({ hasText: 'Проверка Комментариев' }).first().hover(); await page.getByRole('button', { name: 'Редактировать', exact: true }).first().click(); const randomString = generateRandomString(); const currentDate = new Date(); await page.locator('.tiptap').fill('Редактируемый Комментарий: ' + randomString + ' ' + currentDate); await page.getByRole('button', { name: 'Сохранить' }).click(); await expect(page.getByText('Редактируемый Комментарий: ' + randomString + ' ' + currentDate)).toBeVisible(); }); });