-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(e2e): setup playwright, add tests for login scenario
- Loading branch information
Showing
8 changed files
with
300 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { defineConfig, devices } from '@playwright/test'; | ||
|
||
const PORT = process.env.PORT || '3000'; | ||
|
||
export default defineConfig({ | ||
testDir: './tests/playwright', | ||
fullyParallel: true, | ||
forbidOnly: !!process.env.CI, | ||
retries: process.env.CI ? 2 : 0, | ||
workers: process.env.CI ? 1 : undefined, | ||
reporter: !!process.env.CI ? 'github' : 'html', | ||
use: { | ||
baseURL: `http://localhost:${PORT}/`, | ||
trace: 'on-first-retry', | ||
}, | ||
|
||
projects: [ | ||
{ name: 'setup', testMatch: /\/tests\/playwright\/.*\.setup\.ts/ }, | ||
|
||
{ | ||
name: 'Authenticated user', | ||
use: { | ||
...devices['Desktop Chrome'], | ||
storageState: 'tests/playwright/.auth/user.json', | ||
}, | ||
testIgnore: /\/tests\/playwright\/.*\.login\.spec\.ts/, | ||
dependencies: ['setup'], | ||
}, | ||
|
||
{ | ||
name: 'Unauthenticated user', | ||
use: { | ||
...devices['Desktop Chrome'], | ||
}, | ||
testMatch: /\/tests\/playwright\/.*\.login\.spec\.ts/, | ||
}, | ||
], | ||
|
||
timeout: 15 * 1000, | ||
expect: { | ||
timeout: 5 * 1000, | ||
}, | ||
|
||
webServer: { | ||
// @FIXME: Optimize webserver command | ||
//command: 'npm run test:e2e:webserver', | ||
command: 'npm run dev', | ||
port: Number(PORT), | ||
reuseExistingServer: !process.env.CI, | ||
stdout: 'pipe', | ||
stderr: 'pipe', | ||
env: { | ||
PORT, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { test as setup } from '@playwright/test'; | ||
import { loginToApplication } from './auth.utils'; | ||
|
||
const authFile = 'tests/playwright/.auth/user.json'; | ||
|
||
setup('authenticate', async ({ page }) => { | ||
await loginToApplication(page); | ||
|
||
await page.context().storageState({ path: authFile }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import 'dotenv/config'; | ||
|
||
import { type Page, expect, test } from '@playwright/test'; | ||
import { ROUTE } from '@/routes'; | ||
|
||
export type Credentials = { | ||
username: string; | ||
password: string; | ||
}; | ||
|
||
type CredentialsAssertion = [string[], Credentials]; | ||
type EmptyCredentialsAssertion = [string[], Partial<Credentials>]; | ||
|
||
const getCredentials = (): Credentials => { | ||
const username = process.env.E2E_TEST_USERNAME; | ||
const password = process.env.E2E_TEST_PASSWORD; | ||
|
||
if (!username || !password) { | ||
throw new Error( | ||
'Please set `E2E_TEST_USERNAME` and `E2E_TEST_PASSWORD` environment variables to authenticate.' | ||
); | ||
} | ||
|
||
return { username, password }; | ||
}; | ||
|
||
export async function loginToApplication(page: Page) { | ||
test.setTimeout(120000); | ||
const { username, password } = getCredentials(); | ||
|
||
await page.goto(ROUTE.LOGIN); | ||
await page.getByLabel('Username').fill(username); | ||
await page.getByLabel('Password').fill(password); | ||
await page.getByRole('button', { name: 'Log in' }).click(); | ||
|
||
await page.waitForURL(ROUTE.SERVER_LIST); | ||
|
||
await expect( | ||
page.getByRole('heading', { name: 'Server list' }) | ||
).toBeVisible(); | ||
} | ||
|
||
export const INCORRECT_CREDENTIALS = ((): CredentialsAssertion[] => { | ||
const { username, password } = getCredentials(); | ||
|
||
return [ | ||
[ | ||
['password'], | ||
{ | ||
username, | ||
password: 'incorrect', | ||
}, | ||
], | ||
[ | ||
['username'], | ||
{ | ||
username: 'incorrect', | ||
password, | ||
}, | ||
], | ||
[ | ||
['username', 'password'], | ||
{ | ||
username: 'incorrect', | ||
password: 'incorrect', | ||
}, | ||
], | ||
]; | ||
})(); | ||
|
||
export const EMPTY_CREDENTIALS = ((): EmptyCredentialsAssertion[] => { | ||
const { username, password } = getCredentials(); | ||
|
||
return [ | ||
[ | ||
['password'], | ||
{ | ||
username, | ||
}, | ||
], | ||
[ | ||
['username'], | ||
{ | ||
password, | ||
}, | ||
], | ||
[['username', 'password'], {}], | ||
]; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { test, expect } from '@playwright/test'; | ||
import { | ||
loginToApplication, | ||
INCORRECT_CREDENTIALS, | ||
EMPTY_CREDENTIALS, | ||
} from './auth.utils'; | ||
import { ROUTE } from '@/routes'; | ||
|
||
const ERROR_MESSAGE = 'Unable to authenticate with the given credentials.'; | ||
|
||
test.use({ storageState: { cookies: [], origins: [] } }); | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await page.goto(ROUTE.LOGIN); | ||
}); | ||
|
||
test.describe('Login page', () => { | ||
test('Correct page title', async ({ page }) => { | ||
await page.goto(ROUTE.LOGIN); | ||
|
||
await expect(page).toHaveTitle('Login - The Stack'); | ||
}); | ||
|
||
test('Login into app with given credentials', async ({ page }) => { | ||
await loginToApplication(page); | ||
|
||
await expect(page).toHaveURL('/server-list'); | ||
}); | ||
|
||
for (const [fieldType, { username, password }] of INCORRECT_CREDENTIALS) { | ||
test(`Attempt to login with incorrect ${fieldType.join(' and ')}`, async ({ | ||
page, | ||
}) => { | ||
await page.getByLabel('Username').fill(username); | ||
await page.getByLabel('Password').fill(password); | ||
|
||
await page.getByRole('button', { name: 'Log in' }).click(); | ||
|
||
await expect(page.getByTestId('login-error')).toBeVisible(); | ||
await expect(page.getByTestId('login-error')).toContainText( | ||
ERROR_MESSAGE | ||
); | ||
}); | ||
} | ||
|
||
for (const [emptyFields, credentials] of EMPTY_CREDENTIALS) { | ||
test(`Attempt to login with empty ${emptyFields.join(' and ')}`, async ({ | ||
page, | ||
}) => { | ||
if (credentials.password) { | ||
await page.getByLabel('Password').fill(credentials.password); | ||
} | ||
|
||
if (credentials.username) { | ||
await page.getByLabel('Username').fill(credentials.username); | ||
} | ||
|
||
await page.getByRole('button', { name: 'Log in' }).click(); | ||
|
||
await expect(page.getByRole('status')).toHaveCount(emptyFields.length); | ||
await expect(page.getByRole('status')).toHaveText( | ||
emptyFields.map(field => new RegExp(`${field} is required`, 'i')) | ||
); | ||
}); | ||
} | ||
}); |