File size: 3,327 Bytes
9705b6c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import { Page, FullConfig, chromium } from '@playwright/test';
import cleanupUser from './cleanupUser';
import dotenv from 'dotenv';
dotenv.config();
type User = { email: string; name: string; password: string };
const timeout = 3500;
async function register(page: Page, user: User) {
await page.getByRole('link', { name: 'Sign up' }).click();
await page.getByLabel('Full name').click();
await page.getByLabel('Full name').fill('test');
await page.getByText('Username (optional)').click();
await page.getByLabel('Username (optional)').fill('test');
await page.getByLabel('Email').click();
await page.getByLabel('Email').fill(user.email);
await page.getByLabel('Email').press('Tab');
await page.getByTestId('password').click();
await page.getByTestId('password').fill(user.password);
await page.getByTestId('confirm_password').click();
await page.getByTestId('confirm_password').fill(user.password);
await page.getByLabel('Submit registration').click();
}
async function logout(page: Page, user: User) {
await page.getByRole('button', { name: user.name }).click();
await page.getByRole('button', { name: 'Log out' }).click();
}
async function login(page: Page, user: User) {
await page.locator('input[name="email"]').fill(user.email);
await page.locator('input[name="password"]').fill(user.password);
await page.locator('input[name="password"]').press('Enter');
}
async function authenticate(config: FullConfig, user: User) {
console.log('π€: global setup has been started');
const { baseURL, storageState } = config.projects[0].use;
console.log('π€: using baseURL', baseURL);
console.dir(user, { depth: null });
const browser = await chromium.launch({
// headless: false,
});
const page = await browser.newPage();
console.log('π€: π authenticating user:', user.email);
if (!baseURL) {
throw new Error('π€: baseURL is not defined');
}
// Set localStorage before navigating to the page
await page.context().addInitScript(() => {
localStorage.setItem('navVisible', 'true');
});
console.log('π€: βοΈ localStorage: set Nav as Visible', storageState);
await page.goto(baseURL, { timeout });
await register(page, user);
try {
await page.waitForURL(`${baseURL}/chat/new`, { timeout });
} catch (error) {
console.error('Error:', error);
const userExists = page.getByTestId('registration-error');
if (userExists) {
console.log('π€: π¨ user already exists');
await cleanupUser(user);
await page.goto(baseURL, { timeout });
await register(page, user);
} else {
throw new Error('π€: π¨ user failed to register');
}
}
console.log('π€: βοΈ user successfully registered');
// Logout
await logout(page, user);
await page.waitForURL(`${baseURL}/login`, { timeout });
console.log('π€: βοΈ user successfully logged out');
await login(page, user);
await page.waitForURL(`${baseURL}/chat/new`, { timeout });
console.log('π€: βοΈ user successfully authenticated');
await page.context().storageState({ path: storageState as string });
console.log('π€: βοΈ authentication state successfully saved in', storageState);
await browser.close();
console.log('π€: global setup has been finished');
}
export default authenticate;
|