92 lines
3.2 KiB
JavaScript
92 lines
3.2 KiB
JavaScript
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
const { expect } = require('@playwright/test');
|
||
|
|
|
||
|
|
const LIVE_SITE_BASE_URL = process.env.LIVE_SITE_BASE_URL || 'https://www.regalamiunsorriso.it';
|
||
|
|
const LIVE_SITE_LOGIN_URL = process.env.LIVE_SITE_LOGIN_URL || `${LIVE_SITE_BASE_URL}/login_clienti-it.html`;
|
||
|
|
const LIVE_SITE_RACE_URL = process.env.LIVE_SITE_RACE_URL || `${LIVE_SITE_BASE_URL}/42%20HALF%20MARATHON%20FIRENZE_gara-1018545---96-1.html`;
|
||
|
|
const LIVE_SITE_USERNAME = process.env.LIVE_SITE_USERNAME || '';
|
||
|
|
const LIVE_SITE_PASSWORD = process.env.LIVE_SITE_PASSWORD || '';
|
||
|
|
const AUTH_FILE = path.join(__dirname, '.auth', 'user.json');
|
||
|
|
|
||
|
|
function ensureAuthDirectory() {
|
||
|
|
fs.mkdirSync(path.dirname(AUTH_FILE), { recursive: true });
|
||
|
|
}
|
||
|
|
|
||
|
|
function requireCredentials() {
|
||
|
|
if (!LIVE_SITE_USERNAME || !LIVE_SITE_PASSWORD) {
|
||
|
|
throw new Error('LIVE_SITE_USERNAME and LIVE_SITE_PASSWORD must be set before running the live-site Playwright suite.');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function dismissCookieBanner(page) {
|
||
|
|
const cookieButton = page.getByRole('button', { name: /^(Accetto|Accept)$/i });
|
||
|
|
if (await cookieButton.count()) {
|
||
|
|
await cookieButton.first().click({ timeout: 5000 }).catch(() => {});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const fallbackButton = page.locator('.cc-btn, .cc-dismiss, .cc-allow').filter({ hasText: /Accetto|Accept/i });
|
||
|
|
if (await fallbackButton.count()) {
|
||
|
|
await fallbackButton.first().click({ timeout: 5000 }).catch(() => {});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function loginSubmitLocator(page) {
|
||
|
|
return page.locator('a.btn').filter({ hasText: /Accedi|Sign in/i }).first();
|
||
|
|
}
|
||
|
|
|
||
|
|
async function performLiveLogin(page) {
|
||
|
|
requireCredentials();
|
||
|
|
|
||
|
|
await page.goto(LIVE_SITE_LOGIN_URL, { waitUntil: 'domcontentloaded' });
|
||
|
|
await dismissCookieBanner(page);
|
||
|
|
await page.locator('#login').fill(LIVE_SITE_USERNAME);
|
||
|
|
await page.locator('#pwd').fill(LIVE_SITE_PASSWORD);
|
||
|
|
await loginSubmitLocator(page).click();
|
||
|
|
await waitForLoggedInUi(page);
|
||
|
|
}
|
||
|
|
|
||
|
|
async function waitForLoggedInUi(page) {
|
||
|
|
const accountMenu = page.locator('#navbarDropdownMenuLink');
|
||
|
|
const accountLink = page.locator('a[href*="dettaglio_clienti"]');
|
||
|
|
const logoutLink = page.locator('a[href*="user_logout"]');
|
||
|
|
|
||
|
|
await expect.poll(async () => {
|
||
|
|
if (await accountMenu.count() && await accountMenu.first().isVisible().catch(() => false)) {
|
||
|
|
return 'account-menu';
|
||
|
|
}
|
||
|
|
if (await accountLink.count() && await accountLink.first().isVisible().catch(() => false)) {
|
||
|
|
return 'account-link';
|
||
|
|
}
|
||
|
|
if (await logoutLink.count() && await logoutLink.first().isVisible().catch(() => false)) {
|
||
|
|
return 'logout-link';
|
||
|
|
}
|
||
|
|
return '';
|
||
|
|
}, {
|
||
|
|
timeout: 30 * 1000,
|
||
|
|
message: 'Expected the logged-in account UI to appear after authenticating.'
|
||
|
|
}).not.toBe('');
|
||
|
|
}
|
||
|
|
|
||
|
|
async function expectRacePageLoaded(page) {
|
||
|
|
await expect(page.locator('form[onsubmit="return searching()"]')).toBeVisible();
|
||
|
|
await expect(page.locator('#id_gara')).toHaveValue(/\d+/);
|
||
|
|
await expect(page.locator('script[src*="_js/rus-ecom-240621.js"]')).toHaveCount(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
AUTH_FILE,
|
||
|
|
LIVE_SITE_BASE_URL,
|
||
|
|
LIVE_SITE_LOGIN_URL,
|
||
|
|
LIVE_SITE_PASSWORD,
|
||
|
|
LIVE_SITE_RACE_URL,
|
||
|
|
LIVE_SITE_USERNAME,
|
||
|
|
dismissCookieBanner,
|
||
|
|
ensureAuthDirectory,
|
||
|
|
expectRacePageLoaded,
|
||
|
|
loginSubmitLocator,
|
||
|
|
performLiveLogin,
|
||
|
|
requireCredentials,
|
||
|
|
waitForLoggedInUi
|
||
|
|
};
|