Files
openobserve/tests/ui-testing/playwright-tests/utils/global-setup.js
T
Neha PandClaude 4c6282bb19 test: fix alert-v3 tests (#11570)
- API-first methods in alertTemplatesPage.js (_findTemplateViaApi,
createTemplateViaApi, deleteTemplateViaApi) to bypass fragile UI
pagination
- API-first destination lookup in alertDestinationsPage.js
(findDestinationViaApi) resolves cascade failure
- v3-compatible locators for alert type dropdown, condition operators,
deduplication flow in alertCreationWizard.js
- Self-contained test pattern with per-test random suffixes and no
shared state
- URL-based navigation with menu fallback for templates and destinations
pages
- q-portal cleanup before click interactions to prevent interception
- Conditional enterprise feature guards at runtime

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-04 11:40:58 +00:00

245 lines
9.0 KiB
JavaScript

const { chromium } = require('@playwright/test');
const path = require('path');
const fs = require('fs');
const logsdata = require("../../../test-data/logs_data.json");
const testLogger = require('./test-logger.js');
const metricsIngestion = require('./metrics-ingestion.js');
const { ingestTraces } = require('./trace-ingestion.js');
const { ingestRumErrors } = require('./rum-error-ingestion.js');
/**
* Global setup for all tests - handles authentication and test data ingestion
* This runs once before all tests start
*/
async function globalSetup() {
testLogger.info('Starting global setup for all tests');
// Create auth storage directory
const authDir = path.join(__dirname, 'auth');
if (!fs.existsSync(authDir)) {
fs.mkdirSync(authDir, { recursive: true });
}
const authFile = path.join(authDir, 'user.json');
// Launch browser and create context
const browser = await chromium.launch();
const context = await browser.newContext({
viewport: { width: 1500, height: 1024 }
});
const page = await context.newPage();
try {
testLogger.info('Performing global login authentication');
// Navigate to login page with org identifier to establish correct org context
const loginUrl = `${process.env["ZO_BASE_URL"]}?org_identifier=${process.env["ORGNAME"]}`;
await page.goto(loginUrl);
testLogger.debug('Navigated to base URL with org', { url: loginUrl });
await page.waitForLoadState('domcontentloaded');
await page.waitForTimeout(2000);
// Debug: Log current URL and page title
const currentUrl = page.url();
const pageTitle = await page.title();
testLogger.debug('Page loaded', { currentUrl, pageTitle });
// Handle internal user login if needed
const internalUserButton = page.getByText('Login as internal user');
if (await internalUserButton.isVisible()) {
await internalUserButton.click();
testLogger.debug('Clicked internal user login button');
await page.waitForLoadState('domcontentloaded');
}
// Fill login credentials
const userIdField = page.locator('[data-cy="login-user-id"]');
const passwordField = page.locator('[data-cy="login-password"]');
const signInButton = page.locator('[data-cy="login-sign-in"]');
// Debug: Check if login fields exist
testLogger.debug('Waiting for login fields to appear...');
try {
await userIdField.waitFor({ state: 'visible', timeout: 15000 });
} catch (e) {
// Take screenshot and log page content for debugging
await page.screenshot({ path: path.join(__dirname, 'debug-login-page.png') });
const bodyText = await page.locator('body').textContent();
testLogger.error('Login field not found', {
url: page.url(),
bodyPreview: bodyText?.substring(0, 500)
});
throw new Error(`Login page did not load correctly. Current URL: ${page.url()}`);
}
await userIdField.fill(process.env["ZO_ROOT_USER_EMAIL"]);
await passwordField.waitFor({ state: 'visible' });
await passwordField.fill(process.env["ZO_ROOT_USER_PASSWORD"]);
await signInButton.waitFor({ state: 'visible' });
await signInButton.click();
testLogger.debug('Login credentials submitted');
// Wait for login to complete - look for navigation or success indicators
await page.waitForLoadState('networkidle', { timeout: 15000 }).catch(() => {});
// Verify login success by checking for a known element
await page.locator('[data-test="menu-link-\\/-item"]').waitFor({
state: 'visible',
timeout: 10000
});
testLogger.info('Global login authentication successful');
// Save authentication state
await context.storageState({ path: authFile });
testLogger.info('Authentication state saved successfully', { authFile });
// Skip data ingestion for cleanup-only runs
// Use precise regex to match only cleanup.spec.js (not some-cleanup.spec.js or cleanup.spec-backup.js)
const isCleanupOnly = process.argv.some(arg => /cleanup\.spec\.(js|ts)$/.test(arg));
if (isCleanupOnly || process.env.SKIP_INGESTION === 'true') {
testLogger.info('Skipping data ingestion (cleanup-only run or SKIP_INGESTION=true)');
} else {
// Perform global test data ingestion
testLogger.info('Starting global test data ingestion');
await performGlobalIngestion(page);
testLogger.info('Global test data ingestion completed');
// Skip global metrics ingestion - it's now handled in test-specific beforeAll hooks
// This avoids 404 errors when OpenObserve metrics endpoint isn't ready during global setup
// await performMetricsIngestion();
testLogger.debug('Metrics ingestion skipped in global setup - will be handled in test beforeAll hooks');
// Perform trace data ingestion
testLogger.info('Starting trace data ingestion');
await ingestTraces(page, 20); // Ingest 20 test traces
testLogger.info('Trace data ingestion completed');
// Perform RUM error data ingestion
testLogger.info('Starting RUM error data ingestion');
await ingestRumErrors(page, 3); // Ingest 3 different error types
testLogger.info('RUM error data ingestion completed');
}
} catch (error) {
testLogger.error('Global setup failed', { error: error.message, stack: error.stack });
throw error;
} finally {
await context.close();
await browser.close();
testLogger.debug('Browser closed after global setup');
}
testLogger.info('Global setup completed successfully - ready for test execution');
}
/**
* Performs global test data ingestion
* @param {import('@playwright/test').Page} page
*/
async function performGlobalIngestion(page) {
const orgId = process.env["ORGNAME"];
const streamName = "e2e_automate";
const basicAuthCredentials = Buffer.from(
`${process.env["ZO_ROOT_USER_EMAIL"]}:${process.env["ZO_ROOT_USER_PASSWORD"]}`
).toString('base64');
const headers = {
"Authorization": `Basic ${basicAuthCredentials}`,
"Content-Type": "application/json",
};
const baseUrl = (process.env.INGESTION_URL || process.env.ZO_BASE_URL || 'http://localhost:5080').replace(/\/+$/, '');
let response;
try {
response = await page.request.post(`${baseUrl}/api/${orgId}/${streamName}/_json`, {
headers: headers,
data: logsdata
});
} catch (requestError) {
testLogger.error('Data ingestion request failed', { error: requestError.message });
throw new Error(`Data ingestion request failed: ${requestError.message}`);
}
const responseStatus = response.status();
let responseData = null;
const contentType = response.headers()['content-type'] || '';
if (contentType.includes('application/json')) {
try {
const text = await response.text();
responseData = text ? JSON.parse(text) : null;
} catch (e) {
responseData = { error: 'Failed to parse JSON response', text: await response.text().catch(() => '') };
}
} else {
responseData = { text: await response.text().catch(() => '') };
}
testLogger.debug('Data ingestion API response received', {
status: responseStatus,
orgId,
streamName,
data: responseData
});
if (responseStatus !== 200) {
testLogger.error('Data ingestion failed', {
status: responseStatus,
response: responseData,
url: `${baseUrl}/api/${orgId}/${streamName}/_json`,
orgId,
streamName
});
throw new Error(`Data ingestion failed with status: ${responseStatus}. Response: ${JSON.stringify(responseData)}`);
}
testLogger.info('Global data ingestion successful', {
status: responseStatus,
orgId,
streamName,
responseData: responseData
});
}
/**
* Performs global metrics data ingestion
*/
async function performMetricsIngestion() {
try {
// Ingest test metrics data
const result = await metricsIngestion.ingestTestMetrics({
iterations: 20, // Send 20 batches of metrics for better test coverage
delay: 100, // 100ms between batches
useExternal: false // Use local OpenObserve instance
});
if (result.success) {
testLogger.info('Metrics ingestion successful', {
totalBatches: result.totalBatches,
successfulBatches: result.successfulBatches
});
} else {
testLogger.warn('Metrics ingestion completed with some failures', {
totalBatches: result.totalBatches,
successfulBatches: result.successfulBatches
});
}
// Also start continuous ingestion that will run during tests
// This runs in the background and doesn't block
metricsIngestion.ingestContinuously(120000, 10000).then(result => {
testLogger.debug('Background metrics ingestion completed', result);
}).catch(error => {
testLogger.error('Background metrics ingestion failed', { error: error.message });
});
} catch (error) {
testLogger.error('Metrics ingestion failed', { error: error.message });
// Don't fail the entire setup if metrics ingestion fails
// Tests can still run without metrics data
}
}
module.exports = globalSetup;