Skip to content

Commit

Permalink
chore: rearrange failed e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Aug 12, 2024
1 parent b249b03 commit 33cbb85
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 52 deletions.
2 changes: 1 addition & 1 deletion e2e/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
34 changes: 4 additions & 30 deletions e2e/tests/auth.setup.ts
Original file line number Diff line number Diff line change
@@ -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 })
})
30 changes: 15 additions & 15 deletions e2e/tests/chat-flow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,37 @@ 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)
}
})

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()
Expand 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)

Expand Down
34 changes: 34 additions & 0 deletions e2e/tests/events.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
6 changes: 0 additions & 6 deletions e2e/tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
1 change: 1 addition & 0 deletions infra/ec2/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 33cbb85

Please sign in to comment.