Skip to content

Commit

Permalink
test: updated e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Oct 23, 2024
1 parent c6c69d8 commit 41f5013
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules/
/blob-report/
/playwright/.cache/
.idea/
.env
.env
.auth/
18 changes: 18 additions & 0 deletions global-setup.ts
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 6 additions & 13 deletions page-objects/layout/normalLayoutPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,21 @@ export default abstract class NormalLayoutPage {
}

private async checkLoginStatus(): Promise<boolean> {
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;
});
}

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<void>;
Expand Down
9 changes: 8 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -43,6 +47,9 @@ export default defineConfig({

/* Take screenshot on failure */
screenshot: 'only-on-failure',

/* Add storage state */
storageState,
},

/* Configure timeout */
Expand Down Expand Up @@ -91,5 +98,5 @@ export default defineConfig({
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
// }
});
15 changes: 14 additions & 1 deletion tests/login/login.spec.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit 41f5013

Please sign in to comment.