import { Page } from '@playwright/test'; /** * Navigation helper that tries multiple methods to reach a page * 1. Try clicking navigation link * 2. Fall back to direct URL navigation */ export async function navigateToPage( page: Page, linkSelectors: string[], urlPath: string, pageName: string ): Promise { console.log(`Navigating to ${pageName}...`); // Try clicking navigation links first for (const selector of linkSelectors) { const link = page.locator(selector).first(); const isVisible = await link.isVisible({ timeout: 3000 }).catch(() => false); if (isVisible) { console.log(`Found ${pageName} link, clicking...`); await link.click(); await page.waitForLoadState('domcontentloaded', { timeout: 10000 }).catch(() => {}); await page.waitForTimeout(1000); // Verify we're on the right page if (page.url().toLowerCase().includes(urlPath.toLowerCase())) { console.log(`Successfully navigated to ${pageName} via link`); return; } } } // Fall back to direct URL navigation console.log(`${pageName} link not found, using direct URL navigation...`); try { await page.goto(urlPath, { timeout: 15000, waitUntil: 'domcontentloaded' }); await page.waitForTimeout(2000); console.log(`Successfully navigated to ${pageName} via URL`); } catch (error) { console.log(`Failed to navigate to ${urlPath}:`, error); // Try with base URL const baseUrl = page.context().browser()?.contexts()[0]?.pages()[0]?.url() || 'https://devpro.babinnovations.com'; const fullUrl = new URL(urlPath, baseUrl).href; console.log(`Retrying with full URL: ${fullUrl}`); await page.goto(fullUrl, { timeout: 15000, waitUntil: 'domcontentloaded' }); await page.waitForTimeout(2000); console.log(`Successfully navigated to ${pageName} via full URL`); } } export async function navigateToTransactions(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Transaction")', 'a:has-text("Transactions")', '[href*="transaction" i]', 'nav a:has-text("Trans")', ], '/transactions', 'Transactions' ); } export async function navigateToTerminals(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Terminal")', 'a:has-text("Terminals")', '[href*="terminal" i]', 'nav a:has-text("Term")', ], '/terminals', 'Terminals' ); } export async function navigateToRefunds(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Refund")', 'a:has-text("Refunds")', '[href*="refund" i]', 'nav a:has-text("Ref")', ], '/refunds', 'Refunds' ); } export async function navigateToReconciliation(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Reconcil")', 'a:has-text("Reconciliation")', '[href*="reconcil" i]', ], '/reconciliation', 'Reconciliation' ); } export async function navigateToManagement(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Manage")', 'a:has-text("Management")', '[href*="manage" i]', ], '/management', 'Management' ); } export async function navigateToAdmin(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Admin")', '[href*="admin" i]', ], '/admin', 'Admin' ); } export async function navigateToSettlement(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Settlement")', '[href*="settlement" i]', 'nav a:has-text("Settle")', ], '/settlement', 'Settlement' ); } export async function navigateToDiscounts(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Discount")', 'a:has-text("Discounts")', '[href*="discount" i]', 'nav a:has-text("Disc")', ], '/discounts', 'Discounts & Fees' ); } export async function navigateToSettings(page: Page): Promise { await navigateToPage( page, [ 'a:has-text("Settings")', '[href*="settings" i]', 'nav a:has-text("Setting")', ], '/settings', 'Settings' ); }