From 94520bc57b2676d62f84cb858acdfaef2c0b72cc Mon Sep 17 00:00:00 2001 From: Stepan Vladovskiy Date: Wed, 19 Jun 2024 01:06:34 +0000 Subject: [PATCH] feat: Playwright now can start all from comand line, tests are placed in separate files for use workers, webServer is starts before all workers --- playwright.config.ts | 35 +++--- tests/auth-drafts-actions.spec.ts | 128 +++++++++++++++++++++ tests/auth-topics-actions.spec.ts | 115 +++++++++++++++++++ tests/auth-user-actions.spec.ts | 111 ++++++++++++++++++ tests/auth-user-test.spec.ts | 185 ------------------------------ tests/basic-routes.spec.ts | 21 ---- tests/basic-routes.test.js | 37 ------ tests/page-sections.spec.ts | 71 ++++++++++++ 8 files changed, 442 insertions(+), 261 deletions(-) create mode 100644 tests/auth-drafts-actions.spec.ts create mode 100644 tests/auth-topics-actions.spec.ts create mode 100644 tests/auth-user-actions.spec.ts delete mode 100644 tests/auth-user-test.spec.ts delete mode 100644 tests/basic-routes.spec.ts delete mode 100644 tests/basic-routes.test.js create mode 100644 tests/page-sections.spec.ts diff --git a/playwright.config.ts b/playwright.config.ts index 300d6fec..dacddda9 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -10,9 +10,10 @@ import { defineConfig, devices } from '@playwright/test' * See https://playwright.dev/docs/test-configuration. */ export default defineConfig({ + /* Directory to search for tests */ testDir: './tests', /* Run tests in files in parallel */ - fullyParallel: true, + fullyParallel: false, /* Fail the build on CI if you accidentally left test.only in the source code. */ forbidOnly: !!process.env.CI, /* Retry on CI only */ @@ -20,27 +21,23 @@ export default defineConfig({ /* Opt out of parallel tests on CI. */ workers: process.env.CI ? 1 : undefined, /* Reporter to use. See https://playwright.dev/docs/test-reporters */ - reporter: 'html', + reporter: 'list', + /* Timeout for each test */ + timeout: 40000, /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { /* Base URL to use in actions like `await page.goto('/')`. */ - // baseURL: 'http://127.0.0.1:3000', - + baseURL: 'https://localhost:3000', + /* Headless */ + headless: true, + /* Ignode SSL certificates */ + ignoreHTTPSErrors: true, /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: 'on-first-retry', }, /* Configure projects for major browsers */ projects: [ - { - name: 'chromium', - use: { ...devices['Desktop Chrome'] }, - }, - - { - name: 'firefox', - use: { ...devices['Desktop Firefox'] }, - }, { name: 'webkit', @@ -69,9 +66,11 @@ export default defineConfig({ ], /* Run local dev server before starting the tests */ - //webServer: { - // command: 'npm run dev', - // url: 'https://localhost:3000', - // reuseExistingServer: !process.env.CI, - //}, + webServer: { + command: 'npm run dev', + url: 'https://localhost:3000', + ignoreHTTPSErrors: true, + reuseExistingServer: !process.env.CI, + timeout: 5 * 60 * 1000, + }, }) diff --git a/tests/auth-drafts-actions.spec.ts b/tests/auth-drafts-actions.spec.ts new file mode 100644 index 00000000..f060d748 --- /dev/null +++ b/tests/auth-drafts-actions.spec.ts @@ -0,0 +1,128 @@ +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(/Дискурс/); + console.log('Localhost server started successfully!'); + await page.close(); + }); +test.afterAll(async () => { + await page.close(); + }); + + +/* TESTS section */ + +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('*****Undone***** Drafts - article', () => { + test('Open /create', async ({ page }) => { + await page.goto(`/create`); + await expect(page).toHaveTitle('Выберите тип публикации'); + }); +}); + +/* test('Create article', async ({ page }) => { + await page.goto(`/create`); + await page.locator('li').filter({ hasText: 'статья' }).locator('img').click(); +}); + +test('Check Draft', async ({ page }) => { + +}); */ + +/* test('Drafts - create literature', async ({ page }) => { + await page.getByRole('button', { name: 'Т.Р' }).click(); + await page.getByRole('link', { name: 'Черновики' }).click(); + await page.getByRole('link', { name: 'Создать публикацию' }).click(); + await page.locator('li').filter({ hasText: /^литература$/ }).locator('img').click(); + Fill the form + Save + Check is it created +}); */ + +/* test('Drafts - create images', async ({ page }) => { + await page.getByRole('button', { name: 'Т.Р' }).click(); + await page.getByRole('link', { name: 'Черновики' }).click(); + await page.getByRole('link', { name: 'Создать публикацию' }).click(); + await page.locator('li').filter({ hasText: 'изображения' }).locator('img').click(); + Fill the form + Save + Check is it created +}); */ + +/* test('Drafts - create music', async ({ page }) => { + await page.getByRole('button', { name: 'Т.Р' }).click(); + await page.getByRole('link', { name: 'Черновики' }).click(); + await page.getByRole('link', { name: 'Создать публикацию' }).click(); + await page.locator('li').filter({ hasText: 'музыка' }).locator('img').click(); + Fill the form + Save + Check is it created +}); */ + +/* test('Drafts - create video', async ({ page }) => { + await page.getByRole('button', { name: 'Т.Р' }).click(); + await page.getByRole('link', { name: 'Черновики' }).click(); + await page.getByRole('link', { name: 'Создать публикацию' }).click(); + await page.locator('li').filter({ hasText: 'видео' }).locator('img').click(); + Fill the form + Save + Check is it created +}); */ + + +/* test('Post topic', async ({ page }) => { + Open Draft + Post +});*/ \ No newline at end of file diff --git a/tests/auth-topics-actions.spec.ts b/tests/auth-topics-actions.spec.ts new file mode 100644 index 00000000..4e4ad070 --- /dev/null +++ b/tests/auth-topics-actions.spec.ts @@ -0,0 +1,115 @@ +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(); + }); +}); \ No newline at end of file diff --git a/tests/auth-user-actions.spec.ts b/tests/auth-user-actions.spec.ts new file mode 100644 index 00000000..cf2b157b --- /dev/null +++ b/tests/auth-user-actions.spec.ts @@ -0,0 +1,111 @@ +import { test, expect, type Page } from '@playwright/test'; +import https from 'https'; + +let context: any; +let page: Page; + +/* Global starting test config */ + +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); + context = await browser.newContext(); + page = await context.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(); + }); + +/* 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('User Actions', () => { + test('User sandwitch menu', async ({page}) => { + await page.getByRole('button', { name: 'Т.Р' }).click(); + await expect(page.getByRole('link', { name: 'Профиль' })).toBeVisible(); + await page.getByRole('button', { name: 'Т.Р' }).click(); + }); + test('Follow user', async ({page}) => { + 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(); + }); + test('Unfollow user', async ({page}) => { + 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(); + }); + test('Change user data', async ({page}) => { + await page.getByRole('button', { name: 'Т.Р' }).click(); + 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 { + const button = await page.getByRole('button', { name: 'Сохранить настройки' }); + await button.click(); + } catch (error) { + console.log('Button has disappeared'); + } + await expect(page.getByText('test: ' + randomString + ' ' + currentDate)).toBeVisible(); + }); +}); \ No newline at end of file diff --git a/tests/auth-user-test.spec.ts b/tests/auth-user-test.spec.ts deleted file mode 100644 index ceaa3dd1..00000000 --- a/tests/auth-user-test.spec.ts +++ /dev/null @@ -1,185 +0,0 @@ -import { test, expect } from '@playwright/test'; - -const baseHost = process.env.BASE_HOST || 'https://localhost:3000' - -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; -} - -async function login(page) { - 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(); -} - -/* Done */ -test('Open Page', async ({ page }) => { - await page.goto(`${baseHost}/`); - await expect(page.getByRole('link', { name: 'Дискурс', exact: true })).toBeVisible(); -}); - -/* Done */ -test('Login', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page); - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); -}); - -test('Drafts - create article', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page); - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); - await page.getByRole('button', { name: 'Т.Р' }).click(); - await page.getByRole('link', { name: 'Черновики' }).click(); - await page.getByRole('link', { name: 'Создать публикацию' }).click(); - await page.locator('li').filter({ hasText: 'статья' }).locator('img').click(); - /* Fill the form */ - /* Save */ - /* Check is it created */ -}); - -test('Drafts - create literature', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page); - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); - await page.getByRole('button', { name: 'Т.Р' }).click(); - await page.getByRole('link', { name: 'Черновики' }).click(); - await page.getByRole('link', { name: 'Создать публикацию' }).click(); - await page.locator('li').filter({ hasText: /^литература$/ }).locator('img').click(); - /* Fill the form */ - /* Save */ - /* Check is it created */ -}); - -test('Drafts - create images', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page);; - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); - await page.getByRole('button', { name: 'Т.Р' }).click(); - await page.getByRole('link', { name: 'Черновики' }).click(); - await page.getByRole('link', { name: 'Создать публикацию' }).click(); - await page.locator('li').filter({ hasText: 'изображения' }).locator('img').click(); - /* Fill the form */ - /* Save */ - /* Check is it created */ -}); - -test('Drafts - create music', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page); - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); - await page.getByRole('button', { name: 'Т.Р' }).click(); - await page.getByRole('link', { name: 'Черновики' }).click(); - await page.getByRole('link', { name: 'Создать публикацию' }).click(); - await page.locator('li').filter({ hasText: 'музыка' }).locator('img').click(); - /* Fill the form */ - /* Save */ - /* Check is it created */ -}); - -test('Drafts - create video', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page); - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); - await page.getByRole('button', { name: 'Т.Р' }).click(); - await page.getByRole('link', { name: 'Черновики' }).click(); - await page.getByRole('link', { name: 'Создать публикацию' }).click(); - await page.locator('li').filter({ hasText: 'видео' }).locator('img').click(); - /* Fill the form */ - /* Save */ - /* Check is it created */ -}); - - -test('Post topic', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page); - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); - /* Open Draft */ - /* Post */ -}); - -test('Subscribe to user', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page); - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); - await page.goto(`${baseHost}/author/discours`); - await page.getByRole('main').getByRole('button', { name: 'Подписаться' }).click(); - /* Check is it subscribed */ -}); - -/* Done */ -test('Subscribe to topic', async ({ page }) => { - await page.goto(`${baseHost}/topic/society`); - await page.getByRole('button', { 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(); - await expect(page.getByRole('button', { name: 'Подписаться на тему' })).toBeVisible(); - await page.getByRole('button', { name: 'Подписаться на тему' }).click(); -}); - -/* Done */ -test('Un - Subscribe to topic', async ({ page }) => { - await page.goto(`${baseHost}/topic/society`); - await login(page); - await expect(page.getByRole('button', { name: 'Отписаться от темы' })).toBeVisible(); - await page.getByRole('button', { name: 'Отписаться от темы' }).click(); -}); - -/* Done */ -test('Change user data', async ({ page }) => { - await page.goto(`${baseHost}/`); - await login(page); - await expect(page.locator('div:nth-child(4) > a').first()).toBeVisible(); - await page.getByRole('button', { name: 'Т.Р' }).click(); - 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 { - const button = await page.getByRole('button', { name: 'Сохранить настройки' }); - await button.click(); - } catch (error) { - console.log('Button has disappeared'); - } - await expect(page.getByText('test: ' + randomString + ' ' + currentDate)).toBeVisible(); -}); - -/* Done */ -test('Add comment to topic', async ({ page }) => { - await page.goto(`${baseHost}/v-peschere-u-tsiklopa/`); - await login(page); - const randomString = generateRandomString(); - const currentDate = new Date(); - 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(); -}); - -/* Done */ -test('Edit comment to topic', async ({ page }) => { - await page.goto(`${baseHost}/author/testdev/comments`); - await login(page); - const firstCommentEditButton = await page.locator('._32U0J.WXcGK').first(); - await firstCommentEditButton.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(); -}); \ No newline at end of file diff --git a/tests/basic-routes.spec.ts b/tests/basic-routes.spec.ts deleted file mode 100644 index b904fbef..00000000 --- a/tests/basic-routes.spec.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { expect, test } from '@playwright/test' - -const baseHost = process.env.BASE_HOST || 'https://localhost:3000' - -const pagesTitles = { - '/': /Дискурс/, - '/feed': /Лента/, - '/create': /Выберите тип публикации/, - '/about/help': /Поддержите Дискурс/, - '/authors': /Авторы/, - '/topics': /Темы и сюжеты/, - '/inbox': /Входящие/, -} - -Object.keys(pagesTitles).forEach((res: string) => { - test(`страница ${res}`, async ({ page }) => { - await page.goto(`${baseHost}${res}`) - const title = pagesTitles[res] - await expect(page).toHaveTitle(title) - }) -}) diff --git a/tests/basic-routes.test.js b/tests/basic-routes.test.js deleted file mode 100644 index a3ffcf78..00000000 --- a/tests/basic-routes.test.js +++ /dev/null @@ -1,37 +0,0 @@ -import { chromium } from 'playwright' -;(async () => { - const browser = await chromium.launch() - const context = await browser.newContext() - - // Define the URLs to visit - const pagesToVisit = [ - 'https://localhost:3000/', - 'https://localhost:3000/feed', - 'https://localhost:3000/create', - 'https://localhost:3000/about/donate', - 'https://localhost:3000/authors', - 'https://localhost:3000/topics', - 'https://localhost:3000/inbox', - ] - - // Loop through the pages and visit each one - for (const pageUrl of pagesToVisit) { - const page = await context.newPage() - - // Log a message before visiting the page - console.log(`Visiting page: ${pageUrl}`) - - await page.goto(pageUrl) - - // Add your test assertions here if needed - - // Log a message after visiting the page - console.log(`Finished visiting page: ${pageUrl}`) - - // Close the page before moving to the next one - await page.close() - } - - // Close the browser - await browser.close() -})() diff --git a/tests/page-sections.spec.ts b/tests/page-sections.spec.ts new file mode 100644 index 00000000..7ee6aeb6 --- /dev/null +++ b/tests/page-sections.spec.ts @@ -0,0 +1,71 @@ +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(/Дискурс/); + console.log('Localhost server started successfully!'); + }); +test.afterAll(async () => { + await page.close(); + }); + + +/* TESTS section */ + +const pagesTitles = { + '/': /Дискурс/, + '/feed': /Лента/, + '/about/help': /Поддержите Дискурс/, + '/authors': /Авторы/, + '/topics': /Темы и сюжеты/, + '/inbox': /Входящие/, +} +test.describe('Pages open', () => { + Object.keys(pagesTitles).forEach((res: string) => { + test(`Open Page ${res}`, async ({ page }) => { + await page.goto(`${res}`) + const title = pagesTitles[res] + await expect(page).toHaveTitle(title) + }) + }) +}); \ No newline at end of file