Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.2 KB
Raw Blame
import { Page } from '@playwright/test'

import { Scene } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'

type LowercaseEnum<T> = {
    [K in keyof T]: T[K] extends string ? Lowercase<T[K]> : never
}[keyof T]

/** Derive possible identifiers from the Scene enum. Not all of them are
 * actually nav items. This will help keep e2e tests up-to-date when
 * refactoring scenes though. */
export type Identifier = LowercaseEnum<typeof Scene>

const IDENTIFIER_URL_FALLBACKS: Record<string, string> = {
    projecthomepage: urls.projectHomepage(),
    activity: urls.activity(),
    datamanagement: urls.eventDefinitions(),
    'event-definitions': urls.eventDefinitions(),
    annotations: urls.annotations(),
    toolbar: urls.toolbarLaunch(),
    'sql-editor': urls.sqlEditor(),
    settings: urls.settings(),
    surveys: '/surveys',
    cohorts: '/cohorts',
    dashboards: '/dashboard',
    people: '/persons',
    persons: '/persons',
    action: '/data-management/actions',
    'product-analytics': '/insights',
    insight: '/insights/new',
    insights: '/insights',
}

function teamIdFromPath(pathname: string): number | null {
    const match = pathname.match(/^\/project\/(\d+)(?:\/|$)/)
    return match ? Number(match[1]) : null
}

export class Navigation {
    readonly page: Page

    constructor(page: Page) {
        this.page = page
    }

    async openHome(): Promise<void> {
        await this.page.goto(urls.projectRoot())
    }

    async openMenuItem(name: string): Promise<void> {
        // The legacy PanelLayoutNavBar exposed every scene as `menu-item-<name>` /
        // `navbar-<name>`. The AI-first navbar only renders a few `nav-item-*` test-ids
        // (and only when the parent collapsible section is expanded), so for known scenes
        // we navigate by URL — that's the most reliable cross-layout behavior.
        if (IDENTIFIER_URL_FALLBACKS[name]) {
            // Caddy proxies the literal paths `/surveys`, `/surveys/`, `/api/surveys`
            // to the SDK's hypercache-server, and going through `/project/<teamId>/...`
            // avoids that matcher and lets the app load normally.
            await this.page.waitForURL(/\/project\/\d+(?:\/|$)/, { timeout: 10_000 }).catch(() => {})
            const teamId = teamIdFromPath(new URL(this.page.url()).pathname)
            const path = IDENTIFIER_URL_FALLBACKS[name]
            const target = teamId !== null ? `/project/${teamId}${path}` : path
            await this.page.goto(target)
        } else {
            // Fall back to legacy selectors for scenes we haven't mapped yet.
            const navbarSelector = this.page.getByTestId(`navbar-${name}`)
            const menuSelector = this.page.getByTestId(`menu-item-${name}`)
            const element = (await navbarSelector.count()) > 0 ? navbarSelector : menuSelector
            await element.click()
        }
        // Wait for navigation to complete and page to be ready
        await this.page.waitForLoadState('domcontentloaded')
        // Additional wait with timeout for network to settle (catches lazy-loaded components)
        await this.page.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {
            // Ignore timeout - networkidle may not occur with long-polling
        })
    }
}