diff --git a/.gitignore b/.gitignore index 657a53a..bd91dbd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ node_modules/ /blob-report/ /playwright/.cache/ .idea/ -.env \ No newline at end of file +.env +.auth/ \ No newline at end of file diff --git a/global-setup.ts b/global-setup.ts new file mode 100644 index 0000000..251f093 --- /dev/null +++ b/global-setup.ts @@ -0,0 +1,18 @@ +import { chromium, FullConfig } from '@playwright/test'; +import { LoginPage } from './page-objects/login/loginPage'; +import userData from './fixtures/userData.json'; + +async function globalSetup(config: FullConfig) { + const { baseURL, storageState } = config.projects[0].use; + const browser = await chromium.launch(); + const page = await browser.newPage(); + const loginPage = new LoginPage(page); + + await page.goto(baseURL + '/#/login'); + await loginPage.login(userData.adminUser.username, userData.adminUser.password); + + await page.context().storageState({ path: storageState as string }); + await browser.close(); +} + +export default globalSetup; diff --git a/page-objects/layout/normalLayoutPage.ts b/page-objects/layout/normalLayoutPage.ts index 96816c5..08882df 100644 --- a/page-objects/layout/normalLayoutPage.ts +++ b/page-objects/layout/normalLayoutPage.ts @@ -22,8 +22,11 @@ export default abstract class NormalLayoutPage { } private async checkLoginStatus(): Promise { + if (this.page.url().includes('login')) { + return false; + } return await this.page.evaluate(() => { - const token = localStorage.getItem('jwt_token'); + const token = localStorage.getItem('token'); return !!token; }); } @@ -31,19 +34,9 @@ export default abstract class NormalLayoutPage { private async performLogin() { await this.loginPage.navigate(); await this.loginPage.login(userData.adminUser.username, userData.adminUser.password); - // After successful login, store the JWT token - await this.storeJwtToken(); - } - private async storeJwtToken() { - // Assuming the JWT token is stored in localStorage after login - // You may need to adjust this based on your actual implementation - await this.page.evaluate(() => { - const token = localStorage.getItem('your_jwt_token_key'); - if (token) { - localStorage.setItem('jwt_token', token); - } - }); + // Save the storage state to avoid logging in for each test + await this.page.context().storageState({ path: process.env.STORAGE_STATE || '.auth/state.json' }); } abstract waitForPageLoad(): Promise; diff --git a/playwright.config.ts b/playwright.config.ts index 8b9a76a..0462261 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -14,11 +14,15 @@ const headless = process.env.HEADLESS?.toLowerCase() !== 'false' && process.env. const timeout = Number(process.env.TIMEOUT) || 30000; const workers = process.env.WORKERS ? Number(process.env.WORKERS) : (process.env.CI ? 10 : undefined); const retries = process.env.CI ? 2 : 0; +const storageState = process.env.STORAGE_STATE || '.auth/state.json'; /** * See https://playwright.dev/docs/test-configuration. */ export default defineConfig({ + /* Add global setup */ + globalSetup: require.resolve('./global-setup'), + /* Test directory */ testDir: './tests', /* Run tests in files in parallel */ fullyParallel: true, @@ -43,6 +47,9 @@ export default defineConfig({ /* Take screenshot on failure */ screenshot: 'only-on-failure', + + /* Add storage state */ + storageState, }, /* Configure timeout */ @@ -91,5 +98,5 @@ export default defineConfig({ // command: 'npm run start', // url: 'http://127.0.0.1:3000', // reuseExistingServer: !process.env.CI, - // }, + // } }); diff --git a/tests/login/login.spec.ts b/tests/login/login.spec.ts index 1c3a4c8..a0a2018 100644 --- a/tests/login/login.spec.ts +++ b/tests/login/login.spec.ts @@ -1,7 +1,20 @@ -import { test, expect } from '@playwright/test'; +import { test as base, expect } from '@playwright/test'; import { LoginPage } from '@/page-objects/login/loginPage'; import userData from '@/fixtures/userData.json'; +// Define a new test fixture with a blank storage state +const test = base.extend({ + context: async ({ browser }, use) => { + const context = await browser.newContext({ storageState: undefined }); + await use(context); + await context.close(); + }, + page: async ({ context }, use) => { + const page = await context.newPage(); + await use(page); + }, +}); + test.describe('Login Tests', () => { let loginPage: LoginPage;