From 33cbb8508deca60f1a5e4e206dcac15ea3890f3c Mon Sep 17 00:00:00 2001 From: Aseer KT Date: Mon, 12 Aug 2024 14:18:52 +0530 Subject: [PATCH] chore: rearrange failed e2e tests --- e2e/playwright.config.ts | 2 +- e2e/tests/auth.setup.ts | 34 ++++------------------------------ e2e/tests/chat-flow.spec.ts | 30 +++++++++++++++--------------- e2e/tests/events.ts | 34 ++++++++++++++++++++++++++++++++++ e2e/tests/helpers.ts | 6 ------ infra/ec2/init.sh | 1 + 6 files changed, 55 insertions(+), 52 deletions(-) create mode 100644 e2e/tests/events.ts diff --git a/e2e/playwright.config.ts b/e2e/playwright.config.ts index 93ef2c5..c528b77 100644 --- a/e2e/playwright.config.ts +++ b/e2e/playwright.config.ts @@ -25,7 +25,7 @@ export default defineConfig({ /* 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: process.env.BASE_URL || 'http://127.0.0.1:3000', /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ trace: 'on-first-retry', diff --git a/e2e/tests/auth.setup.ts b/e2e/tests/auth.setup.ts index b98511e..3614b1e 100644 --- a/e2e/tests/auth.setup.ts +++ b/e2e/tests/auth.setup.ts @@ -1,35 +1,9 @@ -import { expect, test as setup } from '@playwright/test' -import { authFile, user } from './helpers' +import { test as setup } from '@playwright/test' +import { signup } from './events' +import { authFile } from './helpers' setup('authenticate', async ({ page }) => { - async function checkForChatPage() { - await page.waitForURL('http://localhost:3000/chat') - - await expect( - page.locator('header').filter({ hasText: user.username }), - ).toBeVisible() - } - - // Sign up user - await page.goto('http://localhost:3000/signup') - await page.getByLabel('Username').fill(user.username) - await page.getByLabel('Password').fill(user.password) - await page.getByRole('button', { name: 'Create account' }).click() - - await expect(page.getByText(`Sign up success`)).toBeVisible() - await checkForChatPage() - - // Logout user - await page.getByLabel('logout').click() - await page.waitForURL('http://localhost:3000/login') - - // Login - await page.getByLabel('Username').fill(user.username) - await page.getByLabel('Password').fill(user.password) - await page.getByRole('button', { name: 'Continue' }).click() - - await expect(page.getByText(`Login success`)).toBeVisible() - await checkForChatPage() + await signup(page) await page.context().storageState({ path: authFile }) }) diff --git a/e2e/tests/chat-flow.spec.ts b/e2e/tests/chat-flow.spec.ts index 24cb634..9989b06 100644 --- a/e2e/tests/chat-flow.spec.ts +++ b/e2e/tests/chat-flow.spec.ts @@ -24,9 +24,19 @@ async function createGroup(page: Page) { ).toBeVisible() } +async function sendMessage(page: Page) { + const message = faker.word.words(3) + await page.getByPlaceholder('Send message...').fill(message) + await page.getByPlaceholder('Send message...').press('Enter') + + await page.waitForTimeout(2000) + + await expect(page.getByText(`${message}Today`)).toBeVisible() +} + test.describe('chat flow', () => { test('create group', async ({ page }) => { - await page.goto('http://localhost:3000/chat') + await page.goto('/chat') for (let i = 0; i < groupCount; i++) { await createGroup(page) @@ -34,27 +44,17 @@ test.describe('chat flow', () => { }) test('send message', async ({ page }) => { - await page.goto('http://localhost:3000/chat') + await page.goto('/chat') await createGroup(page) - async function sendMessage() { - const message = faker.word.words(3) - await page.getByPlaceholder('Send message...').fill(message) - await page.getByPlaceholder('Send message...').press('Enter') - - await page.waitForTimeout(2000) - - await expect(page.getByText(`${message}Today`)).toBeVisible() - } - for (let i = 0; i < messageCount; i++) { - await sendMessage() + await sendMessage(page) } }) test('join group', async ({ page }) => { - await page.goto('http://localhost:3000/chat') + await page.goto('/chat') await page.getByRole('button', { name: 'Join group' }).click() const groupLabels = await page.locator('label').all() @@ -76,7 +76,7 @@ test.describe('chat flow', () => { }) test('member list drawer', async ({ page }) => { - await page.goto('http://localhost:3000/chat') + await page.goto('/chat') const auth = await getAuthFromPageContext(page) diff --git a/e2e/tests/events.ts b/e2e/tests/events.ts new file mode 100644 index 0000000..6cf782d --- /dev/null +++ b/e2e/tests/events.ts @@ -0,0 +1,34 @@ +import { faker } from '@faker-js/faker' +import { Browser, expect, Page } from '@playwright/test' + +export const signup = async (page: Page) => { + const user = { + fullName: faker.person.fullName(), + username: faker.internet.userName(), + password: faker.internet.password(), + } + await page.goto('/auth/signup') + await page.getByLabel('Full name').fill(user.fullName) + await page.getByLabel('Username').fill(user.username) + await page.getByLabel('Password').fill(user.password) + await page.getByRole('button', { name: 'Create account' }).click() + + await expect(page.getByText(`Sign up success`)).toBeVisible() + + await page.waitForURL('/chat') + + await expect( + page.locator('header').filter({ hasText: user.username }), + ).toBeVisible() + + return { user } +} + +export const createNewUserSession = async (browser: Browser) => { + const context = await browser.newContext() + const page = await context.newPage() + + const { user } = await signup(page) + + return { page, user } +} diff --git a/e2e/tests/helpers.ts b/e2e/tests/helpers.ts index 198f5c8..aaf057c 100644 --- a/e2e/tests/helpers.ts +++ b/e2e/tests/helpers.ts @@ -1,11 +1,5 @@ -import { faker } from '@faker-js/faker' import { Page } from '@playwright/test' -export const user = { - username: faker.internet.userName(), - password: faker.internet.password(), -} - export const authFile = 'playwright/.auth/user.json' export const authTokenLsKey = 'jwt' diff --git a/infra/ec2/init.sh b/infra/ec2/init.sh index 9b859ac..76caf7a 100644 --- a/infra/ec2/init.sh +++ b/infra/ec2/init.sh @@ -51,3 +51,4 @@ cp ./Caddyfile /etc/caddy/Caddyfile ./docker-up.sh # configure env file by hand +# make pm2 available in github actions - https://stackoverflow.com/a/72030074/10240723