-
Notifications
You must be signed in to change notification settings - Fork 145
e2e: Improve setup and utils #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { test as setup } from 'e2e/utils'; | ||
| import { | ||
| ADMIN_EMAIL, | ||
| ADMIN_FILE, | ||
| USER_EMAIL, | ||
| USER_FILE, | ||
| } from 'e2e/utils/constants'; | ||
|
|
||
| /** | ||
| * @see https://playwright.dev/docs/auth#multiple-signed-in-roles | ||
| */ | ||
|
|
||
| setup('authenticate as admin', async ({ page }) => { | ||
| await page.to('/login'); | ||
| await page.login({ email: ADMIN_EMAIL }); | ||
|
|
||
| await page.waitForURL('/manager'); | ||
| await expect(page.getByTestId('layout-manager')).toBeVisible(); | ||
|
|
||
| await page.context().storageState({ path: ADMIN_FILE }); | ||
| }); | ||
|
|
||
| setup('authenticate as user', async ({ page }) => { | ||
| await page.to('/login'); | ||
| await page.login({ email: USER_EMAIL }); | ||
|
|
||
| await page.waitForURL('/app'); | ||
| await expect(page.getByTestId('layout-app')).toBeVisible(); | ||
|
|
||
| await page.context().storageState({ path: USER_FILE }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { expect, test } from 'e2e/utils'; | ||
| import { randomString } from 'remeda'; | ||
|
|
||
| test.describe('User management as user', () => { | ||
| test.use({ storageState: 'e2e/.auth/user.json' }); | ||
| test('Should not have access', async ({ page }) => { | ||
| await page.to('/manager/users'); | ||
|
|
||
| await expect( | ||
| page.getByText("You don't have access to this page") | ||
| ).toBeVisible(); | ||
| }); | ||
| }); | ||
|
|
||
| test.describe('User management as manager', () => { | ||
| test.use({ | ||
| storageState: 'e2e/.auth/admin.json', | ||
| }); | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| await page.to('/manager/users'); | ||
| }); | ||
|
|
||
| test('Create a user', async ({ page }) => { | ||
| await page.getByText('New User').click(); | ||
|
|
||
| const randomId = randomString(8); | ||
| const uniqueEmail = `new-user-${randomId}@user.com`; | ||
|
|
||
| // Fill the form | ||
| await page.waitForURL('/manager/users/new'); | ||
| await page.getByLabel('Name').fill('New user'); | ||
| await page.getByLabel('Email').fill(uniqueEmail); | ||
| await page.getByText('Create').click(); | ||
|
|
||
| await page.waitForURL('/manager/users'); | ||
| await page.getByPlaceholder('Search...').fill('new-user'); | ||
| await expect(page.getByText(uniqueEmail)).toBeVisible(); | ||
| }); | ||
|
|
||
| test('Edit a user', async ({ page }) => { | ||
| await page.getByText('[email protected]').click({ | ||
| force: true, | ||
| }); | ||
|
|
||
| await page.getByRole('link', { name: 'Edit user' }).click(); | ||
|
|
||
| const randomId = randomString(8); | ||
| const newAdminName = `Admin ${randomId}`; | ||
| await page.getByLabel('Name').fill(newAdminName); | ||
| await page.getByText('Save').click(); | ||
|
|
||
| await expect(page.getByText(newAdminName).first()).toBeVisible(); | ||
| }); | ||
|
|
||
| test('Delete a user', async ({ page }) => { | ||
| await page | ||
| .getByText('user', { | ||
| exact: true, | ||
| }) | ||
| .first() | ||
| .click({ force: true }); | ||
|
|
||
| await page.getByRole('button', { name: 'Delete' }).click(); | ||
|
|
||
| await expect( | ||
| page.getByText('You are about to permanently delete this user.') | ||
| ).toBeVisible(); | ||
|
|
||
| await page.getByRole('button', { name: 'Delete' }).click(); | ||
|
|
||
| await expect(page.getByText('User deleted')).toBeVisible(); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| const AUTH_FILE_BASE = 'e2e/.auth'; | ||
|
|
||
| export const USER_FILE = `${AUTH_FILE_BASE}/user.json`; | ||
| export const USER_EMAIL = '[email protected]'; | ||
|
|
||
| export const ADMIN_FILE = `${AUTH_FILE_BASE}/admin.json`; | ||
| export const ADMIN_EMAIL = '[email protected]'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { test as base } from '@playwright/test'; | ||
| import { ExtendedPage, pageWithUtils } from 'e2e/utils/page'; | ||
|
|
||
| const test = base.extend<ExtendedPage>({ | ||
| page: pageWithUtils, | ||
| }); | ||
|
|
||
| export * from '@playwright/test'; | ||
| export { test }; |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { expect, Page } from '@playwright/test'; | ||
| import { CustomFixture } from 'e2e/utils/types'; | ||
|
|
||
| import { DEFAULT_LANGUAGE_KEY } from '@/lib/i18n/constants'; | ||
|
|
||
| import { | ||
| AUTH_EMAIL_OTP_MOCKED, | ||
| AUTH_SIGNUP_ENABLED, | ||
| } from '@/features/auth/config'; | ||
| import locales from '@/locales'; | ||
| import { FileRouteTypes } from '@/routeTree.gen'; | ||
|
|
||
| interface PageUtils { | ||
| /** | ||
| * Utility used to authenticate a user on the app | ||
| */ | ||
| login: (input: { email: string; code?: string }) => Promise<void>; | ||
|
|
||
| /** | ||
| * Override of the `page.goto` method with typed routes from the app | ||
| */ | ||
| to: ( | ||
| url: FileRouteTypes['to'], | ||
| options?: Parameters<Page['goto']>[1] | ||
| ) => ReturnType<Page['goto']>; | ||
| } | ||
|
|
||
| export type ExtendedPage = { page: PageUtils }; | ||
|
|
||
| export const pageWithUtils: CustomFixture<Page & PageUtils> = async ( | ||
| { page }, | ||
| apply | ||
| ) => { | ||
| page.login = async function login(input: { email: string; code?: string }) { | ||
| const routeLogin = '/login' satisfies FileRouteTypes['to']; | ||
| const routeLoginVerify = '/login/verify' satisfies FileRouteTypes['to']; | ||
| await page.waitForURL(`**${routeLogin}**`); | ||
|
|
||
| await expect( | ||
| page.getByText( | ||
| locales[DEFAULT_LANGUAGE_KEY].auth.pageLoginWithSignUp.title | ||
| ) | ||
| ).toBeVisible(); | ||
|
|
||
| await page | ||
| .getByPlaceholder(locales[DEFAULT_LANGUAGE_KEY].auth.common.email.label) | ||
| .fill(input.email); | ||
|
|
||
| await page | ||
| .getByRole('button', { | ||
| name: locales[DEFAULT_LANGUAGE_KEY].auth[ | ||
| AUTH_SIGNUP_ENABLED ? 'pageLoginWithSignUp' : 'pageLogin' | ||
| ].loginWithEmail, | ||
| }) | ||
| .click(); | ||
|
|
||
| await page.waitForURL(`**${routeLoginVerify}**`); | ||
| await page | ||
| .getByText(locales[DEFAULT_LANGUAGE_KEY].auth.common.otp.label) | ||
| .fill(input.code ?? AUTH_EMAIL_OTP_MOCKED); | ||
| }; | ||
|
|
||
| page.to = page.goto; | ||
|
|
||
| apply(page); | ||
| }; | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { | ||
| PlaywrightTestArgs, | ||
| PlaywrightTestOptions, | ||
| TestFixture, | ||
| } from '@playwright/test'; | ||
| import { ExtendedPage } from 'e2e/utils/page'; | ||
|
|
||
| export type CustomFixture<TReturn> = TestFixture< | ||
| TReturn, | ||
| PlaywrightTestArgs & PlaywrightTestOptions & ExtendedPage | ||
| >; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,13 +30,15 @@ | |
| "test": "vitest --browser.headless", | ||
| "test:ci": "vitest run", | ||
| "test:ui": "vitest", | ||
| "e2e:setup": "dotenv -- cross-env playwright test --project=setup", | ||
| "e2e": "dotenv -- cross-env playwright test", | ||
| "e2e:ui": "dotenv -- cross-env playwright test --ui", | ||
| "dk:init": "docker compose up -d", | ||
| "dk:start": "docker compose start", | ||
| "dk:stop": "docker compose stop", | ||
| "dk:clear": "docker compose down --volumes", | ||
| "db:init": "pnpm db:push && pnpm db:seed", | ||
| "db:reset": "pnpm dk:clear && pnpm dk:init && pnpm db:init", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that db:init can fail here if db is not ready in the docker after dk:init 😕
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was more of a quick workaround to get a "Clean db state" between e2e test runs when running them in local ! We should discuss what's the best path forward to have consistent e2e test runs |
||
| "db:push": "prisma db push", | ||
| "db:ui": "prisma studio", | ||
| "db:seed": "dotenv -- cross-env node ./run-jiti ./prisma/seed/index.ts" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.